001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.load.java.structure.impl;
018
019 import com.intellij.psi.*;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.kotlin.load.java.structure.JavaAnnotation;
023 import org.jetbrains.kotlin.load.java.structure.JavaAnnotationArgument;
024 import org.jetbrains.kotlin.load.java.structure.JavaClass;
025 import org.jetbrains.kotlin.name.ClassId;
026 import org.jetbrains.kotlin.name.FqName;
027 import org.jetbrains.kotlin.name.Name;
028
029 import java.util.Collection;
030
031 import static org.jetbrains.kotlin.load.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.namedAnnotationArguments;
032
033 public class JavaAnnotationImpl extends JavaElementImpl<PsiAnnotation> implements JavaAnnotation {
034 public JavaAnnotationImpl(@NotNull PsiAnnotation psiAnnotation) {
035 super(psiAnnotation);
036 }
037
038 @Override
039 @Nullable
040 public JavaAnnotationArgument findArgument(@NotNull Name name) {
041 PsiAnnotationMemberValue attribute = getPsi().findAttributeValue(name.asString());
042 return attribute == null ? null : JavaAnnotationArgumentImpl.Factory.create(attribute, name);
043 }
044
045 @Override
046 @NotNull
047 public Collection<JavaAnnotationArgument> getArguments() {
048 return namedAnnotationArguments(getPsi().getParameterList().getAttributes());
049 }
050
051 @Override
052 @Nullable
053 public ClassId getClassId() {
054 PsiClass resolved = resolvePsi();
055 if (resolved != null) return computeClassId(resolved);
056
057 // External annotations do not have PSI behind them,
058 // so we can only heuristically reconstruct annotation class ids from qualified names
059 String qualifiedName = getPsi().getQualifiedName();
060 if (qualifiedName != null) return ClassId.topLevel(new FqName(qualifiedName));
061
062 return null;
063 }
064
065 @Nullable
066 @Override
067 public JavaClass resolve() {
068 PsiClass resolved = resolvePsi();
069 return resolved == null ? null : new JavaClassImpl(resolved);
070 }
071
072 @Nullable
073 private static ClassId computeClassId(@NotNull PsiClass psiClass) {
074 PsiClass container = psiClass.getContainingClass();
075 if (container != null) {
076 ClassId parentClassId = computeClassId(container);
077 return parentClassId == null ? null : parentClassId.createNestedClassId(Name.identifier(psiClass.getName()));
078 }
079
080 String fqName = psiClass.getQualifiedName();
081 return fqName == null ? null : ClassId.topLevel(new FqName(fqName));
082 }
083
084 @Nullable
085 private PsiClass resolvePsi() {
086 PsiJavaCodeReferenceElement referenceElement = getPsi().getNameReferenceElement();
087 if (referenceElement == null) return null;
088
089 PsiElement resolved = referenceElement.resolve();
090 return resolved instanceof PsiClass ? (PsiClass) resolved : null;
091 }
092 }