1 11 package org.eclipse.jdt.internal.corext.dom; 12 13 import java.util.Arrays ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.text.edits.TextEditGroup; 18 19 import org.eclipse.jdt.core.dom.AST; 20 import org.eclipse.jdt.core.dom.ASTNode; 21 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 22 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; 23 import org.eclipse.jdt.core.dom.Block; 24 import org.eclipse.jdt.core.dom.FieldDeclaration; 25 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 26 import org.eclipse.jdt.core.dom.Type; 27 import org.eclipse.jdt.core.dom.VariableDeclarationExpression; 28 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 29 import org.eclipse.jdt.core.dom.VariableDeclarationStatement; 30 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 31 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 32 33 34 public class VariableDeclarationRewrite { 35 36 public static void rewriteModifiers(final SingleVariableDeclaration declarationNode, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) { 37 ModifierRewrite listRewrite= ModifierRewrite.create(rewrite, declarationNode); 38 listRewrite.setModifiers(includedModifiers, excludedModifiers, group); 39 } 40 41 public static void rewriteModifiers(final VariableDeclarationExpression declarationNode, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) { 42 ModifierRewrite listRewrite= ModifierRewrite.create(rewrite, declarationNode); 43 listRewrite.setModifiers(includedModifiers, excludedModifiers, group); 44 } 45 46 public static void rewriteModifiers(final FieldDeclaration declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) { 47 final List fragmentsToChange= Arrays.asList(toChange); 48 AST ast= declarationNode.getAST(); 49 50 List fragments= declarationNode.fragments(); 51 Iterator iter= fragments.iterator(); 52 53 ListRewrite blockRewrite; 54 if (declarationNode.getParent() instanceof AbstractTypeDeclaration) { 55 blockRewrite= rewrite.getListRewrite(declarationNode.getParent(), ((AbstractTypeDeclaration)declarationNode.getParent()).getBodyDeclarationsProperty()); 56 } else { 57 blockRewrite= rewrite.getListRewrite(declarationNode.getParent(), AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); 58 } 59 60 VariableDeclarationFragment lastFragment= (VariableDeclarationFragment)iter.next(); 61 ASTNode lastStatement= declarationNode; 62 63 if (fragmentsToChange.contains(lastFragment)) { 64 ModifierRewrite modifierRewrite= ModifierRewrite.create(rewrite, declarationNode); 65 modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group); 66 } 67 68 ListRewrite fragmentsRewrite= null; 69 while (iter.hasNext()) { 70 VariableDeclarationFragment currentFragment= (VariableDeclarationFragment)iter.next(); 71 72 if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) { 73 74 FieldDeclaration newStatement= ast.newFieldDeclaration((VariableDeclarationFragment)rewrite.createMoveTarget(currentFragment)); 75 newStatement.setType((Type)rewrite.createCopyTarget(declarationNode.getType())); 76 77 ModifierRewrite modifierRewrite= ModifierRewrite.create(rewrite, newStatement); 78 if (fragmentsToChange.contains(currentFragment)) { 79 modifierRewrite.copyAllAnnotations(declarationNode, group); 80 modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group); 81 } else { 82 modifierRewrite.copyAllModifiers(declarationNode, group); 83 } 84 blockRewrite.insertAfter(newStatement, lastStatement, group); 85 86 fragmentsRewrite= rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY); 87 lastStatement= newStatement; 88 } else if (fragmentsRewrite != null) { 89 ASTNode fragment0= rewrite.createMoveTarget(currentFragment); 90 fragmentsRewrite.insertLast(fragment0, group); 91 } 92 lastFragment= currentFragment; 93 } 94 } 95 96 public static void rewriteModifiers(final VariableDeclarationStatement declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, ASTRewrite rewrite, final TextEditGroup group) { 97 final List fragmentsToChange= Arrays.asList(toChange); 98 AST ast= declarationNode.getAST(); 99 100 List fragments= declarationNode.fragments(); 101 Iterator iter= fragments.iterator(); 102 103 ListRewrite blockRewrite= rewrite.getListRewrite(declarationNode.getParent(), Block.STATEMENTS_PROPERTY); 104 105 VariableDeclarationFragment lastFragment= (VariableDeclarationFragment)iter.next(); 106 ASTNode lastStatement= declarationNode; 107 108 if (fragmentsToChange.contains(lastFragment)) { 109 ModifierRewrite modifierRewrite= ModifierRewrite.create(rewrite, declarationNode); 110 modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group); 111 } 112 113 ListRewrite fragmentsRewrite= null; 114 while (iter.hasNext()) { 115 VariableDeclarationFragment currentFragment= (VariableDeclarationFragment)iter.next(); 116 117 if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) { 118 119 VariableDeclarationStatement newStatement= ast.newVariableDeclarationStatement((VariableDeclarationFragment)rewrite.createMoveTarget(currentFragment)); 120 newStatement.setType((Type)rewrite.createCopyTarget(declarationNode.getType())); 121 122 ModifierRewrite modifierRewrite= ModifierRewrite.create(rewrite, newStatement); 123 if (fragmentsToChange.contains(currentFragment)) { 124 modifierRewrite.copyAllAnnotations(declarationNode, group); 125 modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group); 126 } else { 127 modifierRewrite.copyAllModifiers(declarationNode, group); 128 } 129 blockRewrite.insertAfter(newStatement, lastStatement, group); 130 131 fragmentsRewrite= rewrite.getListRewrite(newStatement, VariableDeclarationStatement.FRAGMENTS_PROPERTY); 132 lastStatement= newStatement; 133 } else if (fragmentsRewrite != null) { 134 ASTNode fragment0= rewrite.createMoveTarget(currentFragment); 135 fragmentsRewrite.insertLast(fragment0, group); 136 } 137 lastFragment= currentFragment; 138 } 139 } 140 } 141 | Popular Tags |