2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.psi
.impl
.source
.tree
.java
;
18 import com
.intellij
.codeInsight
.daemon
.impl
.analysis
.AnnotationsHighlightUtil
;
19 import com
.intellij
.lang
.ASTNode
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.psi
.*;
22 import com
.intellij
.psi
.impl
.PsiImplUtil
;
23 import com
.intellij
.psi
.impl
.java
.stubs
.JavaStubElementTypes
;
24 import com
.intellij
.psi
.impl
.java
.stubs
.PsiAnnotationStub
;
25 import com
.intellij
.psi
.impl
.meta
.MetaRegistry
;
26 import com
.intellij
.psi
.impl
.source
.JavaStubPsiElement
;
27 import com
.intellij
.psi
.impl
.source
.tree
.ChildRole
;
28 import com
.intellij
.psi
.impl
.source
.tree
.CompositeElement
;
29 import com
.intellij
.psi
.meta
.PsiMetaData
;
30 import com
.intellij
.util
.PairFunction
;
31 import org
.jetbrains
.annotations
.NonNls
;
32 import org
.jetbrains
.annotations
.NotNull
;
33 import org
.jetbrains
.annotations
.Nullable
;
38 public class PsiAnnotationImpl
extends JavaStubPsiElement
<PsiAnnotationStub
> implements PsiAnnotation
{
39 private static final PairFunction
<Project
, String
, PsiAnnotation
> ANNOTATION_CREATOR
= new PairFunction
<Project
, String
, PsiAnnotation
>() {
40 public PsiAnnotation
fun(Project project
, String text
) {
41 return JavaPsiFacade
.getInstance(project
).getElementFactory().createAnnotationFromText(text
, null);
45 public PsiAnnotationImpl(final PsiAnnotationStub stub
) {
46 super(stub
, JavaStubElementTypes
.ANNOTATION
);
49 public PsiAnnotationImpl(final ASTNode node
) {
53 public PsiJavaCodeReferenceElement
getNameReferenceElement() {
54 return (PsiJavaCodeReferenceElement
)getMirrorTreeElement().findChildByRoleAsPsiElement(ChildRole
.CLASS_REFERENCE
);
58 private CompositeElement
getMirrorTreeElement() {
59 final PsiAnnotationStub stub
= getStub();
61 return stub
.getTreeElement();
64 return (CompositeElement
)getNode();
67 public PsiAnnotationMemberValue
findAttributeValue(String attributeName
) {
68 return PsiImplUtil
.findAttributeValue(this, attributeName
);
72 public PsiAnnotationMemberValue
findDeclaredAttributeValue(@NonNls final String attributeName
) {
73 return PsiImplUtil
.findDeclaredAttributeValue(this, attributeName
);
76 public <T
extends PsiAnnotationMemberValue
> T
setDeclaredAttributeValue(@NonNls String attributeName
, @Nullable T value
) {
77 return (T
)PsiImplUtil
.setDeclaredAttributeValue(this, attributeName
, value
, ANNOTATION_CREATOR
);
80 public String
toString() {
81 return "PsiAnnotation";
85 public PsiAnnotationParameterList
getParameterList() {
86 return (PsiAnnotationParameterList
)getMirrorTreeElement().findChildByRoleAsPsiElement(ChildRole
.PARAMETER_LIST
);
89 @Nullable public String
getQualifiedName() {
90 final PsiJavaCodeReferenceElement nameRef
= getNameReferenceElement();
91 if (nameRef
== null) return null;
92 return nameRef
.getCanonicalText();
95 public final void accept(@NotNull PsiElementVisitor visitor
) {
96 if (visitor
instanceof JavaElementVisitor
) {
97 ((JavaElementVisitor
)visitor
).visitAnnotation(this);
100 visitor
.visitElement(this);
104 public PsiMetaData
getMetaData() {
105 return MetaRegistry
.getMetaBase(this);
108 public PsiAnnotationOwner
getOwner() {
109 PsiElement parent
= getParent();
110 if (parent
instanceof PsiTypeElement
) {
111 return ((PsiTypeElement
)parent
).getOwner(this);
113 if (parent
instanceof PsiMethodReceiver
|| parent
instanceof PsiTypeParameter
) return (PsiAnnotationOwner
)parent
;
114 PsiElement member
= parent
.getParent();
115 String
[] elementTypeFields
= AnnotationsHighlightUtil
.getApplicableElementTypeFields(member
);
116 if (elementTypeFields
== null) return null;
117 if (AnnotationsHighlightUtil
.isAnnotationApplicableTo(this, true, elementTypeFields
)) return (PsiAnnotationOwner
)parent
;
119 PsiAnnotationOwner typeElement
;
120 if (member
instanceof PsiVariable
) {
121 typeElement
= ((PsiVariable
)member
).getTypeElement();
123 else if (member
instanceof PsiMethod
) {
124 typeElement
= ((PsiMethod
)member
).getReturnTypeElement();
126 else if (parent
instanceof PsiAnnotationOwner
) {
127 typeElement
= (PsiAnnotationOwner
)parent
;