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
.lang
.ASTNode
;
19 import com
.intellij
.openapi
.diagnostic
.Logger
;
20 import com
.intellij
.psi
.PsiKeyword
;
21 import com
.intellij
.psi
.PsiModifier
;
22 import com
.intellij
.psi
.PsiModifierList
;
23 import com
.intellij
.psi
.impl
.source
.Constants
;
24 import com
.intellij
.psi
.impl
.source
.SourceTreeToPsiMap
;
25 import com
.intellij
.psi
.impl
.source
.tree
.*;
26 import com
.intellij
.psi
.tree
.ChildRoleBase
;
27 import com
.intellij
.util
.containers
.HashMap
;
29 public class ModifierListElement
extends CompositeElement
{
30 private static final Logger LOG
= Logger
.getInstance("#com.intellij.psi.impl.source.tree.java.ModifierListElement");
32 public ModifierListElement() {
33 super(Constants
.MODIFIER_LIST
);
36 public TreeElement
addInternal(TreeElement first
, ASTNode last
, ASTNode anchor
, Boolean before
) {
38 if (first
== last
&& ElementType
.KEYWORD_BIT_SET
.contains(first
.getElementType())){
39 anchor
= getDefaultAnchor((PsiModifierList
)SourceTreeToPsiMap
.treeElementToPsi(this),
40 (PsiKeyword
)SourceTreeToPsiMap
.treeElementToPsi(first
));
41 before
= Boolean
.TRUE
;
44 return super.addInternal(first
, last
, anchor
, before
);
47 public int getChildRole(ASTNode child
) {
48 LOG
.assertTrue(child
.getTreeParent() == this);
49 if (child
.getElementType() == JavaElementType
.ANNOTATION
) return ChildRole
.ANNOTATION
;
50 return ChildRoleBase
.NONE
;
53 private static final HashMap
<String
, Integer
> ourModifierToOrderMap
= new HashMap
<String
, Integer
>();
55 static { //TODO : options?
56 ourModifierToOrderMap
.put(PsiModifier
.PUBLIC
, 1);
57 ourModifierToOrderMap
.put(PsiModifier
.PRIVATE
, 1);
58 ourModifierToOrderMap
.put(PsiModifier
.PROTECTED
, 1);
59 ourModifierToOrderMap
.put(PsiModifier
.STATIC
, 2);
60 ourModifierToOrderMap
.put(PsiModifier
.ABSTRACT
, 2);
61 ourModifierToOrderMap
.put(PsiModifier
.FINAL
, 3);
62 ourModifierToOrderMap
.put(PsiModifier
.SYNCHRONIZED
, 4);
63 ourModifierToOrderMap
.put(PsiModifier
.TRANSIENT
, 4);
64 ourModifierToOrderMap
.put(PsiModifier
.VOLATILE
, 4);
65 ourModifierToOrderMap
.put(PsiModifier
.NATIVE
, 5);
66 ourModifierToOrderMap
.put(PsiModifier
.STRICTFP
, 6);
69 private static ASTNode
getDefaultAnchor(PsiModifierList modifierList
, PsiKeyword modifier
) {
70 Integer order
= ourModifierToOrderMap
.get(modifier
.getText());
71 if (order
== null) return null;
72 for (ASTNode child
= SourceTreeToPsiMap
.psiElementToTree(modifierList
).getFirstChildNode(); child
!= null; child
= child
.getTreeNext())
74 if (ElementType
.KEYWORD_BIT_SET
.contains(child
.getElementType())) {
75 Integer order1
= ourModifierToOrderMap
.get(child
.getText());
76 if (order1
== null) continue;
77 if (order1
.intValue() > order
.intValue()) {