1 11 package org.eclipse.jdt.internal.corext.dom; 12 13 import java.util.List ; 14 15 import org.eclipse.jdt.core.dom.*; 16 17 import org.eclipse.jdt.internal.corext.Assert; 18 19 public class ASTRewriteClear extends ASTVisitor { 20 21 private OldASTRewrite fRewrite; 22 23 public ASTRewriteClear(OldASTRewrite rewrite) { 24 fRewrite= rewrite; 25 } 26 27 private void clearList(List list) { 28 for (int i= list.size() - 1; i >= 0 ; i--) { 29 ASTNode element= (ASTNode) list.get(i); 30 if (fRewrite.isInserted(element)) { 31 list.remove(i); 32 } else { 33 if (fRewrite.isCollapsed(element)) { 34 List collapsed= ((Block) element).statements(); 35 list.remove(i); 36 for (int k= collapsed.size() - 1; k >= 0 ; k--) { 37 Object curr= collapsed.remove(k); 38 list.add(i, curr); 39 } 40 } 41 } 42 } 43 } 44 45 private boolean isInserted(ASTNode node) { 46 return node != null && fRewrite.isInserted(node); 47 } 48 49 50 53 public boolean visit(AnonymousClassDeclaration node) { 54 clearList(node.bodyDeclarations()); 55 return true; 56 } 57 58 61 public boolean visit(ArrayCreation node) { 62 clearList(node.dimensions()); 63 if (isInserted(node.getInitializer())) { 64 node.setInitializer(null); 65 } 66 return true; 67 } 68 69 72 public boolean visit(ArrayInitializer node) { 73 clearList(node.expressions()); 74 return true; 75 } 76 77 78 81 public boolean visit(AssertStatement node) { 82 node.getExpression().accept(this); 83 if (isInserted(node.getMessage())) { 84 node.setMessage(null); 85 } 86 return true; 87 } 88 89 92 public boolean visit(Block node) { 93 clearList(node.statements()); 94 return true; 95 } 96 97 100 public boolean visit(BreakStatement node) { 101 if (isInserted(node.getLabel())) { 102 node.setLabel(null); 103 } 104 return true; 105 } 106 107 110 public boolean visit(ClassInstanceCreation node) { 111 if (isInserted(node.getExpression())) { 112 node.setExpression(null); 113 } 114 clearList(node.arguments()); 115 if (isInserted(node.getAnonymousClassDeclaration())) { 116 node.setAnonymousClassDeclaration(null); 117 } 118 return true; 119 } 120 121 124 public boolean visit(CompilationUnit node) { 125 if (isInserted(node.getPackage())) { 126 node.setPackage(null); 127 } 128 clearList(node.imports()); 129 clearList(node.types()); 130 return true; 131 } 132 133 134 137 public boolean visit(ConstructorInvocation node) { 138 clearList(node.arguments()); 139 return true; 140 } 141 142 145 public boolean visit(ContinueStatement node) { 146 if (isInserted(node.getLabel())) { 147 node.setLabel(null); 148 } 149 return true; 150 } 151 152 155 public boolean visit(FieldDeclaration node) { 156 if (isInserted(node.getJavadoc())) { 157 node.setJavadoc(null); 158 } 159 clearList(node.fragments()); 160 return true; 161 } 162 163 166 public boolean visit(ForStatement node) { 167 clearList(node.initializers()); 168 clearList(node.updaters()); 169 if (isInserted(node.getExpression())) { 170 node.setExpression(null); 171 } 172 return true; 173 } 174 175 178 public boolean visit(IfStatement node) { 179 if (isInserted(node.getElseStatement())) { 180 node.setElseStatement(null); 181 } 182 return true; 183 } 184 185 188 public boolean visit(InfixExpression node) { 189 clearList(node.extendedOperands()); 190 return true; 191 } 192 193 196 public boolean visit(Initializer node) { 197 if (isInserted(node.getJavadoc())) { 198 node.setJavadoc(null); 199 } 200 return true; 201 } 202 203 206 public boolean visit(MethodDeclaration node) { 207 if (isInserted(node.getJavadoc())) { 208 node.setJavadoc(null); 209 } 210 if (isInserted(node.getReturnType())) { 211 node.setReturnType(node.getAST().newPrimitiveType(PrimitiveType.VOID)); 212 } 213 clearList(node.parameters()); 214 clearList(node.thrownExceptions()); 215 if (isInserted(node.getBody())) { 216 node.setBody(null); 217 } 218 return true; 219 } 220 221 224 public boolean visit(MethodInvocation node) { 225 if (isInserted(node.getExpression())) { 226 node.setExpression(null); 227 } 228 clearList(node.arguments()); 229 return true; 230 } 231 232 235 public boolean visit(ReturnStatement node) { 236 if (isInserted(node.getExpression())) { 237 node.setExpression(null); 238 } 239 return true; 240 } 241 242 245 public boolean visit(SingleVariableDeclaration node) { 246 if (isInserted(node.getInitializer())) { 247 node.setInitializer(null); 248 } 249 return true; 250 } 251 252 255 public boolean visit(SuperConstructorInvocation node) { 256 if (isInserted(node.getExpression())) { 257 node.setExpression(null); 258 } 259 clearList(node.arguments()); 260 return true; 261 } 262 263 266 public boolean visit(SuperFieldAccess node) { 267 if (isInserted(node.getQualifier())) { 268 node.setQualifier(null); 269 } 270 return true; 271 } 272 273 276 public boolean visit(SuperMethodInvocation node) { 277 if (isInserted(node.getQualifier())) { 278 node.setQualifier(null); 279 } 280 clearList(node.arguments()); 281 return true; 282 } 283 284 287 public boolean visit(SwitchCase node) { 288 if (isInserted(node.getExpression())) { 289 node.setExpression(null); 290 } 291 return true; 292 } 293 294 297 public boolean visit(SwitchStatement node) { 298 clearList(node.statements()); 299 return true; 300 } 301 302 305 public boolean visit(ThisExpression node) { 306 if (isInserted(node.getQualifier())) { 307 node.setQualifier(null); 308 } 309 return true; 310 } 311 312 315 public boolean visit(TryStatement node) { 316 clearList(node.catchClauses()); 317 if (isInserted(node.getFinally())) { 318 node.setFinally(null); 319 } 320 return true; 321 } 322 323 326 public boolean visit(TypeDeclaration node) { 327 if (isInserted(node.getJavadoc())) { 328 node.setJavadoc(null); 329 } 330 if (isInserted(node.getSuperclass())) { 331 node.setSuperclass(null); 332 } 333 clearList(node.superInterfaces()); 334 clearList(node.bodyDeclarations()); 335 return true; 336 } 337 338 341 public boolean visit(VariableDeclarationExpression node) { 342 clearList(node.fragments()); 343 return true; 344 } 345 346 349 public boolean visit(VariableDeclarationFragment node) { 350 if (isInserted(node.getInitializer())) { 351 node.setInitializer(null); 352 } 353 return true; 354 } 355 356 359 public boolean visit(VariableDeclarationStatement node) { 360 clearList(node.fragments()); 361 return true; 362 } 363 364 367 public void postVisit(ASTNode node) { 368 if (fRewrite.isInserted(node)) { 369 Assert.isTrue(false, "Inserted node not removed " + node + ", parent: " + node.getParent()); } 371 } 372 373 } 374 | Popular Tags |