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
.psi
.*;
20 import com
.intellij
.psi
.impl
.PsiImplUtil
;
21 import com
.intellij
.psi
.impl
.java
.stubs
.JavaStubElementTypes
;
22 import com
.intellij
.psi
.impl
.java
.stubs
.PsiModifierListStub
;
23 import com
.intellij
.psi
.impl
.source
.PsiModifierListImpl
;
24 import com
.intellij
.psi
.impl
.source
.SourceTreeToPsiMap
;
25 import com
.intellij
.psi
.impl
.source
.tree
.JavaElementType
;
26 import com
.intellij
.psi
.impl
.source
.tree
.TreeElement
;
27 import com
.intellij
.util
.IncorrectOperationException
;
28 import org
.jetbrains
.annotations
.NonNls
;
29 import org
.jetbrains
.annotations
.NotNull
;
31 public class ClsModifierListImpl
extends ClsRepositoryPsiElement
<PsiModifierListStub
> implements PsiModifierList
{
32 private static final Logger LOG
= Logger
.getInstance("#com.intellij.psi.impl.compiled.ClsModifierListImpl");
34 public ClsModifierListImpl(final PsiModifierListStub stub
) {
39 public PsiElement
[] getChildren() {
40 return getAnnotations();
43 public boolean hasModifierProperty(@NotNull String name
) {
44 int flag
= PsiModifierListImpl
.NAME_TO_MODIFIER_FLAG_MAP
.get(name
);
46 return (getStub().getModifiersMask() & flag
) != 0;
49 public boolean hasExplicitModifier(@NotNull String name
) {
50 return hasModifierProperty(name
);
53 public void setModifierProperty(@NotNull String name
, boolean value
) throws IncorrectOperationException
{
54 throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE
);
57 public void checkSetModifierProperty(@NotNull String name
, boolean value
) throws IncorrectOperationException
{
58 throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE
);
62 public PsiAnnotation
[] getAnnotations() {
63 return getStub().getChildrenByType(JavaStubElementTypes
.ANNOTATION
, PsiAnnotation
.ARRAY_FACTORY
);
67 public PsiAnnotation
[] getApplicableAnnotations() {
68 return getAnnotations();
71 public PsiAnnotation
findAnnotation(@NotNull String qualifiedName
) {
72 return PsiImplUtil
.findAnnotation(this, qualifiedName
);
76 public PsiAnnotation
addAnnotation(@NotNull @NonNls String qualifiedName
) {
77 throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE
);
80 private boolean isAnnotationFormattingAllowed() {
81 final PsiElement element
= getParent();
82 return element
instanceof PsiClass
83 || element
instanceof PsiMethod
84 || element
instanceof PsiField
;
87 public void appendMirrorText(final int indentLevel
, final StringBuffer buffer
) {
88 PsiAnnotation
[] annotations
= getAnnotations();
89 final boolean formattingAllowed
= isAnnotationFormattingAllowed();
90 for (PsiAnnotation annotation
: annotations
) {
91 ((ClsAnnotationImpl
)annotation
).appendMirrorText(indentLevel
, buffer
);
92 if (formattingAllowed
) {
93 goNextLine(indentLevel
, buffer
);
99 PsiElement parent
= getParent();
101 //TODO : filtering & ordering modifiers can go to CodeStyleManager
102 boolean isInterface
= parent
instanceof PsiClass
&& ((PsiClass
)parent
).isInterface();
103 boolean isInterfaceMethod
= parent
instanceof PsiMethod
&& parent
.getParent() instanceof PsiClass
&& ((PsiClass
)parent
.getParent()).isInterface();
104 boolean isInterfaceField
= parent
instanceof PsiField
&& parent
.getParent() instanceof PsiClass
&& ((PsiClass
)parent
.getParent()).isInterface();
105 boolean isInterfaceClass
= parent
instanceof PsiClass
&& parent
.getParent() instanceof PsiClass
&& ((PsiClass
)parent
.getParent()).isInterface();
106 if (hasModifierProperty(PsiModifier
.PUBLIC
)) {
107 if (!isInterfaceMethod
&& !isInterfaceField
&& !isInterfaceClass
) {
108 buffer
.append(PsiModifier
.PUBLIC
);
112 if (hasModifierProperty(PsiModifier
.PROTECTED
)) {
113 buffer
.append(PsiModifier
.PROTECTED
);
116 if (hasModifierProperty(PsiModifier
.PRIVATE
)) {
117 buffer
.append(PsiModifier
.PRIVATE
);
120 if (hasModifierProperty(PsiModifier
.STATIC
)) {
121 if (!isInterfaceField
) {
122 buffer
.append(PsiModifier
.STATIC
);
126 if (hasModifierProperty(PsiModifier
.ABSTRACT
)) {
127 if (!isInterface
&& !isInterfaceMethod
) {
128 buffer
.append(PsiModifier
.ABSTRACT
);
132 if (hasModifierProperty(PsiModifier
.FINAL
)) {
133 if (!isInterfaceField
) {
134 buffer
.append(PsiModifier
.FINAL
);
138 if (hasModifierProperty(PsiModifier
.NATIVE
)) {
139 buffer
.append(PsiModifier
.NATIVE
);
142 if (hasModifierProperty(PsiModifier
.SYNCHRONIZED
)) {
143 buffer
.append(PsiModifier
.SYNCHRONIZED
);
146 if (hasModifierProperty(PsiModifier
.TRANSIENT
)) {
147 buffer
.append(PsiModifier
.TRANSIENT
);
150 if (hasModifierProperty(PsiModifier
.VOLATILE
)) {
151 buffer
.append(PsiModifier
.VOLATILE
);
156 public void setMirror(@NotNull TreeElement element
) {
157 setMirrorCheckingType(element
, JavaElementType
.MODIFIER_LIST
);
159 PsiElement
[] mirrorAnnotations
= ((PsiModifierList
)SourceTreeToPsiMap
.treeElementToPsi(element
)).getAnnotations();
160 PsiAnnotation
[] annotations
= getAnnotations();
161 LOG
.assertTrue(annotations
.length
== mirrorAnnotations
.length
);
162 for (int i
= 0; i
< annotations
.length
; i
++) {
163 ((ClsElementImpl
)annotations
[i
]).setMirror((TreeElement
)SourceTreeToPsiMap
.psiElementToTree(mirrorAnnotations
[i
]));
167 public void accept(@NotNull PsiElementVisitor visitor
) {
168 if (visitor
instanceof JavaElementVisitor
) {
169 ((JavaElementVisitor
)visitor
).visitModifierList(this);
172 visitor
.visitElement(this);
176 public String
toString() {
177 return "PsiModifierList";