1 11 package org.eclipse.jdt.internal.corext.dom; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.eclipse.text.edits.TextEditGroup; 17 18 import org.eclipse.jdt.core.dom.AST; 19 import org.eclipse.jdt.core.dom.ASTNode; 20 import org.eclipse.jdt.core.dom.Annotation; 21 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; 22 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; 23 import org.eclipse.jdt.core.dom.EnumConstantDeclaration; 24 import org.eclipse.jdt.core.dom.EnumDeclaration; 25 import org.eclipse.jdt.core.dom.FieldDeclaration; 26 import org.eclipse.jdt.core.dom.IExtendedModifier; 27 import org.eclipse.jdt.core.dom.MethodDeclaration; 28 import org.eclipse.jdt.core.dom.Modifier; 29 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 30 import org.eclipse.jdt.core.dom.TypeDeclaration; 31 import org.eclipse.jdt.core.dom.VariableDeclarationExpression; 32 import org.eclipse.jdt.core.dom.VariableDeclarationStatement; 33 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 34 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 35 36 39 public class ModifierRewrite { 40 41 public static final int VISIBILITY_MODIFIERS= Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED; 42 43 private ListRewrite fModifierRewrite; 44 private AST fAst; 45 46 47 public static ModifierRewrite create(ASTRewrite rewrite, ASTNode declNode) { 48 return new ModifierRewrite(rewrite, declNode); 49 } 50 51 private ModifierRewrite(ASTRewrite rewrite, ASTNode declNode) { 52 fModifierRewrite= evaluateListRewrite(rewrite, declNode); 53 fAst= declNode.getAST(); 54 } 55 56 private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) { 57 switch (declNode.getNodeType()) { 58 case ASTNode.METHOD_DECLARATION: 59 return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY); 60 case ASTNode.FIELD_DECLARATION: 61 return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY); 62 case ASTNode.VARIABLE_DECLARATION_EXPRESSION: 63 return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY); 64 case ASTNode.VARIABLE_DECLARATION_STATEMENT: 65 return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY); 66 case ASTNode.SINGLE_VARIABLE_DECLARATION: 67 return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY); 68 case ASTNode.TYPE_DECLARATION: 69 return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY); 70 case ASTNode.ENUM_DECLARATION: 71 return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY); 72 case ASTNode.ANNOTATION_TYPE_DECLARATION: 73 return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY); 74 case ASTNode.ENUM_CONSTANT_DECLARATION: 75 return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY); 76 case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION: 77 return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY); 78 default: 79 throw new IllegalArgumentException ("node has no modifiers: " + declNode.getClass().getName()); } 81 } 82 83 public ListRewrite getModifierRewrite() { 84 return fModifierRewrite; 85 } 86 87 public void setModifiers(int modfiers, TextEditGroup editGroup) { 88 internalSetModifiers(modfiers, -1, editGroup); 89 } 90 91 public void setModifiers(int included, int excluded, TextEditGroup editGroup) { 92 internalSetModifiers(included, included | excluded, editGroup); 93 } 94 95 public void setVisibility(int visibilityFlags, TextEditGroup editGroup) { 96 internalSetModifiers(visibilityFlags, VISIBILITY_MODIFIERS, editGroup); 97 } 98 99 public void copyAllModifiers(ASTNode otherDecl, TextEditGroup editGroup) { 100 ListRewrite modifierList= evaluateListRewrite(fModifierRewrite.getASTRewrite(), otherDecl); 101 List originalList= modifierList.getOriginalList(); 102 if (originalList.isEmpty()) { 103 return; 104 } 105 106 ASTNode copy= modifierList.createCopyTarget((ASTNode) originalList.get(0), (ASTNode) originalList.get(originalList.size() - 1)); 107 if (copy != null) { 108 fModifierRewrite.insertLast(copy, editGroup); 109 } 110 } 111 112 public void copyAllAnnotations(ASTNode otherDecl, TextEditGroup editGroup) { 113 ListRewrite modifierList= evaluateListRewrite(fModifierRewrite.getASTRewrite(), otherDecl); 114 List originalList= modifierList.getOriginalList(); 115 116 for (Iterator iterator= originalList.iterator(); iterator.hasNext();) { 117 IExtendedModifier modifier= (IExtendedModifier) iterator.next(); 118 if (modifier.isAnnotation()) { 119 fModifierRewrite.insertLast(fModifierRewrite.getASTRewrite().createCopyTarget((Annotation) modifier), editGroup); 120 } 121 } 122 } 123 124 private void internalSetModifiers(int modfiers, int consideredFlags, TextEditGroup editGroup) { 125 int newModifiers= modfiers & consideredFlags; 127 128 List originalList= fModifierRewrite.getOriginalList(); 129 for (int i= 0; i < originalList.size(); i++) { 130 ASTNode curr= (ASTNode) originalList.get(i); 131 if (curr instanceof Modifier) { 132 int flag= ((Modifier)curr).getKeyword().toFlagValue(); 133 if ((consideredFlags & flag) != 0) { 134 if ((newModifiers & flag) == 0) { 135 fModifierRewrite.remove(curr, editGroup); 136 } 137 newModifiers &= ~flag; 138 } 139 } 140 } 141 142 IExtendedModifier lastAnnotation= null; 144 List extendedList= fModifierRewrite.getRewrittenList(); 145 for (int i= 0; i < extendedList.size(); i++) { 146 IExtendedModifier curr= (IExtendedModifier) extendedList.get(i); 147 if (curr.isAnnotation()) 148 lastAnnotation= curr; 149 } 150 151 List newNodes= ASTNodeFactory.newModifiers(fAst, newModifiers); 153 for (int i= 0; i < newNodes.size(); i++) { 154 Modifier curr= (Modifier) newNodes.get(i); 155 if ((curr.getKeyword().toFlagValue() & VISIBILITY_MODIFIERS) != 0) { 156 if (lastAnnotation != null) 157 fModifierRewrite.insertAfter(curr, (ASTNode) lastAnnotation, editGroup); 158 else 159 fModifierRewrite.insertFirst(curr, editGroup); 160 } else { 161 fModifierRewrite.insertLast(curr, editGroup); 162 } 163 } 164 } 165 } 166 | Popular Tags |