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
.compiled
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.pom
.Navigatable
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.impl
.PsiImplUtil
;
22 import com
.intellij
.psi
.impl
.java
.stubs
.PsiAnnotationStub
;
23 import com
.intellij
.psi
.impl
.meta
.MetaRegistry
;
24 import com
.intellij
.psi
.impl
.source
.SourceTreeToPsiMap
;
25 import com
.intellij
.psi
.impl
.source
.tree
.ChildRole
;
26 import com
.intellij
.psi
.impl
.source
.tree
.CompositeElement
;
27 import com
.intellij
.psi
.impl
.source
.tree
.TreeElement
;
28 import com
.intellij
.psi
.meta
.PsiMetaData
;
29 import com
.intellij
.util
.IncorrectOperationException
;
30 import org
.jetbrains
.annotations
.NonNls
;
31 import org
.jetbrains
.annotations
.NotNull
;
32 import org
.jetbrains
.annotations
.Nullable
;
37 public class ClsAnnotationImpl
extends ClsRepositoryPsiElement
<PsiAnnotationStub
> implements PsiAnnotation
, Navigatable
{
38 private static final Logger LOG
= Logger
.getInstance("com.intellij.psi.impl.compiled.ClsAnnotationImpl");
39 private ClsJavaCodeReferenceElementImpl myReferenceElement
; //protected by lock
40 private ClsAnnotationParameterListImpl myParameterList
; //protected by lock
41 private final Object lock
= new Object();
43 public ClsAnnotationImpl(final PsiAnnotationStub stub
) {
47 public void appendMirrorText(final int indentLevel
, final StringBuffer buffer
) {
48 buffer
.append("@").append(getReferenceElement().getCanonicalText());
49 ((ClsAnnotationParameterListImpl
)getParameterList()).appendMirrorText(indentLevel
, buffer
);
52 public void setMirror(@NotNull TreeElement element
) {
53 setMirrorCheckingType(element
, null);
55 PsiAnnotation mirror
= (PsiAnnotation
)SourceTreeToPsiMap
.treeElementToPsi(element
);
56 ((ClsElementImpl
)getParameterList()).setMirror((TreeElement
)SourceTreeToPsiMap
.psiElementToTree(mirror
.getParameterList()));
57 ((ClsElementImpl
)getNameReferenceElement()).setMirror((TreeElement
)SourceTreeToPsiMap
.psiElementToTree(mirror
.getNameReferenceElement()));
61 public PsiElement
[] getChildren() {
62 return new PsiElement
[]{getReferenceElement(), getParameterList()};
65 public void accept(@NotNull PsiElementVisitor visitor
) {
66 if (visitor
instanceof JavaElementVisitor
) {
67 ((JavaElementVisitor
)visitor
).visitAnnotation(this);
70 visitor
.visitElement(this);
75 public PsiAnnotationParameterList
getParameterList() {
77 if (myParameterList
== null) {
78 final PsiAnnotationStub stub
= getStub();
79 final CompositeElement mirror
= stub
.getTreeElement();
81 final PsiAnnotationParameterList paramList
= (PsiAnnotationParameterList
)mirror
.findChildByRoleAsPsiElement(ChildRole
.PARAMETER_LIST
);
83 myParameterList
= new ClsAnnotationParameterListImpl(this, paramList
.getAttributes());
86 return myParameterList
;
90 @Nullable public String
getQualifiedName() {
91 if (getReferenceElement() == null) return null;
92 return getReferenceElement().getCanonicalText();
95 public PsiJavaCodeReferenceElement
getNameReferenceElement() {
96 return getReferenceElement();
99 public PsiAnnotationMemberValue
findAttributeValue(String attributeName
) {
100 return PsiImplUtil
.findAttributeValue(this, attributeName
);
104 public PsiAnnotationMemberValue
findDeclaredAttributeValue(@NonNls final String attributeName
) {
105 return PsiImplUtil
.findDeclaredAttributeValue(this, attributeName
);
108 public <T
extends PsiAnnotationMemberValue
> T
setDeclaredAttributeValue(@NonNls String attributeName
, T value
) {
109 throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE
);
112 public String
getText() {
113 final StringBuffer buffer
= new StringBuffer();
114 appendMirrorText(0, buffer
);
115 return buffer
.toString();
118 public PsiMetaData
getMetaData() {
119 return MetaRegistry
.getMetaBase(this);
122 private ClsJavaCodeReferenceElementImpl
getReferenceElement() {
123 synchronized (lock
) {
124 if (myReferenceElement
== null) {
125 final PsiAnnotationStub stub
= getStub();
126 final CompositeElement mirror
= stub
.getTreeElement();
127 myReferenceElement
= new ClsJavaCodeReferenceElementImpl(this, mirror
.findChildByRole(ChildRole
.CLASS_REFERENCE
).getText());
130 return myReferenceElement
;
134 public PsiAnnotationOwner
getOwner() {
135 return (PsiAnnotationOwner
)getParent();//todo