1 /******************************************************************************* 2 * Copyright (c) 2000, 2016 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.core.tests.rewrite.describing; 15 16 import java.util.Arrays; 17 import java.util.Comparator; 18 import java.util.Iterator; 19 import java.util.List; 20 import java.util.Map; 21 22 import junit.framework.Test; 23 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.IPackageFragment; 26 import org.eclipse.jdt.core.JavaCore; 27 import org.eclipse.jdt.core.dom.*; 28 import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword; 29 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 30 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 31 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 32 33 @SuppressWarnings({"rawtypes", "unchecked"}) 34 public class ASTRewritingMethodDeclTest extends ASTRewritingTest { 35 36 /** @deprecated using deprecated code */ 37 private static final SimplePropertyDescriptor INTERNAL_FIELD_MODIFIERS_PROPERTY = FieldDeclaration.MODIFIERS_PROPERTY; 38 /** @deprecated using deprecated code */ 39 private static final SimplePropertyDescriptor INTERNAL_INITIALIZER_MODIFIERS_PROPERTY = Initializer.MODIFIERS_PROPERTY; 40 /** @deprecated using deprecated code */ 41 private static final SimplePropertyDescriptor INTERNAL_METHOD_MODIFIERS_PROPERTY = MethodDeclaration.MODIFIERS_PROPERTY; 42 /** @deprecated using deprecated code */ 43 private static final ChildPropertyDescriptor INTERNAL_METHOD_RETURN_TYPE_PROPERTY = MethodDeclaration.RETURN_TYPE_PROPERTY; 44 /** @deprecated using deprecated code */ 45 private static final SimplePropertyDescriptor INTERNAL_METHOD_EXTRA_DIMENSIONS_PROPERTY = MethodDeclaration.EXTRA_DIMENSIONS_PROPERTY; 46 /** @deprecated using deprecated code */ 47 private static final ChildListPropertyDescriptor INTERNAL_METHOD_THROWN_EXCEPTIONS_PROPERTY = MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY; 48 ASTRewritingMethodDeclTest(String name)49 public ASTRewritingMethodDeclTest(String name) { 50 super(name); 51 } 52 ASTRewritingMethodDeclTest(String name, int apiLevel)53 public ASTRewritingMethodDeclTest(String name, int apiLevel) { 54 super(name, apiLevel); 55 } 56 suite()57 public static Test suite() { 58 return createSuite(ASTRewritingMethodDeclTest.class); 59 } 60 61 /** @deprecated using deprecated code */ getReturnType(MethodDeclaration methodDecl)62 private Type getReturnType(MethodDeclaration methodDecl) { 63 return this.apiLevel < AST.JLS3 ? methodDecl.getReturnType() : methodDecl.getReturnType2(); 64 } 65 66 /** @deprecated using deprecated code */ getMethodReturnTypeProperty(AST ast)67 private ChildPropertyDescriptor getMethodReturnTypeProperty(AST ast) { 68 return ast.apiLevel() < AST.JLS3 ? INTERNAL_METHOD_RETURN_TYPE_PROPERTY : MethodDeclaration.RETURN_TYPE2_PROPERTY; 69 } 70 71 /** @deprecated using deprecated code */ createNewExceptionType(AST ast, String name)72 private ASTNode createNewExceptionType(AST ast, String name) { 73 return ast.apiLevel() < AST.JLS8 ? ast.newSimpleName(name) : (ASTNode) ast.newSimpleType(ast.newSimpleName(name)); 74 } 75 76 /** @deprecated using deprecated code */ getThrownExceptions(MethodDeclaration methodDecl)77 private List getThrownExceptions(MethodDeclaration methodDecl) { 78 return this.apiLevel < AST.JLS8 ? methodDecl.thrownExceptions() : methodDecl.thrownExceptionTypes(); 79 } 80 81 /** @deprecated using deprecated code */ getMethodThrownExceptionsProperty(AST ast)82 private ChildListPropertyDescriptor getMethodThrownExceptionsProperty(AST ast) { 83 return ast.apiLevel() < AST.JLS8 ? INTERNAL_METHOD_THROWN_EXCEPTIONS_PROPERTY : MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY; 84 } 85 86 /** @deprecated using deprecated code */ setModifiers(ASTRewrite rewrite, MethodDeclaration methodDecl, int newModifiers)87 private void setModifiers(ASTRewrite rewrite, MethodDeclaration methodDecl, int newModifiers) { 88 if (this.apiLevel < AST.JLS3) { 89 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(newModifiers), null); 90 } else { 91 ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 92 for (Iterator iter= listRewrite.getOriginalList().iterator(); iter.hasNext(); ) { 93 ASTNode modifier= (ASTNode) iter.next(); 94 listRewrite.remove(modifier, null); 95 } 96 List newModifierNodes = methodDecl.getAST().newModifiers(newModifiers); 97 for (Iterator iter= newModifierNodes.iterator(); iter.hasNext(); ) { 98 Modifier modifier= (Modifier) iter.next(); 99 listRewrite.insertLast(modifier, null); 100 } 101 } 102 } 103 104 /** @deprecated using deprecated code */ setExtraDimensions(ASTRewrite rewrite, MethodDeclaration methodDecl, int extraDimensions)105 private void setExtraDimensions(ASTRewrite rewrite, MethodDeclaration methodDecl, int extraDimensions) { 106 if (this.apiLevel < AST.JLS8) { 107 rewrite.set(methodDecl, INTERNAL_METHOD_EXTRA_DIMENSIONS_PROPERTY, Integer.valueOf(extraDimensions), null); 108 } else { 109 ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY); 110 for (Iterator iter= listRewrite.getOriginalList().iterator(); iter.hasNext(); ) { 111 ASTNode extraDimension= (ASTNode) iter.next(); 112 listRewrite.remove(extraDimension, null); 113 } 114 for (int i= 0; i < extraDimensions; i++) { 115 listRewrite.insertFirst(methodDecl.getAST().newDimension(), null); 116 } 117 } 118 } 119 testMethodDeclChanges()120 public void testMethodDeclChanges() throws Exception { 121 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 122 StringBuffer buf= new StringBuffer(); 123 buf.append("package test1;\n"); 124 buf.append("public abstract class E {\n"); 125 buf.append(" public E(int p1, int p2, int p3) {}\n"); 126 buf.append(" public void gee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 127 buf.append(" public void hee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 128 buf.append(" public void iee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 129 buf.append(" public void jee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 130 buf.append(" public abstract void kee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 131 buf.append(" public abstract void lee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 132 buf.append("}\n"); 133 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 134 135 CompilationUnit astRoot= createAST(cu); 136 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 137 AST ast= astRoot.getAST(); 138 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 139 140 { // convert constructor to method: insert return type 141 MethodDeclaration methodDecl= findMethodDeclaration(type, "E"); 142 143 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 144 145 // from constructor to method 146 rewrite.set(methodDecl, getMethodReturnTypeProperty(ast), newReturnType, null); 147 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 148 } 149 { // change return type 150 MethodDeclaration methodDecl= findMethodDeclaration(type, "gee"); 151 assertTrue("Has no return type: gee", getReturnType(methodDecl) != null); 152 153 Type returnType= getReturnType(methodDecl); 154 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 155 rewrite.replace(returnType, newReturnType, null); 156 } 157 { // remove return type 158 MethodDeclaration methodDecl= findMethodDeclaration(type, "hee"); 159 assertTrue("Has no return type: hee", getReturnType(methodDecl) != null); 160 161 // from method to constructor 162 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 163 } 164 { // rename method name 165 MethodDeclaration methodDecl= findMethodDeclaration(type, "iee"); 166 167 SimpleName name= methodDecl.getName(); 168 SimpleName newName= ast.newSimpleName("xii"); 169 170 rewrite.replace(name, newName, null); 171 } 172 { // rename first param & last throw statement 173 MethodDeclaration methodDecl= findMethodDeclaration(type, "jee"); 174 List parameters= methodDecl.parameters(); 175 assertTrue("must be 3 parameters", parameters.size() == 3); 176 SingleVariableDeclaration newParam= createNewParam(ast, "m"); 177 rewrite.replace((ASTNode) parameters.get(0), newParam, null); 178 179 List thrownExceptions= getThrownExceptions(methodDecl); 180 assertTrue("must be 2 thrown exceptions", thrownExceptions.size() == 2); 181 ASTNode newThrownException= createNewExceptionType(ast, "ArrayStoreException"); 182 rewrite.replace((ASTNode) thrownExceptions.get(1), newThrownException, null); 183 } 184 { // rename first and second param & rename first and last exception 185 MethodDeclaration methodDecl= findMethodDeclaration(type, "kee"); 186 List parameters= methodDecl.parameters(); 187 assertTrue("must be 3 parameters", parameters.size() == 3); 188 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 189 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 190 rewrite.replace((ASTNode) parameters.get(0), newParam1, null); 191 rewrite.replace((ASTNode) parameters.get(1), newParam2, null); 192 193 List thrownExceptions= getThrownExceptions(methodDecl); 194 assertTrue("must be 3 thrown exceptions", thrownExceptions.size() == 3); 195 ASTNode newThrownException1= createNewExceptionType(ast, "ArrayStoreException"); 196 ASTNode newThrownException2= createNewExceptionType(ast, "InterruptedException"); 197 rewrite.replace((ASTNode) thrownExceptions.get(0), newThrownException1, null); 198 rewrite.replace((ASTNode) thrownExceptions.get(2), newThrownException2, null); 199 } 200 { // rename all params & rename second exception 201 MethodDeclaration methodDecl= findMethodDeclaration(type, "lee"); 202 List parameters= methodDecl.parameters(); 203 assertTrue("must be 3 parameters", parameters.size() == 3); 204 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 205 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 206 SingleVariableDeclaration newParam3= createNewParam(ast, "m3"); 207 rewrite.replace((ASTNode) parameters.get(0), newParam1, null); 208 rewrite.replace((ASTNode) parameters.get(1), newParam2, null); 209 rewrite.replace((ASTNode) parameters.get(2), newParam3, null); 210 211 List thrownExceptions= getThrownExceptions(methodDecl); 212 assertTrue("must be 3 thrown exceptions", thrownExceptions.size() == 3); 213 ASTNode newThrownException= createNewExceptionType(ast, "ArrayStoreException"); 214 rewrite.replace((ASTNode) thrownExceptions.get(1), newThrownException, null); 215 } 216 217 218 String preview= evaluateRewrite(cu, rewrite); 219 220 buf= new StringBuffer(); 221 buf.append("package test1;\n"); 222 buf.append("public abstract class E {\n"); 223 buf.append(" public float E(int p1, int p2, int p3) {}\n"); 224 buf.append(" public float gee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 225 buf.append(" public hee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 226 buf.append(" public void xii(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 227 buf.append(" public void jee(float m, int p2, int p3) throws IllegalArgumentException, ArrayStoreException {}\n"); 228 buf.append(" public abstract void kee(float m1, float m2, int p3) throws ArrayStoreException, IllegalAccessException, InterruptedException;\n"); 229 buf.append(" public abstract void lee(float m1, float m2, float m3) throws IllegalArgumentException, ArrayStoreException, SecurityException;\n"); 230 buf.append("}\n"); 231 232 assertEqualString(preview, buf.toString()); 233 } 234 testMethodTypeParameterAdds_since_3()235 public void testMethodTypeParameterAdds_since_3() throws Exception { 236 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 237 StringBuffer buf= new StringBuffer(); 238 buf.append("package test1;\n"); 239 buf.append("public abstract class E {\n"); 240 buf.append(" /**\n"); 241 buf.append(" *\n"); 242 buf.append(" */\n"); 243 buf.append(" E(int p1) {}\n"); 244 buf.append(" E(int p1, int p2) {}\n"); 245 buf.append(" public E(int p1, byte p2) {}\n"); 246 buf.append(" /**\n"); 247 buf.append(" *\n"); 248 buf.append(" */\n"); 249 buf.append(" void gee(int p1) {}\n"); 250 buf.append(" void hee(int p1, int p2) {}\n"); 251 buf.append(" public void hee(int p1, byte p2) {}\n"); 252 buf.append("}\n"); 253 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 254 255 CompilationUnit astRoot= createAST(cu); 256 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 257 AST ast= astRoot.getAST(); 258 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 259 260 MethodDeclaration[] methods= type.getMethods(); 261 for (int i= 0; i < methods.length; i++) { 262 263 // add type parameter 264 MethodDeclaration methodDecl= methods[i]; 265 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.TYPE_PARAMETERS_PROPERTY); 266 TypeParameter typeParameter= ast.newTypeParameter(); 267 typeParameter.setName(ast.newSimpleName("X")); 268 listRewrite.insertFirst(typeParameter, null); 269 } 270 String preview= evaluateRewrite(cu, rewrite); 271 272 buf= new StringBuffer(); 273 buf.append("package test1;\n"); 274 buf.append("public abstract class E {\n"); 275 buf.append(" /**\n"); 276 buf.append(" *\n"); 277 buf.append(" */\n"); 278 buf.append(" <X> E(int p1) {}\n"); 279 buf.append(" <X> E(int p1, int p2) {}\n"); 280 buf.append(" public <X> E(int p1, byte p2) {}\n"); 281 buf.append(" /**\n"); 282 buf.append(" *\n"); 283 buf.append(" */\n"); 284 buf.append(" <X> void gee(int p1) {}\n"); 285 buf.append(" <X> void hee(int p1, int p2) {}\n"); 286 buf.append(" public <X> void hee(int p1, byte p2) {}\n"); 287 buf.append("}\n"); 288 assertEqualString(preview, buf.toString()); 289 } 290 testMethodTypeParameterRemoves_since_3()291 public void testMethodTypeParameterRemoves_since_3() throws Exception { 292 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 293 StringBuffer buf= new StringBuffer(); 294 buf.append("package test1;\n"); 295 buf.append("public abstract class E {\n"); 296 buf.append(" /**\n"); 297 buf.append(" *\n"); 298 buf.append(" */\n"); 299 buf.append(" <X> E(int p1) {}\n"); 300 buf.append(" <X> E(int p1, int p2) {}\n"); 301 buf.append(" public <X> E(int p1, byte p2) {}\n"); 302 buf.append(" /**\n"); 303 buf.append(" *\n"); 304 buf.append(" */\n"); 305 buf.append(" <X> void gee(int p1) {}\n"); 306 buf.append(" <X> void hee(int p1, int p2) {}\n"); 307 buf.append(" public <X> void hee(int p1, byte p2) {}\n"); 308 buf.append(" public<X>void hee(int p1, byte p2) {}\n"); 309 buf.append("}\n"); 310 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 311 312 CompilationUnit astRoot= createAST(cu); 313 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 314 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 315 316 MethodDeclaration[] methods= type.getMethods(); 317 for (int i= 0; i < methods.length; i++) { 318 319 // add type parameter 320 MethodDeclaration methodDecl= methods[i]; 321 rewrite.remove((ASTNode) methodDecl.typeParameters().get(0), null); 322 323 } 324 String preview= evaluateRewrite(cu, rewrite); 325 326 buf= new StringBuffer(); 327 buf.append("package test1;\n"); 328 buf.append("public abstract class E {\n"); 329 buf.append(" /**\n"); 330 buf.append(" *\n"); 331 buf.append(" */\n"); 332 buf.append(" E(int p1) {}\n"); 333 buf.append(" E(int p1, int p2) {}\n"); 334 buf.append(" public E(int p1, byte p2) {}\n"); 335 buf.append(" /**\n"); 336 buf.append(" *\n"); 337 buf.append(" */\n"); 338 buf.append(" void gee(int p1) {}\n"); 339 buf.append(" void hee(int p1, int p2) {}\n"); 340 buf.append(" public void hee(int p1, byte p2) {}\n"); 341 buf.append(" public void hee(int p1, byte p2) {}\n"); 342 buf.append("}\n"); 343 assertEqualString(preview, buf.toString()); 344 } 345 346 347 testMethodReturnTypeChanges_only_2()348 public void testMethodReturnTypeChanges_only_2() throws Exception { 349 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 350 StringBuffer buf= new StringBuffer(); 351 buf.append("package test1;\n"); 352 buf.append("public abstract class E {\n"); 353 buf.append(" public E() {}\n"); 354 buf.append(" E(int i) {}\n"); 355 buf.append(" /** javadoc comment */\n"); 356 buf.append(" /* comment */ E(int i, int j) {}\n"); 357 buf.append(" public void gee1() {}\n"); 358 buf.append(" void gee2() {}\n"); 359 buf.append(" /** javadoc comment */\n"); 360 buf.append(" /* comment */ void gee3() {}\n"); 361 buf.append("}\n"); 362 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 363 364 CompilationUnit astRoot= createAST(cu); 365 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 366 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 367 List list= type.bodyDeclarations(); 368 369 { // insert return type, add second modifier 370 MethodDeclaration methodDecl= (MethodDeclaration) list.get(0); 371 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(Modifier.PUBLIC | Modifier.FINAL), null); 372 373 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 374 375 // from constructor to method 376 rewrite.set(methodDecl, INTERNAL_METHOD_RETURN_TYPE_PROPERTY, newReturnType, null); 377 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 378 379 } 380 { // insert return type, add (first) modifier 381 MethodDeclaration methodDecl= (MethodDeclaration) list.get(1); 382 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(Modifier.FINAL), null); 383 384 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 385 386 // from constructor to method 387 rewrite.set(methodDecl, INTERNAL_METHOD_RETURN_TYPE_PROPERTY, newReturnType, null); 388 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 389 } 390 391 { // insert return type, add second modifier with comments 392 MethodDeclaration methodDecl= (MethodDeclaration) list.get(2); 393 394 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(Modifier.FINAL), null); 395 396 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 397 398 // from constructor to method 399 rewrite.set(methodDecl, INTERNAL_METHOD_RETURN_TYPE_PROPERTY, newReturnType, null); 400 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 401 402 } 403 404 { // add second modifier 405 MethodDeclaration methodDecl= (MethodDeclaration) list.get(3); 406 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(Modifier.PUBLIC | Modifier.FINAL), null); 407 408 // from method to constructor 409 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 410 411 } 412 { // add (first) modifier 413 MethodDeclaration methodDecl= (MethodDeclaration) list.get(4); 414 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(Modifier.FINAL), null); 415 416 // from method to constructor 417 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 418 } 419 420 { // add second modifier with comments 421 MethodDeclaration methodDecl= (MethodDeclaration) list.get(5); 422 423 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(Modifier.FINAL), null); 424 425 // from method to constructor 426 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 427 } 428 429 String preview= evaluateRewrite(cu, rewrite); 430 431 buf= new StringBuffer(); 432 buf.append("package test1;\n"); 433 buf.append("public abstract class E {\n"); 434 buf.append(" public final float E() {}\n"); 435 buf.append(" final float E(int i) {}\n"); 436 buf.append(" /** javadoc comment */\n"); 437 buf.append(" final /* comment */ float E(int i, int j) {}\n"); 438 buf.append(" public final gee1() {}\n"); 439 buf.append(" final gee2() {}\n"); 440 buf.append(" /** javadoc comment */\n"); 441 buf.append(" final gee3() {}\n"); 442 buf.append("}\n"); 443 444 assertEqualString(preview, buf.toString()); 445 446 } 447 testMethodReturnTypeChanges2_only_2()448 public void testMethodReturnTypeChanges2_only_2() throws Exception { 449 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 450 StringBuffer buf= new StringBuffer(); 451 buf.append("package test1;\n"); 452 buf.append("public abstract class E {\n"); 453 buf.append(" public synchronized E() {}\n"); 454 buf.append(" public E(int i) {}\n"); 455 buf.append(" /** javadoc comment */\n"); 456 buf.append(" public /* comment */ E(int i, int j) {}\n"); 457 buf.append(" public synchronized void gee1() {}\n"); 458 buf.append(" public void gee2() {}\n"); 459 buf.append(" /** javadoc comment */\n"); 460 buf.append(" public /* comment */ void gee3() {}\n"); 461 buf.append("}\n"); 462 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 463 464 CompilationUnit astRoot= createAST(cu); 465 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 466 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 467 List list= type.bodyDeclarations(); 468 469 { // insert return type, remove second modifier 470 MethodDeclaration methodDecl= (MethodDeclaration) list.get(0); 471 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(Modifier.PUBLIC), null); 472 473 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 474 475 // from constructor to method 476 rewrite.set(methodDecl, INTERNAL_METHOD_RETURN_TYPE_PROPERTY, newReturnType, null); 477 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 478 479 } 480 { // insert return type, remove (only) modifier 481 MethodDeclaration methodDecl= (MethodDeclaration) list.get(1); 482 setModifiers(rewrite, methodDecl, 0); 483 484 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 485 486 // from constructor to method 487 rewrite.set(methodDecl, INTERNAL_METHOD_RETURN_TYPE_PROPERTY, newReturnType, null); 488 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 489 } 490 491 { // insert return type, remove modifier with comments 492 MethodDeclaration methodDecl= (MethodDeclaration) list.get(2); 493 494 setModifiers(rewrite, methodDecl, 0); 495 496 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 497 498 // from constructor to method 499 rewrite.set(methodDecl, INTERNAL_METHOD_RETURN_TYPE_PROPERTY, newReturnType, null); 500 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 501 502 } 503 504 { // remove second modifier 505 MethodDeclaration methodDecl= (MethodDeclaration) list.get(3); 506 rewrite.set(methodDecl, INTERNAL_METHOD_MODIFIERS_PROPERTY, Integer.valueOf(Modifier.PUBLIC), null); 507 508 // from method to constructor 509 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 510 511 } 512 { // remove (only) modifier 513 MethodDeclaration methodDecl= (MethodDeclaration) list.get(4); 514 setModifiers(rewrite, methodDecl, 0); 515 516 // from method to constructor 517 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 518 } 519 520 { // remove return type, remove modifier with comments 521 MethodDeclaration methodDecl= (MethodDeclaration) list.get(5); 522 523 setModifiers(rewrite, methodDecl, 0); 524 525 // from method to constructor 526 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 527 } 528 529 String preview= evaluateRewrite(cu, rewrite); 530 531 buf= new StringBuffer(); 532 buf.append("package test1;\n"); 533 buf.append("public abstract class E {\n"); 534 buf.append(" public float E() {}\n"); 535 buf.append(" float E(int i) {}\n"); 536 buf.append(" /** javadoc comment */\n"); 537 buf.append(" /* comment */ float E(int i, int j) {}\n"); 538 buf.append(" public gee1() {}\n"); 539 buf.append(" gee2() {}\n"); 540 buf.append(" /** javadoc comment */\n"); 541 buf.append(" gee3() {}\n"); 542 buf.append("}\n"); 543 544 assertEqualString(preview, buf.toString()); 545 546 } 547 548 549 testMethodReturnTypeChanges_since_3()550 public void testMethodReturnTypeChanges_since_3() throws Exception { 551 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 552 StringBuffer buf= new StringBuffer(); 553 buf.append("package test1;\n"); 554 buf.append("public abstract class E {\n"); 555 buf.append(" public E() {}\n"); 556 buf.append(" E(int i) {}\n"); 557 buf.append(" /** javadoc comment */\n"); 558 buf.append(" /* comment */ E(int i, int j) {}\n"); 559 buf.append(" public void gee1() {}\n"); 560 buf.append(" void gee2() {}\n"); 561 buf.append(" /** javadoc comment */\n"); 562 buf.append(" /* comment */ void gee3() {}\n"); 563 buf.append("}\n"); 564 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 565 566 CompilationUnit astRoot= createAST(cu); 567 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 568 AST ast= astRoot.getAST(); 569 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 570 List list= type.bodyDeclarations(); 571 572 { // insert return type, add second modifier 573 MethodDeclaration methodDecl= (MethodDeclaration) list.get(0); 574 575 ListRewrite modifiers= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 576 modifiers.insertLast(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 577 578 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 579 580 // from constructor to method 581 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null); 582 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 583 584 } 585 { // insert return type, add (first) modifier 586 MethodDeclaration methodDecl= (MethodDeclaration) list.get(1); 587 588 ListRewrite modifiers= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 589 modifiers.insertLast(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 590 591 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 592 593 // from constructor to method 594 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null); 595 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 596 } 597 598 { // insert return type, add second modifier with comments 599 MethodDeclaration methodDecl= (MethodDeclaration) list.get(2); 600 601 ListRewrite modifiers= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 602 modifiers.insertLast(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 603 604 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 605 606 // from constructor to method 607 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null); 608 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 609 610 } 611 612 { // remove return type, add second modifier 613 MethodDeclaration methodDecl= (MethodDeclaration) list.get(3); 614 615 ListRewrite modifiers= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 616 modifiers.insertLast(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 617 618 // from method to constructor 619 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, null, null); 620 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 621 622 } 623 { // remove return type, add (first) modifier 624 MethodDeclaration methodDecl= (MethodDeclaration) list.get(4); 625 626 ListRewrite modifiers= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 627 modifiers.insertLast(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 628 629 // from method to constructor 630 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, null, null); 631 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 632 } 633 634 { // remove return type, add second modifier with comments 635 MethodDeclaration methodDecl= (MethodDeclaration) list.get(5); 636 637 ListRewrite modifiers= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 638 modifiers.insertLast(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 639 640 // from method to constructor 641 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, null, null); 642 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 643 } 644 645 String preview= evaluateRewrite(cu, rewrite); 646 647 buf= new StringBuffer(); 648 buf.append("package test1;\n"); 649 buf.append("public abstract class E {\n"); 650 buf.append(" public final float E() {}\n"); 651 buf.append(" final float E(int i) {}\n"); 652 buf.append(" /** javadoc comment */\n"); 653 buf.append(" final /* comment */ float E(int i, int j) {}\n"); 654 buf.append(" public final gee1() {}\n"); 655 buf.append(" final gee2() {}\n"); 656 buf.append(" /** javadoc comment */\n"); 657 buf.append(" final gee3() {}\n"); 658 buf.append("}\n"); 659 660 assertEqualString(preview, buf.toString()); 661 662 } 663 testMethodReturnTypeChanges2_since_3()664 public void testMethodReturnTypeChanges2_since_3() throws Exception { 665 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 666 StringBuffer buf= new StringBuffer(); 667 buf.append("package test1;\n"); 668 buf.append("public abstract class E {\n"); 669 buf.append(" public synchronized E() {}\n"); 670 buf.append(" public E(int i) {}\n"); 671 buf.append(" /** javadoc comment */\n"); 672 buf.append(" public /* comment */ E(int i, int j) {}\n"); 673 buf.append(" public synchronized void gee1() {}\n"); 674 buf.append(" public void gee2() {}\n"); 675 buf.append(" /** javadoc comment */\n"); 676 buf.append(" public /* comment */ void gee3() {}\n"); 677 buf.append("}\n"); 678 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 679 680 CompilationUnit astRoot= createAST(cu); 681 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 682 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 683 List list= type.bodyDeclarations(); 684 685 { // insert return type, remove second modifier 686 MethodDeclaration methodDecl= (MethodDeclaration) list.get(0); 687 rewrite.remove((ASTNode) methodDecl.modifiers().get(1), null); 688 689 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 690 691 // from constructor to method 692 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null); 693 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 694 695 } 696 { // insert return type, remove (only) modifier 697 MethodDeclaration methodDecl= (MethodDeclaration) list.get(1); 698 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 699 700 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 701 702 // from constructor to method 703 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null); 704 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 705 } 706 707 { // insert return type, remove modifier with comments 708 MethodDeclaration methodDecl= (MethodDeclaration) list.get(2); 709 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 710 Type newReturnType= astRoot.getAST().newPrimitiveType(PrimitiveType.FLOAT); 711 712 // from constructor to method 713 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null); 714 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null); 715 } 716 717 { // remove return type, remove second modifier 718 MethodDeclaration methodDecl= (MethodDeclaration) list.get(3); 719 rewrite.remove((ASTNode) methodDecl.modifiers().get(1), null); 720 721 // from method to constructor 722 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, null, null); 723 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 724 725 } 726 { // remove return type, remove (only) modifier 727 MethodDeclaration methodDecl= (MethodDeclaration) list.get(4); 728 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 729 730 // from method to constructor 731 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, null, null); 732 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 733 } 734 735 { // remove return type, remove modifier with comments 736 MethodDeclaration methodDecl= (MethodDeclaration) list.get(5); 737 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 738 739 // from method to constructor 740 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, null, null); 741 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 742 } 743 744 String preview= evaluateRewrite(cu, rewrite); 745 746 buf= new StringBuffer(); 747 buf.append("package test1;\n"); 748 buf.append("public abstract class E {\n"); 749 buf.append(" public float E() {}\n"); 750 buf.append(" float E(int i) {}\n"); 751 buf.append(" /** javadoc comment */\n"); 752 buf.append(" /* comment */ float E(int i, int j) {}\n"); 753 buf.append(" public gee1() {}\n"); 754 buf.append(" gee2() {}\n"); 755 buf.append(" /** javadoc comment */\n"); 756 buf.append(" gee3() {}\n"); 757 buf.append("}\n"); 758 759 assertEqualString(preview, buf.toString()); 760 761 } 762 763 764 765 testListRemoves()766 public void testListRemoves() throws Exception { 767 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 768 StringBuffer buf= new StringBuffer(); 769 buf.append("package test1;\n"); 770 buf.append("public abstract class E {\n"); 771 buf.append(" public E(int p1, int p2, int p3) {}\n"); 772 buf.append(" public void gee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 773 buf.append(" public void hee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 774 buf.append(" public void iee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 775 buf.append(" public void jee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 776 buf.append(" public abstract void kee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 777 buf.append(" public abstract void lee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 778 buf.append("}\n"); 779 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 780 781 CompilationUnit astRoot= createAST(cu); 782 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 783 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 784 785 { // delete first param 786 MethodDeclaration methodDecl= findMethodDeclaration(type, "E"); 787 List parameters= methodDecl.parameters(); 788 assertTrue("must be 3 parameters", parameters.size() == 3); 789 rewrite.remove((ASTNode) parameters.get(0), null); 790 } 791 { // delete second param & remove exception & remove public 792 MethodDeclaration methodDecl= findMethodDeclaration(type, "gee"); 793 794 // change flags 795 setModifiers(rewrite, methodDecl, 0); 796 797 List parameters= methodDecl.parameters(); 798 assertTrue("must be 3 parameters", parameters.size() == 3); 799 rewrite.remove((ASTNode) parameters.get(1), null); 800 801 List thrownExceptions= getThrownExceptions(methodDecl); 802 assertTrue("must be 1 thrown exceptions", thrownExceptions.size() == 1); 803 rewrite.remove((ASTNode) thrownExceptions.get(0), null); 804 } 805 { // delete last param 806 MethodDeclaration methodDecl= findMethodDeclaration(type, "hee"); 807 List parameters= methodDecl.parameters(); 808 assertTrue("must be 3 parameters", parameters.size() == 3); 809 rewrite.remove((ASTNode) parameters.get(2), null); 810 } 811 { // delete first and second param & remove first exception 812 MethodDeclaration methodDecl= findMethodDeclaration(type, "iee"); 813 List parameters= methodDecl.parameters(); 814 assertTrue("must be 3 parameters", parameters.size() == 3); 815 rewrite.remove((ASTNode) parameters.get(0), null); 816 rewrite.remove((ASTNode) parameters.get(1), null); 817 818 List thrownExceptions= getThrownExceptions(methodDecl); 819 assertTrue("must be 2 thrown exceptions", thrownExceptions.size() == 2); 820 rewrite.remove((ASTNode) thrownExceptions.get(0), null); 821 } 822 { // delete first and last param & remove second 823 MethodDeclaration methodDecl= findMethodDeclaration(type, "jee"); 824 List parameters= methodDecl.parameters(); 825 assertTrue("must be 3 parameters", parameters.size() == 3); 826 rewrite.remove((ASTNode) parameters.get(0), null); 827 rewrite.remove((ASTNode) parameters.get(2), null); 828 829 List thrownExceptions= getThrownExceptions(methodDecl); 830 assertTrue("must be 2 thrown exceptions", thrownExceptions.size() == 2); 831 rewrite.remove((ASTNode) thrownExceptions.get(1), null); 832 } 833 { // delete second and last param & remove first exception 834 MethodDeclaration methodDecl= findMethodDeclaration(type, "kee"); 835 List parameters= methodDecl.parameters(); 836 assertTrue("must be 3 parameters", parameters.size() == 3); 837 rewrite.remove((ASTNode) parameters.get(1), null); 838 rewrite.remove((ASTNode) parameters.get(2), null); 839 840 List thrownExceptions= getThrownExceptions(methodDecl); 841 assertTrue("must be 3 thrown exceptions", thrownExceptions.size() == 3); 842 rewrite.remove((ASTNode) thrownExceptions.get(1), null); 843 } 844 { // delete all params & remove first and last exception 845 MethodDeclaration methodDecl= findMethodDeclaration(type, "lee"); 846 List parameters= methodDecl.parameters(); 847 assertTrue("must be 3 parameters", parameters.size() == 3); 848 rewrite.remove((ASTNode) parameters.get(0), null); 849 rewrite.remove((ASTNode) parameters.get(1), null); 850 rewrite.remove((ASTNode) parameters.get(2), null); 851 852 List thrownExceptions= getThrownExceptions(methodDecl); 853 assertTrue("must be 3 thrown exceptions", thrownExceptions.size() == 3); 854 rewrite.remove((ASTNode) thrownExceptions.get(0), null); 855 rewrite.remove((ASTNode) thrownExceptions.get(2), null); 856 } 857 858 859 String preview= evaluateRewrite(cu, rewrite); 860 861 buf= new StringBuffer(); 862 buf.append("package test1;\n"); 863 buf.append("public abstract class E {\n"); 864 buf.append(" public E(int p2, int p3) {}\n"); 865 buf.append(" void gee(int p1, int p3) {}\n"); 866 buf.append(" public void hee(int p1, int p2) throws IllegalArgumentException {}\n"); 867 buf.append(" public void iee(int p3) throws IllegalAccessException {}\n"); 868 buf.append(" public void jee(int p2) throws IllegalArgumentException {}\n"); 869 buf.append(" public abstract void kee(int p1) throws IllegalArgumentException, SecurityException;\n"); 870 buf.append(" public abstract void lee() throws IllegalAccessException;\n"); 871 buf.append("}\n"); 872 873 assertEqualString(preview, buf.toString()); 874 875 } 876 testListRemoves2_since_3()877 public void testListRemoves2_since_3() throws Exception { 878 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 879 StringBuffer buf= new StringBuffer(); 880 buf.append("package test1;\n"); 881 buf.append("public class E {\n"); 882 buf.append(" public void setMyProp(String property1) {}\n"); 883 buf.append("}\n"); 884 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 885 886 CompilationUnit astRoot= createAST(cu); 887 AST ast= astRoot.getAST(); 888 889 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 890 TypeDeclaration type = (TypeDeclaration) astRoot.types().get(0); 891 892 { // delete param, insert new 893 MethodDeclaration methodDecl= (MethodDeclaration) type.bodyDeclarations().get(0); 894 List parameters= methodDecl.parameters(); 895 rewrite.remove((ASTNode) parameters.get(0), null); 896 897 SingleVariableDeclaration decl= ast.newSingleVariableDeclaration(); 898 decl.setType(ast.newPrimitiveType(PrimitiveType.INT)); 899 decl.setName(ast.newSimpleName("property11")); 900 901 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertLast(decl, null); 902 903 } 904 String preview= evaluateRewrite(cu, rewrite); 905 906 buf= new StringBuffer(); 907 buf.append("package test1;\n"); 908 buf.append("public class E {\n"); 909 buf.append(" public void setMyProp(int property11) {}\n"); 910 buf.append("}\n"); 911 912 assertEqualString(preview, buf.toString()); 913 } 914 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=331111 _testListRemoves3()915 public void _testListRemoves3() throws Exception { 916 Map options = this.project1.getOptions(true); 917 Map newOptions = this.project1.getOptions(true); 918 try { 919 newOptions.put( 920 DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_OPENING_PAREN_IN_METHOD_DECLARATION, 921 JavaCore.INSERT); 922 newOptions.put( 923 DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_PAREN_IN_METHOD_DECLARATION, 924 JavaCore.INSERT); 925 newOptions.put( 926 DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BETWEEN_EMPTY_PARENS_IN_METHOD_DECLARATION, 927 JavaCore.DO_NOT_INSERT); 928 this.project1.setOptions(newOptions); 929 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 930 StringBuffer buf= new StringBuffer(); 931 buf.append("package test1;\n"); 932 buf.append("public class E {\n"); 933 buf.append(" public void foo( String s ) {}\n"); 934 buf.append("}\n"); 935 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 936 937 CompilationUnit astRoot= createAST(cu); 938 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 939 TypeDeclaration type = (TypeDeclaration) astRoot.types().get(0); 940 941 { // delete param, insert new 942 MethodDeclaration methodDecl= (MethodDeclaration) type.bodyDeclarations().get(0); 943 List parameters= methodDecl.parameters(); 944 rewrite.remove((ASTNode) parameters.get(0), null); 945 } 946 String preview= evaluateRewrite(cu, rewrite); 947 948 buf= new StringBuffer(); 949 buf.append("package test1;\n"); 950 buf.append("public class E {\n"); 951 buf.append(" public void foo() {}\n"); 952 buf.append("}\n"); 953 954 assertEqualString(preview, buf.toString()); 955 } finally { 956 this.project1.setOptions(options); 957 } 958 } 959 testListInserts()960 public void testListInserts() throws Exception { 961 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 962 StringBuffer buf= new StringBuffer(); 963 buf.append("package test1;\n"); 964 buf.append("public abstract class E {\n"); 965 buf.append(" public E(int p1, int p2, int p3) {}\n"); 966 buf.append(" public void gee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 967 buf.append(" public void hee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 968 buf.append(" public void iee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 969 buf.append(" public void jee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 970 buf.append(" public abstract void kee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 971 buf.append(" public abstract void lee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 972 buf.append("}\n"); 973 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 974 975 CompilationUnit astRoot= createAST(cu); 976 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 977 AST ast= astRoot.getAST(); 978 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 979 980 { // insert before first param & insert an exception 981 MethodDeclaration methodDecl= findMethodDeclaration(type, "E"); 982 List parameters= methodDecl.parameters(); 983 assertTrue("must be 3 parameters", parameters.size() == 3); 984 985 SingleVariableDeclaration newParam= createNewParam(ast, "m"); 986 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertFirst(newParam, null); 987 988 List thrownExceptions= getThrownExceptions(methodDecl); 989 assertTrue("must be 0 thrown exceptions", thrownExceptions.size() == 0); 990 991 ASTNode newThrownException= createNewExceptionType(ast, "InterruptedException"); 992 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertFirst(newThrownException, null); 993 994 } 995 { // insert before second param & insert before first exception & add synchronized 996 MethodDeclaration methodDecl= findMethodDeclaration(type, "gee"); 997 998 // change flags 999 int newModifiers= Modifier.PUBLIC | Modifier.SYNCHRONIZED; 1000 setModifiers(rewrite, methodDecl, newModifiers); 1001 1002 List parameters= methodDecl.parameters(); 1003 assertTrue("must be 3 parameters", parameters.size() == 3); 1004 1005 ASTNode secondParam= (ASTNode) parameters.get(1); 1006 SingleVariableDeclaration newParam= createNewParam(ast, "m"); 1007 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertBefore(newParam, secondParam, null); 1008 1009 List thrownExceptions= getThrownExceptions(methodDecl); 1010 assertTrue("must be 1 thrown exceptions", thrownExceptions.size() == 1); 1011 1012 ASTNode firstException= (ASTNode) thrownExceptions.get(0); 1013 ASTNode newThrownException= createNewExceptionType(ast, "InterruptedException"); 1014 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertBefore(newThrownException, firstException, null); 1015 } 1016 { // insert after last param & insert after first exception & add synchronized, static 1017 MethodDeclaration methodDecl= findMethodDeclaration(type, "hee"); 1018 1019 // change flags 1020 int newModifiers= Modifier.PUBLIC | Modifier.SYNCHRONIZED | Modifier.STATIC; 1021 setModifiers(rewrite, methodDecl, newModifiers); 1022 1023 List parameters= methodDecl.parameters(); 1024 assertTrue("must be 3 parameters", parameters.size() == 3); 1025 1026 SingleVariableDeclaration newParam= createNewParam(ast, "m"); 1027 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertLast(newParam, null); 1028 1029 List thrownExceptions= getThrownExceptions(methodDecl); 1030 assertTrue("must be 1 thrown exceptions", thrownExceptions.size() == 1); 1031 1032 ASTNode firstException= (ASTNode) thrownExceptions.get(0); 1033 ASTNode newThrownException= createNewExceptionType(ast, "InterruptedException"); 1034 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertAfter(newThrownException, firstException, null); 1035 1036 } 1037 { // insert 2 params before first & insert between two exception 1038 MethodDeclaration methodDecl= findMethodDeclaration(type, "iee"); 1039 List parameters= methodDecl.parameters(); 1040 assertTrue("must be 3 parameters", parameters.size() == 3); 1041 1042 ASTNode firstParam= (ASTNode) parameters.get(0); 1043 1044 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 1045 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 1046 1047 ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY); 1048 listRewrite.insertBefore(newParam1, firstParam, null); 1049 listRewrite.insertBefore(newParam2, firstParam, null); 1050 1051 List thrownExceptions= getThrownExceptions(methodDecl); 1052 assertTrue("must be 2 thrown exceptions", thrownExceptions.size() == 2); 1053 1054 ASTNode firstException= (ASTNode) thrownExceptions.get(0); 1055 ASTNode newThrownException= createNewExceptionType(ast, "InterruptedException"); 1056 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertAfter(newThrownException, firstException, null); 1057 } 1058 { // insert 2 params after first & replace the second exception and insert new after 1059 MethodDeclaration methodDecl= findMethodDeclaration(type, "jee"); 1060 List parameters= methodDecl.parameters(); 1061 assertTrue("must be 3 parameters", parameters.size() == 3); 1062 1063 ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY); 1064 1065 ASTNode firstParam= (ASTNode) parameters.get(0); 1066 1067 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 1068 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 1069 listRewrite.insertAfter(newParam2, firstParam, null); 1070 listRewrite.insertAfter(newParam1, firstParam, null); 1071 1072 List thrownExceptions= getThrownExceptions(methodDecl); 1073 assertTrue("must be 2 thrown exceptions", thrownExceptions.size() == 2); 1074 1075 ASTNode newThrownException1= createNewExceptionType(ast, "InterruptedException"); 1076 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertLast(newThrownException1, null); 1077 1078 ASTNode newThrownException2= createNewExceptionType(ast, "ArrayStoreException"); 1079 rewrite.replace((ASTNode) thrownExceptions.get(1), newThrownException2, null); 1080 } 1081 { // insert 2 params after last & remove the last exception and insert new after 1082 MethodDeclaration methodDecl= findMethodDeclaration(type, "kee"); 1083 List parameters= methodDecl.parameters(); 1084 assertTrue("must be 3 parameters", parameters.size() == 3); 1085 1086 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY); 1087 ASTNode lastParam= (ASTNode) parameters.get(2); 1088 1089 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 1090 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 1091 1092 listRewrite.insertAfter(newParam2, lastParam, null); 1093 listRewrite.insertAfter(newParam1, lastParam, null); 1094 1095 List thrownExceptions= getThrownExceptions(methodDecl); 1096 assertTrue("must be 3 thrown exceptions", thrownExceptions.size() == 3); 1097 1098 ASTNode lastException= (ASTNode) thrownExceptions.get(2); 1099 rewrite.remove(lastException, null); 1100 1101 ASTNode newThrownException= createNewExceptionType(ast, "InterruptedException"); 1102 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertBefore(newThrownException, lastException, null); 1103 } 1104 { // insert at first and last position & remove 2nd, add after 2nd, remove 3rd 1105 MethodDeclaration methodDecl= findMethodDeclaration(type, "lee"); 1106 List parameters= methodDecl.parameters(); 1107 assertTrue("must be 3 parameters", parameters.size() == 3); 1108 1109 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY); 1110 1111 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 1112 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 1113 listRewrite.insertFirst(newParam1, null); 1114 listRewrite.insertLast(newParam2, null); 1115 1116 List thrownExceptions= getThrownExceptions(methodDecl); 1117 assertTrue("must be 3 thrown exceptions", thrownExceptions.size() == 3); 1118 1119 ASTNode secondException= (ASTNode) thrownExceptions.get(1); 1120 ASTNode lastException= (ASTNode) thrownExceptions.get(2); 1121 rewrite.remove(secondException, null); 1122 rewrite.remove(lastException, null); 1123 1124 ASTNode newThrownException= createNewExceptionType(ast, "InterruptedException"); 1125 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertAfter(newThrownException, secondException, null); 1126 1127 } 1128 1129 1130 String preview= evaluateRewrite(cu, rewrite); 1131 1132 buf= new StringBuffer(); 1133 buf.append("package test1;\n"); 1134 buf.append("public abstract class E {\n"); 1135 buf.append(" public E(float m, int p1, int p2, int p3) throws InterruptedException {}\n"); 1136 buf.append(" public synchronized void gee(int p1, float m, int p2, int p3) throws InterruptedException, IllegalArgumentException {}\n"); 1137 buf.append(" public static synchronized void hee(int p1, int p2, int p3, float m) throws IllegalArgumentException, InterruptedException {}\n"); 1138 buf.append(" public void iee(float m1, float m2, int p1, int p2, int p3) throws IllegalArgumentException, InterruptedException, IllegalAccessException {}\n"); 1139 buf.append(" public void jee(int p1, float m1, float m2, int p2, int p3) throws IllegalArgumentException, ArrayStoreException, InterruptedException {}\n"); 1140 buf.append(" public abstract void kee(int p1, int p2, int p3, float m1, float m2) throws IllegalArgumentException, IllegalAccessException, InterruptedException;\n"); 1141 buf.append(" public abstract void lee(float m1, int p1, int p2, int p3, float m2) throws IllegalArgumentException, InterruptedException;\n"); 1142 buf.append("}\n"); 1143 1144 assertEqualString(preview, buf.toString()); 1145 1146 } 1147 testListInsert()1148 public void testListInsert() throws Exception { 1149 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1150 StringBuffer buf= new StringBuffer(); 1151 buf.append("package test1;\n"); 1152 buf.append("public abstract class E {\n"); 1153 buf.append(" public abstract void lee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 1154 buf.append("}\n"); 1155 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1156 1157 CompilationUnit astRoot= createAST(cu); 1158 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1159 AST ast= astRoot.getAST(); 1160 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1161 1162 { // insert at first and last position & remove 2nd, add after 2nd, remove 3rd 1163 MethodDeclaration methodDecl= findMethodDeclaration(type, "lee"); 1164 List parameters= methodDecl.parameters(); 1165 assertTrue("must be 3 parameters", parameters.size() == 3); 1166 1167 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY); 1168 1169 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 1170 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 1171 listRewrite.insertFirst(newParam1, null); 1172 listRewrite.insertLast(newParam2, null); 1173 1174 List thrownExceptions= getThrownExceptions(methodDecl); 1175 assertTrue("must be 3 thrown exceptions", thrownExceptions.size() == 3); 1176 1177 rewrite.remove((ASTNode) thrownExceptions.get(1), null); 1178 rewrite.remove((ASTNode) thrownExceptions.get(2), null); 1179 1180 ASTNode newThrownException= createNewExceptionType(ast, "InterruptedException"); 1181 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertLast(newThrownException, null); 1182 } 1183 1184 1185 1186 String preview= evaluateRewrite(cu, rewrite); 1187 1188 buf= new StringBuffer(); 1189 buf.append("package test1;\n"); 1190 buf.append("public abstract class E {\n"); 1191 buf.append(" public abstract void lee(float m1, int p1, int p2, int p3, float m2) throws IllegalArgumentException, InterruptedException;\n"); 1192 buf.append("}\n"); 1193 1194 assertEqualString(preview, buf.toString()); 1195 1196 } 1197 testListCombinations()1198 public void testListCombinations() throws Exception { 1199 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1200 StringBuffer buf= new StringBuffer(); 1201 buf.append("package test1;\n"); 1202 buf.append("public abstract class E {\n"); 1203 buf.append(" public E(int p1, int p2, int p3) {}\n"); 1204 buf.append(" public void gee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 1205 buf.append(" public void hee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 1206 buf.append(" public void iee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 1207 buf.append(" public void jee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 1208 buf.append(" public abstract void kee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 1209 buf.append(" public abstract void lee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 1210 buf.append("}\n"); 1211 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1212 1213 CompilationUnit astRoot= createAST(cu); 1214 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1215 AST ast= astRoot.getAST(); 1216 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1217 1218 { // delete all and insert after & insert 2 exceptions 1219 MethodDeclaration methodDecl= findMethodDeclaration(type, "E"); 1220 List parameters= methodDecl.parameters(); 1221 assertTrue("must be 3 parameters", parameters.size() == 3); 1222 1223 rewrite.remove((ASTNode) parameters.get(0), null); 1224 rewrite.remove((ASTNode) parameters.get(1), null); 1225 rewrite.remove((ASTNode) parameters.get(2), null); 1226 1227 SingleVariableDeclaration newParam= createNewParam(ast, "m"); 1228 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertLast(newParam, null); 1229 1230 1231 List thrownExceptions= getThrownExceptions(methodDecl); 1232 assertTrue("must be 0 thrown exceptions", thrownExceptions.size() == 0); 1233 1234 ASTNode newThrownException1= createNewExceptionType(ast, "InterruptedException"); 1235 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertLast(newThrownException1, null); 1236 1237 ASTNode newThrownException2= createNewExceptionType(ast, "ArrayStoreException"); 1238 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertLast(newThrownException2, null); 1239 1240 } 1241 { // delete first 2, replace last and insert after & replace first exception and insert before 1242 MethodDeclaration methodDecl= findMethodDeclaration(type, "gee"); 1243 List parameters= methodDecl.parameters(); 1244 assertTrue("must be 3 parameters", parameters.size() == 3); 1245 1246 rewrite.remove((ASTNode) parameters.get(0), null); 1247 rewrite.remove((ASTNode) parameters.get(1), null); 1248 1249 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 1250 rewrite.replace((ASTNode) parameters.get(2), newParam1, null); 1251 1252 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 1253 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertLast(newParam2, null); 1254 1255 1256 List thrownExceptions= getThrownExceptions(methodDecl); 1257 assertTrue("must be 1 thrown exceptions", thrownExceptions.size() == 1); 1258 1259 ASTNode modifiedThrownException= createNewExceptionType(ast, "InterruptedException"); 1260 rewrite.replace((ASTNode) thrownExceptions.get(0), modifiedThrownException, null); 1261 1262 ASTNode newThrownException2= createNewExceptionType(ast, "ArrayStoreException"); 1263 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertLast(newThrownException2, null); 1264 1265 } 1266 { // delete first 2, replace last and insert at first & remove first and insert before 1267 MethodDeclaration methodDecl= findMethodDeclaration(type, "hee"); 1268 List parameters= methodDecl.parameters(); 1269 assertTrue("must be 3 parameters", parameters.size() == 3); 1270 1271 rewrite.remove((ASTNode) parameters.get(0), null); 1272 rewrite.remove((ASTNode) parameters.get(1), null); 1273 1274 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 1275 rewrite.replace((ASTNode) parameters.get(2), newParam1, null); 1276 1277 SingleVariableDeclaration newParam2= createNewParam(ast, "m2"); 1278 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertFirst(newParam2, null); 1279 1280 List thrownExceptions= getThrownExceptions(methodDecl); 1281 assertTrue("must be 1 thrown exceptions", thrownExceptions.size() == 1); 1282 1283 rewrite.remove((ASTNode) thrownExceptions.get(0), null); 1284 1285 ASTNode newThrownException2= createNewExceptionType(ast, "ArrayStoreException"); 1286 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertLast(newThrownException2, null); 1287 } 1288 1289 1290 String preview= evaluateRewrite(cu, rewrite); 1291 1292 buf= new StringBuffer(); 1293 buf.append("package test1;\n"); 1294 buf.append("public abstract class E {\n"); 1295 buf.append(" public E(float m) throws InterruptedException, ArrayStoreException {}\n"); 1296 buf.append(" public void gee(float m1, float m2) throws InterruptedException, ArrayStoreException {}\n"); 1297 buf.append(" public void hee(float m2, float m1) throws ArrayStoreException {}\n"); 1298 buf.append(" public void iee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 1299 buf.append(" public void jee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 1300 buf.append(" public abstract void kee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 1301 buf.append(" public abstract void lee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 1302 buf.append("}\n"); 1303 1304 assertEqualString(preview, buf.toString()); 1305 1306 } 1307 testListCombination()1308 public void testListCombination() throws Exception { 1309 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1310 StringBuffer buf= new StringBuffer(); 1311 buf.append("package test1;\n"); 1312 buf.append("public abstract class E {\n"); 1313 buf.append(" public E(int p1, int p2, int p3) {}\n"); 1314 buf.append("}\n"); 1315 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1316 1317 CompilationUnit astRoot= createAST(cu); 1318 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1319 AST ast= astRoot.getAST(); 1320 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1321 1322 { // delete all and insert after & insert 2 exceptions 1323 MethodDeclaration methodDecl= findMethodDeclaration(type, "E"); 1324 List parameters= methodDecl.parameters(); 1325 assertTrue("must be 3 parameters", parameters.size() == 3); 1326 1327 rewrite.remove((ASTNode) parameters.get(0), null); 1328 rewrite.remove((ASTNode) parameters.get(1), null); 1329 rewrite.remove((ASTNode) parameters.get(2), null); 1330 1331 SingleVariableDeclaration newParam= createNewParam(ast, "m"); 1332 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertLast(newParam, null); 1333 1334 List thrownExceptions= getThrownExceptions(methodDecl); 1335 assertTrue("must be 0 thrown exceptions", thrownExceptions.size() == 0); 1336 1337 ASTNode newThrownException1= createNewExceptionType(ast, "InterruptedException"); 1338 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertLast(newThrownException1, null); 1339 1340 ASTNode newThrownException2= createNewExceptionType(ast, "ArrayStoreException"); 1341 rewrite.getListRewrite(methodDecl, getMethodThrownExceptionsProperty(ast)).insertLast(newThrownException2, null); 1342 1343 1344 } 1345 1346 String preview= evaluateRewrite(cu, rewrite); 1347 1348 buf= new StringBuffer(); 1349 buf.append("package test1;\n"); 1350 buf.append("public abstract class E {\n"); 1351 buf.append(" public E(float m) throws InterruptedException, ArrayStoreException {}\n"); 1352 buf.append("}\n"); 1353 1354 assertEqualString(preview, buf.toString()); 1355 1356 } 1357 testListCombination2()1358 public void testListCombination2() throws Exception { 1359 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1360 StringBuffer buf= new StringBuffer(); 1361 buf.append("package test1;\n"); 1362 buf.append("public abstract class E {\n"); 1363 buf.append(" public void foo() {\n"); 1364 buf.append(" }\n"); 1365 buf.append("\n"); 1366 buf.append(" void bar() {\n"); 1367 buf.append(" }\n"); 1368 buf.append("\n"); 1369 buf.append(" void foo2() {\n"); 1370 buf.append(" // user comment\n"); 1371 buf.append(" }\n"); 1372 buf.append("}\n"); 1373 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1374 1375 CompilationUnit astRoot= createAST(cu); 1376 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1377 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1378 1379 MethodDeclaration[] methods= type.getMethods(); 1380 Arrays.sort(methods, new Comparator() { 1381 @Override 1382 public int compare(Object o1, Object o2) { 1383 return ((MethodDeclaration) o1).getName().getIdentifier().compareTo(((MethodDeclaration) o2).getName().getIdentifier()); 1384 } 1385 }); 1386 1387 ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); 1388 for (int i= 0; i < methods.length; i++) { 1389 ASTNode copy= rewrite.createMoveTarget(methods[i]); 1390 listRewrite.insertLast(copy, null); 1391 } 1392 1393 String preview= evaluateRewrite(cu, rewrite); 1394 1395 buf= new StringBuffer(); 1396 buf.append("package test1;\n"); 1397 buf.append("public abstract class E {\n"); 1398 buf.append(" void bar() {\n"); 1399 buf.append(" }\n"); 1400 buf.append("\n"); 1401 buf.append(" public void foo() {\n"); 1402 buf.append(" }\n"); 1403 buf.append("\n"); 1404 buf.append(" void foo2() {\n"); 1405 buf.append(" // user comment\n"); 1406 buf.append(" }\n"); 1407 buf.append("}\n"); 1408 1409 assertEqualString(preview, buf.toString()); 1410 1411 } 1412 1413 testMethodBody()1414 public void testMethodBody() throws Exception { 1415 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1416 StringBuffer buf= new StringBuffer(); 1417 buf.append("package test1;\n"); 1418 buf.append("public abstract class E {\n"); 1419 buf.append(" public E(int p1, int p2, int p3) {}\n"); 1420 buf.append(" public void gee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 1421 buf.append(" public void hee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 1422 buf.append(" public void iee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 1423 buf.append(" public void jee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 1424 buf.append(" public abstract void kee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 1425 buf.append(" public abstract void lee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 1426 buf.append("}\n"); 1427 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1428 1429 CompilationUnit astRoot= createAST(cu); 1430 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1431 AST ast= astRoot.getAST(); 1432 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1433 1434 { // replace block 1435 MethodDeclaration methodDecl= findMethodDeclaration(type, "E"); 1436 1437 Block body= methodDecl.getBody(); 1438 assertTrue("No body: E", body != null); 1439 1440 Block newBlock= ast.newBlock(); 1441 1442 rewrite.replace(body, newBlock, null); 1443 } 1444 { // delete block & set abstract 1445 MethodDeclaration methodDecl= findMethodDeclaration(type, "gee"); 1446 1447 // change flags 1448 int newModifiers= Modifier.PUBLIC | Modifier.ABSTRACT; 1449 setModifiers(rewrite, methodDecl, newModifiers); 1450 1451 Block body= methodDecl.getBody(); 1452 assertTrue("No body: gee", body != null); 1453 1454 rewrite.remove(body, null); 1455 } 1456 { // insert block & set to private 1457 MethodDeclaration methodDecl= findMethodDeclaration(type, "kee"); 1458 1459 // change flags 1460 int newModifiers= Modifier.PRIVATE; 1461 setModifiers(rewrite, methodDecl, newModifiers); 1462 1463 1464 Block body= methodDecl.getBody(); 1465 assertTrue("Has body", body == null); 1466 1467 Block newBlock= ast.newBlock(); 1468 rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, newBlock, null); 1469 } 1470 1471 String preview= evaluateRewrite(cu, rewrite); 1472 1473 buf= new StringBuffer(); 1474 buf.append("package test1;\n"); 1475 buf.append("public abstract class E {\n"); 1476 buf.append(" public E(int p1, int p2, int p3) {\n"); 1477 buf.append(" }\n"); 1478 buf.append(" public abstract void gee(int p1, int p2, int p3) throws IllegalArgumentException;\n"); 1479 buf.append(" public void hee(int p1, int p2, int p3) throws IllegalArgumentException {}\n"); 1480 buf.append(" public void iee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 1481 buf.append(" public void jee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException {}\n"); 1482 buf.append(" private void kee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException {\n"); 1483 buf.append(" }\n"); 1484 buf.append(" public abstract void lee(int p1, int p2, int p3) throws IllegalArgumentException, IllegalAccessException, SecurityException;\n"); 1485 buf.append("}\n"); 1486 assertEqualString(preview, buf.toString()); 1487 1488 } 1489 testMethodDeclarationExtraDimensions_only_2_3_4()1490 public void testMethodDeclarationExtraDimensions_only_2_3_4() throws Exception { 1491 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1492 StringBuffer buf= new StringBuffer(); 1493 buf.append("package test1;\n"); 1494 buf.append("public abstract class E {\n"); 1495 buf.append(" public Object foo1() { return null; }\n"); 1496 buf.append(" public Object foo2() throws IllegalArgumentException { return null; }\n"); 1497 buf.append(" public Object foo3()[][] { return null; }\n"); 1498 buf.append(" public Object foo4()[][] throws IllegalArgumentException { return null; }\n"); 1499 buf.append(" public Object foo5()[][] { return null; }\n"); 1500 buf.append(" public Object foo6(int i)[][] throws IllegalArgumentException { return null; }\n"); 1501 buf.append(" public Object foo7(int i)[][] { return null; }\n"); 1502 buf.append("}\n"); 1503 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1504 1505 CompilationUnit astRoot= createAST(cu); 1506 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1507 AST ast= astRoot.getAST(); 1508 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1509 1510 { // add extra dim, add throws 1511 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 1512 1513 rewrite.set(methodDecl, INTERNAL_METHOD_EXTRA_DIMENSIONS_PROPERTY, 1, null); 1514 1515 Name newThrownException2= ast.newSimpleName("ArrayStoreException"); 1516 rewrite.getListRewrite(methodDecl, INTERNAL_METHOD_THROWN_EXCEPTIONS_PROPERTY).insertLast(newThrownException2, null); 1517 1518 } 1519 { // add extra dim, remove throws 1520 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo2"); 1521 1522 rewrite.set(methodDecl, INTERNAL_METHOD_EXTRA_DIMENSIONS_PROPERTY, 1, null); 1523 1524 rewrite.remove((ASTNode) getThrownExceptions(methodDecl).get(0), null); 1525 } 1526 { // remove extra dim, add throws 1527 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo3"); 1528 1529 rewrite.set(methodDecl, INTERNAL_METHOD_EXTRA_DIMENSIONS_PROPERTY, 1, null); 1530 1531 Name newThrownException2= ast.newSimpleName("ArrayStoreException"); 1532 rewrite.getListRewrite(methodDecl, INTERNAL_METHOD_THROWN_EXCEPTIONS_PROPERTY).insertLast(newThrownException2, null); 1533 1534 } 1535 { // add extra dim, remove throws 1536 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo4"); 1537 1538 rewrite.set(methodDecl, INTERNAL_METHOD_EXTRA_DIMENSIONS_PROPERTY, 1, null); 1539 1540 rewrite.remove((ASTNode) getThrownExceptions(methodDecl).get(0), null); 1541 } 1542 { // add params, add extra dim, add throws 1543 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo5"); 1544 1545 SingleVariableDeclaration newParam1= createNewParam(ast, "m1"); 1546 rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY).insertLast(newParam1, null); 1547 1548 1549 rewrite.set(methodDecl, INTERNAL_METHOD_EXTRA_DIMENSIONS_PROPERTY, 4, null); 1550 1551 Name newThrownException2= ast.newSimpleName("ArrayStoreException"); 1552 rewrite.getListRewrite(methodDecl, INTERNAL_METHOD_THROWN_EXCEPTIONS_PROPERTY).insertLast(newThrownException2, null); 1553 1554 } 1555 { // remove params, add extra dim, remove throws 1556 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo6"); 1557 1558 rewrite.remove((ASTNode) methodDecl.parameters().get(0), null); 1559 1560 rewrite.set(methodDecl, INTERNAL_METHOD_EXTRA_DIMENSIONS_PROPERTY, 4, null); 1561 1562 rewrite.remove((ASTNode) getThrownExceptions(methodDecl).get(0), null); 1563 } 1564 { // remove block 1565 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo7"); 1566 rewrite.remove(methodDecl.getBody(), null); 1567 } 1568 1569 String preview= evaluateRewrite(cu, rewrite); 1570 1571 buf= new StringBuffer(); 1572 buf.append("package test1;\n"); 1573 buf.append("public abstract class E {\n"); 1574 buf.append(" public Object foo1()[] throws ArrayStoreException { return null; }\n"); 1575 buf.append(" public Object foo2()[] { return null; }\n"); 1576 buf.append(" public Object foo3()[] throws ArrayStoreException { return null; }\n"); 1577 buf.append(" public Object foo4()[] { return null; }\n"); 1578 buf.append(" public Object foo5(float m1)[][][][] throws ArrayStoreException { return null; }\n"); 1579 buf.append(" public Object foo6()[][][][] { return null; }\n"); 1580 buf.append(" public Object foo7(int i)[][];\n"); 1581 buf.append("}\n"); 1582 assertEqualString(preview, buf.toString()); 1583 1584 } 1585 testModifiersAST3_since_3()1586 public void testModifiersAST3_since_3() throws Exception { 1587 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1588 StringBuffer buf= new StringBuffer(); 1589 buf.append("package test1;\n"); 1590 buf.append("public abstract class E {\n"); 1591 buf.append(" public Object foo1() { return null; }\n"); 1592 buf.append(" /** javadoc comment */\n"); 1593 buf.append(" public Object foo2() { return null; }\n"); 1594 buf.append(" public Object foo3() { return null; }\n"); 1595 buf.append(" /** javadoc comment */\n"); 1596 buf.append(" public Object foo4() { return null; }\n"); 1597 buf.append(" Object foo5() { return null; }\n"); 1598 buf.append(" /** javadoc comment */\n"); 1599 buf.append(" public Object foo6() { return null; }\n"); 1600 buf.append(" public Object foo7() { return null; }\n"); 1601 buf.append(" /** javadoc comment */\n"); 1602 buf.append(" public static Object foo8() { return null; }\n"); 1603 buf.append(" /** javadoc comment */\n"); 1604 buf.append(" Object foo9() { return null; }\n"); 1605 buf.append("}\n"); 1606 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1607 1608 CompilationUnit astRoot= createAST(cu); 1609 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1610 AST ast= astRoot.getAST(); 1611 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1612 1613 { // insert first and last 1614 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 1615 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1616 listRewrite.insertFirst(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 1617 listRewrite.insertLast(ast.newModifier(Modifier.ModifierKeyword.SYNCHRONIZED_KEYWORD), null); 1618 } 1619 { // insert 2x first 1620 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo2"); 1621 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1622 listRewrite.insertFirst(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 1623 listRewrite.insertFirst(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD), null); 1624 } 1625 { // remove and insert first 1626 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo3"); 1627 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 1628 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1629 listRewrite.insertFirst(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 1630 } 1631 { // remove and insert last 1632 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo4"); 1633 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 1634 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1635 listRewrite.insertLast(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 1636 } 1637 { // insert first and insert Javadoc 1638 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo5"); 1639 Javadoc javadoc= ast.newJavadoc(); 1640 TextElement textElem= ast.newTextElement(); 1641 textElem.setText("Hello"); 1642 TagElement tagElement= ast.newTagElement(); 1643 tagElement.fragments().add(textElem); 1644 javadoc.tags().add(tagElement); 1645 rewrite.set(methodDecl, MethodDeclaration.JAVADOC_PROPERTY, javadoc, null); 1646 1647 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1648 listRewrite.insertFirst(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 1649 } 1650 { // remove modifier and remove javadoc 1651 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo6"); 1652 rewrite.remove(methodDecl.getJavadoc(), null); 1653 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 1654 } 1655 { // remove modifier and insert javadoc 1656 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo7"); 1657 1658 Javadoc javadoc= ast.newJavadoc(); 1659 TextElement textElem= ast.newTextElement(); 1660 textElem.setText("Hello"); 1661 TagElement tagElement= ast.newTagElement(); 1662 tagElement.fragments().add(textElem); 1663 javadoc.tags().add(tagElement); 1664 rewrite.set(methodDecl, MethodDeclaration.JAVADOC_PROPERTY, javadoc, null); 1665 1666 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 1667 } 1668 { // remove all 1669 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo8"); 1670 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 1671 rewrite.remove((ASTNode) methodDecl.modifiers().get(1), null); 1672 } 1673 { // insert (first) with javadoc 1674 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo9"); 1675 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1676 listRewrite.insertFirst(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 1677 } 1678 1679 String preview= evaluateRewrite(cu, rewrite); 1680 1681 buf= new StringBuffer(); 1682 buf.append("package test1;\n"); 1683 buf.append("public abstract class E {\n"); 1684 buf.append(" final public synchronized Object foo1() { return null; }\n"); 1685 buf.append(" /** javadoc comment */\n"); 1686 buf.append(" static final public Object foo2() { return null; }\n"); 1687 buf.append(" final Object foo3() { return null; }\n"); 1688 buf.append(" /** javadoc comment */\n"); 1689 buf.append(" final Object foo4() { return null; }\n"); 1690 buf.append(" /**\n"); 1691 buf.append(" * Hello\n"); 1692 buf.append(" */\n"); 1693 buf.append(" final Object foo5() { return null; }\n"); 1694 buf.append(" Object foo6() { return null; }\n"); 1695 buf.append(" /**\n"); 1696 buf.append(" * Hello\n"); 1697 buf.append(" */\n"); 1698 buf.append(" Object foo7() { return null; }\n"); 1699 buf.append(" /** javadoc comment */\n"); 1700 buf.append(" Object foo8() { return null; }\n"); 1701 buf.append(" /** javadoc comment */\n"); 1702 buf.append(" final Object foo9() { return null; }\n"); 1703 buf.append("}\n"); 1704 assertEqualString(preview, buf.toString()); 1705 } 1706 testModifiersAST3WithAnnotations_since_3()1707 public void testModifiersAST3WithAnnotations_since_3() throws Exception { 1708 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1709 StringBuffer buf= new StringBuffer(); 1710 buf.append("package test1;\n"); 1711 buf.append("public abstract class E {\n"); 1712 buf.append(" public Object foo1() { return null; }\n"); 1713 buf.append(" /** javadoc comment */\n"); 1714 buf.append(" @Deprecated\n"); 1715 buf.append(" public Object foo2() { return null; }\n"); 1716 buf.append(" @ToBeRemoved\n"); 1717 buf.append(" public Object foo3() { return null; }\n"); 1718 buf.append(" /** javadoc comment */\n"); 1719 buf.append(" @ToBeRemoved\n"); 1720 buf.append(" @Deprecated\n"); 1721 buf.append(" public Object foo4() { return null; }\n"); 1722 buf.append("}\n"); 1723 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1724 1725 CompilationUnit astRoot= createAST(cu); 1726 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1727 AST ast= astRoot.getAST(); 1728 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1729 1730 { // insert annotation first before normal 1731 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 1732 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1733 MarkerAnnotation annot= ast.newMarkerAnnotation(); 1734 annot.setTypeName(ast.newSimpleName("Override")); 1735 listRewrite.insertFirst(annot, null); 1736 } 1737 { // insert annotation first before annotation 1738 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo2"); 1739 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1740 MarkerAnnotation annot= ast.newMarkerAnnotation(); 1741 annot.setTypeName(ast.newSimpleName("Override")); 1742 listRewrite.insertFirst(annot, null); 1743 } 1744 { // remove annotation before normal 1745 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo3"); 1746 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 1747 } 1748 { // remove annotation before annotation 1749 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo4"); 1750 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 1751 } 1752 1753 String preview= evaluateRewrite(cu, rewrite); 1754 1755 buf= new StringBuffer(); 1756 buf.append("package test1;\n"); 1757 buf.append("public abstract class E {\n"); 1758 buf.append(" @Override\n"); 1759 buf.append(" public Object foo1() { return null; }\n"); 1760 buf.append(" /** javadoc comment */\n"); 1761 buf.append(" @Override\n"); 1762 buf.append(" @Deprecated\n"); 1763 buf.append(" public Object foo2() { return null; }\n"); 1764 buf.append(" public Object foo3() { return null; }\n"); 1765 buf.append(" /** javadoc comment */\n"); 1766 buf.append(" @Deprecated\n"); 1767 buf.append(" public Object foo4() { return null; }\n"); 1768 buf.append("}\n"); 1769 assertEqualString(preview, buf.toString()); 1770 } 1771 testModifiersAST3WithAnnotations2_since_3()1772 public void testModifiersAST3WithAnnotations2_since_3() throws Exception { 1773 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1774 StringBuffer buf= new StringBuffer(); 1775 buf.append("package test1;\n"); 1776 buf.append("public abstract class E {\n"); 1777 buf.append(" Object foo1() { return null; }\n"); 1778 buf.append(" Object foo2() { return null; }\n"); 1779 buf.append(" @Deprecated()Object foo3() { return null; }\n"); 1780 buf.append(" @Deprecated()Object foo4() { return null; }\n"); 1781 buf.append("}\n"); 1782 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1783 1784 CompilationUnit astRoot= createAST(cu); 1785 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1786 AST ast= astRoot.getAST(); 1787 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1788 1789 { // insert annotation first 1790 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 1791 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1792 MarkerAnnotation annot= ast.newMarkerAnnotation(); 1793 annot.setTypeName(ast.newSimpleName("Override")); 1794 listRewrite.insertFirst(annot, null); 1795 } 1796 { // insert modifier first 1797 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo2"); 1798 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1799 Modifier modifier= ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); 1800 listRewrite.insertFirst(modifier, null); 1801 } 1802 { // insert modifier last 1803 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo3"); 1804 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1805 Modifier modifier= ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); 1806 listRewrite.insertLast(modifier, null); 1807 } 1808 { // insert modifier first 1809 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo4"); 1810 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY); 1811 Modifier modifier= ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); 1812 listRewrite.insertFirst(modifier, null); 1813 } 1814 1815 String preview= evaluateRewrite(cu, rewrite); 1816 1817 buf= new StringBuffer(); 1818 buf.append("package test1;\n"); 1819 buf.append("public abstract class E {\n"); 1820 buf.append(" @Override\n"); 1821 buf.append(" Object foo1() { return null; }\n"); 1822 buf.append(" public Object foo2() { return null; }\n"); 1823 buf.append(" @Deprecated()\n"); 1824 buf.append(" public Object foo3() { return null; }\n"); 1825 buf.append(" public @Deprecated()Object foo4() { return null; }\n"); 1826 buf.append("}\n"); 1827 assertEqualString(preview, buf.toString()); 1828 } 1829 1830 1831 testFieldDeclaration_only_2()1832 public void testFieldDeclaration_only_2() throws Exception { 1833 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1834 StringBuffer buf= new StringBuffer(); 1835 buf.append("package test1;\n"); 1836 buf.append("public class A {\n"); 1837 buf.append(" int i1= 1;\n"); 1838 buf.append(" int i2= 1, k2= 2, n2= 3;\n"); 1839 buf.append(" static final int i3= 1, k3= 2, n3= 3;\n"); 1840 buf.append("}\n"); 1841 ICompilationUnit cu= pack1.createCompilationUnit("A.java", buf.toString(), false, null); 1842 1843 CompilationUnit astRoot= createAST(cu); 1844 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1845 1846 AST ast= astRoot.getAST(); 1847 1848 assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); 1849 TypeDeclaration type= findTypeDeclaration(astRoot, "A"); 1850 1851 FieldDeclaration[] fieldDeclarations= type.getFields(); 1852 assertTrue("Number of fieldDeclarations not 3", fieldDeclarations.length == 3); 1853 { // add modifier, change type, add fragment 1854 FieldDeclaration decl= fieldDeclarations[0]; 1855 1856 // add modifier 1857 int newModifiers= Modifier.FINAL; 1858 rewrite.set(decl, INTERNAL_FIELD_MODIFIERS_PROPERTY, Integer.valueOf(newModifiers), null); 1859 1860 PrimitiveType newType= ast.newPrimitiveType(PrimitiveType.BOOLEAN); 1861 rewrite.replace(decl.getType(), newType, null); 1862 1863 VariableDeclarationFragment frag= ast.newVariableDeclarationFragment(); 1864 frag.setName(ast.newSimpleName("k1")); 1865 frag.setInitializer(null); 1866 1867 rewrite.getListRewrite(decl, FieldDeclaration.FRAGMENTS_PROPERTY).insertLast(frag, null); 1868 1869 } 1870 { // add modifiers, remove first two fragments, replace last 1871 FieldDeclaration decl= fieldDeclarations[1]; 1872 1873 // add modifier 1874 int newModifiers= Modifier.FINAL | Modifier.STATIC | Modifier.TRANSIENT; 1875 rewrite.set(decl, INTERNAL_FIELD_MODIFIERS_PROPERTY, Integer.valueOf(newModifiers), null); 1876 1877 List fragments= decl.fragments(); 1878 assertTrue("Number of fragments not 3", fragments.size() == 3); 1879 1880 rewrite.remove((ASTNode) fragments.get(0), null); 1881 rewrite.remove((ASTNode) fragments.get(1), null); 1882 1883 VariableDeclarationFragment frag= ast.newVariableDeclarationFragment(); 1884 frag.setName(ast.newSimpleName("k2")); 1885 frag.setInitializer(null); 1886 1887 rewrite.replace((ASTNode) fragments.get(2), frag, null); 1888 } 1889 { // remove modifiers 1890 FieldDeclaration decl= fieldDeclarations[2]; 1891 1892 // change modifier 1893 int newModifiers= 0; 1894 rewrite.set(decl, INTERNAL_FIELD_MODIFIERS_PROPERTY, Integer.valueOf(newModifiers), null); 1895 } 1896 1897 String preview= evaluateRewrite(cu, rewrite); 1898 1899 buf= new StringBuffer(); 1900 buf.append("package test1;\n"); 1901 buf.append("public class A {\n"); 1902 buf.append(" final boolean i1= 1, k1;\n"); 1903 buf.append(" static final transient int k2;\n"); 1904 buf.append(" int i3= 1, k3= 2, n3= 3;\n"); 1905 buf.append("}\n"); 1906 1907 assertEqualString(preview, buf.toString()); 1908 1909 } 1910 testInitializer_only_2()1911 public void testInitializer_only_2() throws Exception { 1912 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1913 StringBuffer buf= new StringBuffer(); 1914 buf.append("package test1;\n"); 1915 buf.append("public class A {\n"); 1916 buf.append(" {\n"); 1917 buf.append(" foo();\n"); 1918 buf.append(" }\n"); 1919 buf.append(" static {\n"); 1920 buf.append(" }\n"); 1921 buf.append("}\n"); 1922 ICompilationUnit cu= pack1.createCompilationUnit("A.java", buf.toString(), false, null); 1923 1924 CompilationUnit astRoot= createAST(cu); 1925 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1926 1927 AST ast= astRoot.getAST(); 1928 1929 assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); 1930 TypeDeclaration type= findTypeDeclaration(astRoot, "A"); 1931 1932 List declarations= type.bodyDeclarations(); 1933 assertTrue("Number of fieldDeclarations not 2", declarations.size() == 2); 1934 { // change modifier, replace body 1935 Initializer initializer= (Initializer) declarations.get(0); 1936 1937 // add modifier 1938 int newModifiers= Modifier.STATIC; 1939 rewrite.set(initializer, INTERNAL_INITIALIZER_MODIFIERS_PROPERTY, Integer.valueOf(newModifiers), null); 1940 1941 1942 Block block= ast.newBlock(); 1943 block.statements().add(ast.newReturnStatement()); 1944 1945 rewrite.replace(initializer.getBody(), block, null); 1946 } 1947 { // change modifier 1948 Initializer initializer= (Initializer) declarations.get(1); 1949 1950 int newModifiers= 0; 1951 rewrite.set(initializer, INTERNAL_INITIALIZER_MODIFIERS_PROPERTY, Integer.valueOf(newModifiers), null); 1952 1953 } 1954 1955 String preview= evaluateRewrite(cu, rewrite); 1956 1957 buf= new StringBuffer(); 1958 buf.append("package test1;\n"); 1959 buf.append("public class A {\n"); 1960 buf.append(" static {\n"); 1961 buf.append(" return;\n"); 1962 buf.append(" }\n"); 1963 buf.append(" {\n"); 1964 buf.append(" }\n"); 1965 buf.append("}\n"); 1966 1967 assertEqualString(preview, buf.toString()); 1968 1969 } 1970 1971 testMethodDeclarationParamShuffel()1972 public void testMethodDeclarationParamShuffel() throws Exception { 1973 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 1974 StringBuffer buf= new StringBuffer(); 1975 buf.append("package test1;\n"); 1976 buf.append("public abstract class E {\n"); 1977 buf.append(" public Object foo1(int i, boolean b) { return null; }\n"); 1978 buf.append("}\n"); 1979 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 1980 1981 CompilationUnit astRoot= createAST(cu); 1982 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 1983 AST ast= astRoot.getAST(); 1984 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 1985 1986 { // add extra dim, add throws 1987 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 1988 1989 List params= methodDecl.parameters(); 1990 1991 SingleVariableDeclaration first= (SingleVariableDeclaration) params.get(0); 1992 SingleVariableDeclaration second= (SingleVariableDeclaration) params.get(1); 1993 rewrite.replace(first.getName(), ast.newSimpleName("x"), null); 1994 rewrite.replace(second.getName(), ast.newSimpleName("y"), null); 1995 1996 ASTNode copy1= rewrite.createCopyTarget(first); 1997 ASTNode copy2= rewrite.createCopyTarget(second); 1998 1999 rewrite.replace(first, copy2, null); 2000 rewrite.replace(second, copy1, null); 2001 2002 } 2003 2004 String preview= evaluateRewrite(cu, rewrite); 2005 2006 buf= new StringBuffer(); 2007 buf.append("package test1;\n"); 2008 buf.append("public abstract class E {\n"); 2009 buf.append(" public Object foo1(boolean y, int x) { return null; }\n"); 2010 buf.append("}\n"); 2011 assertEqualString(preview, buf.toString()); 2012 2013 } 2014 2015 testMethodDeclarationParamShuffel1()2016 public void testMethodDeclarationParamShuffel1() throws Exception { 2017 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2018 StringBuffer buf= new StringBuffer(); 2019 buf.append("package test1;\n"); 2020 buf.append("public abstract class E {\n"); 2021 buf.append(" public Object foo1(int i, boolean b) { return null; }\n"); 2022 buf.append("}\n"); 2023 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 2024 2025 CompilationUnit astRoot= createAST(cu); 2026 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2027 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 2028 2029 { 2030 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 2031 2032 List params= methodDecl.parameters(); 2033 2034 SingleVariableDeclaration first= (SingleVariableDeclaration) params.get(0); 2035 SingleVariableDeclaration second= (SingleVariableDeclaration) params.get(1); 2036 2037 ASTNode copy2= rewrite.createCopyTarget(second); 2038 2039 rewrite.replace(first, copy2, null); 2040 rewrite.remove(second, null); 2041 } 2042 2043 String preview= evaluateRewrite(cu, rewrite); 2044 2045 buf= new StringBuffer(); 2046 buf.append("package test1;\n"); 2047 buf.append("public abstract class E {\n"); 2048 buf.append(" public Object foo1(boolean b) { return null; }\n"); 2049 buf.append("}\n"); 2050 assertEqualString(preview, buf.toString()); 2051 2052 } 2053 testMethodDeclaration_bug24916()2054 public void testMethodDeclaration_bug24916() throws Exception { 2055 2056 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2057 StringBuffer buf= new StringBuffer(); 2058 buf.append("package test1;\n"); 2059 buf.append("public class DD {\n"); 2060 buf.append(" private int DD()[]{\n"); 2061 buf.append(" };\n"); 2062 buf.append("}\n"); 2063 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2064 2065 CompilationUnit astRoot= createAST(cu); 2066 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2067 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2068 { 2069 MethodDeclaration methodDecl= findMethodDeclaration(type, "DD"); 2070 2071 rewrite.set(methodDecl, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null); 2072 setExtraDimensions(rewrite, methodDecl, 0); 2073 } 2074 2075 String preview= evaluateRewrite(cu, rewrite); 2076 2077 buf= new StringBuffer(); 2078 buf.append("package test1;\n"); 2079 buf.append("public class DD {\n"); 2080 buf.append(" private DD(){\n"); 2081 buf.append(" };\n"); 2082 buf.append("}\n"); 2083 assertEqualString(preview, buf.toString()); 2084 2085 } 2086 testMethodComments1()2087 public void testMethodComments1() throws Exception { 2088 2089 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2090 StringBuffer buf= new StringBuffer(); 2091 buf.append("package test1;\n"); 2092 2093 buf.append("public class DD {\n"); 2094 buf.append(" // one line comment\n"); 2095 buf.append(" private void foo(){\n"); 2096 buf.append(" }\n"); 2097 buf.append("\n"); 2098 buf.append(" /**\n"); 2099 buf.append(" *\n"); 2100 buf.append(" */\n"); 2101 buf.append(" private void foo1(){\n"); 2102 buf.append(" }\n"); 2103 buf.append("\n"); 2104 buf.append(" private void foo2(){\n"); 2105 buf.append(" }\n"); 2106 buf.append("}\n"); 2107 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2108 2109 CompilationUnit astRoot= createAST(cu); 2110 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2111 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2112 { 2113 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); 2114 rewrite.remove(methodDecl, null); 2115 } 2116 2117 String preview= evaluateRewrite(cu, rewrite); 2118 2119 buf= new StringBuffer(); 2120 buf.append("package test1;\n"); 2121 buf.append("public class DD {\n"); 2122 buf.append(" /**\n"); 2123 buf.append(" *\n"); 2124 buf.append(" */\n"); 2125 buf.append(" private void foo1(){\n"); 2126 buf.append(" }\n"); 2127 buf.append("\n"); 2128 buf.append(" private void foo2(){\n"); 2129 buf.append(" }\n"); 2130 buf.append("}\n"); 2131 assertEqualString(preview, buf.toString()); 2132 2133 } 2134 testMethodComments2()2135 public void testMethodComments2() throws Exception { 2136 2137 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2138 StringBuffer buf= new StringBuffer(); 2139 buf.append("package test1;\n"); 2140 buf.append("public class DD {\n"); 2141 buf.append(" // one line comment\n"); 2142 buf.append(" private void foo(){\n"); 2143 buf.append(" }\n"); 2144 buf.append("\n"); 2145 buf.append(" /*\n"); 2146 buf.append(" *\n"); 2147 buf.append(" */\n"); 2148 buf.append(" private void foo1(){\n"); 2149 buf.append(" }\n"); 2150 buf.append("\n"); 2151 buf.append(" private void foo2(){\n"); 2152 buf.append(" }\n"); 2153 buf.append("}\n"); 2154 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2155 2156 CompilationUnit astRoot= createAST(cu); 2157 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2158 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2159 { 2160 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo2"); 2161 ASTNode node= rewrite.createCopyTarget(methodDecl); 2162 2163 ASTNode firstDecl= (ASTNode) type.bodyDeclarations().get(0); 2164 rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY).insertAfter(node, firstDecl, null); 2165 } 2166 2167 String preview= evaluateRewrite(cu, rewrite); 2168 2169 buf= new StringBuffer(); 2170 buf.append("package test1;\n"); 2171 buf.append("public class DD {\n"); 2172 buf.append(" // one line comment\n"); 2173 buf.append(" private void foo(){\n"); 2174 buf.append(" }\n"); 2175 buf.append("\n"); 2176 buf.append(" private void foo2(){\n"); 2177 buf.append(" }\n"); 2178 buf.append("\n"); 2179 buf.append(" /*\n"); 2180 buf.append(" *\n"); 2181 buf.append(" */\n"); 2182 buf.append(" private void foo1(){\n"); 2183 buf.append(" }\n"); 2184 buf.append("\n"); 2185 buf.append(" private void foo2(){\n"); 2186 buf.append(" }\n"); 2187 buf.append("}\n"); 2188 assertEqualString(preview, buf.toString()); 2189 2190 } 2191 testMethodComments3()2192 public void testMethodComments3() throws Exception { 2193 2194 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2195 StringBuffer buf= new StringBuffer(); 2196 buf.append("package test1;\n"); 2197 2198 buf.append("public class DD {\n"); 2199 buf.append(" // one line comment\n"); 2200 buf.append("\n"); 2201 buf.append(" private void foo(){\n"); 2202 buf.append(" } // another\n"); 2203 buf.append("\n"); 2204 buf.append(" /*\n"); 2205 buf.append(" *\n"); 2206 buf.append(" */\n"); 2207 buf.append(" private void foo1(){\n"); 2208 buf.append(" }\n"); 2209 buf.append("\n"); 2210 buf.append(" private void foo2(){\n"); 2211 buf.append(" }\n"); 2212 buf.append("}\n"); 2213 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2214 2215 CompilationUnit astRoot= createAST(cu); 2216 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2217 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2218 { 2219 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); 2220 rewrite.remove(methodDecl, null); 2221 } 2222 2223 String preview= evaluateRewrite(cu, rewrite); 2224 2225 buf= new StringBuffer(); 2226 buf.append("package test1;\n"); 2227 buf.append("public class DD {\n"); 2228 buf.append(" // one line comment\n"); 2229 buf.append("\n"); 2230 buf.append(" /*\n"); 2231 buf.append(" *\n"); 2232 buf.append(" */\n"); 2233 buf.append(" private void foo1(){\n"); 2234 buf.append(" }\n"); 2235 buf.append("\n"); 2236 buf.append(" private void foo2(){\n"); 2237 buf.append(" }\n"); 2238 buf.append("}\n"); 2239 assertEqualString(preview, buf.toString()); 2240 2241 } 2242 2243 testBUG_38447()2244 public void testBUG_38447() throws Exception { 2245 2246 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2247 StringBuffer buf= new StringBuffer(); 2248 buf.append("package test1;\n"); 2249 2250 buf.append("public class DD {\n"); 2251 buf.append("\n"); 2252 buf.append(" private void foo(){\n"); 2253 buf.append("\n"); // missing closing bracket 2254 buf.append(" /*\n"); 2255 buf.append(" *\n"); 2256 buf.append(" */\n"); 2257 buf.append(" private void foo1(){\n"); 2258 buf.append(" }\n"); 2259 buf.append("\n"); 2260 buf.append(" private void foo2(){\n"); 2261 buf.append(" }\n"); 2262 buf.append("}\n"); 2263 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2264 2265 CompilationUnit astRoot= createAST(cu); 2266 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2267 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2268 { 2269 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); 2270 rewrite.remove(methodDecl, null); 2271 } 2272 2273 String preview= evaluateRewrite(cu, rewrite); 2274 2275 buf= new StringBuffer(); 2276 buf.append("package test1;\n"); 2277 buf.append("public class DD {\n"); 2278 buf.append("\n"); 2279 buf.append(" /*\n"); 2280 buf.append(" *\n"); 2281 buf.append(" */\n"); 2282 buf.append(" private void foo1(){\n"); 2283 buf.append(" }\n"); 2284 buf.append("\n"); 2285 buf.append(" private void foo2(){\n"); 2286 buf.append(" }\n"); 2287 buf.append("}\n"); 2288 assertEqualString(preview, buf.toString()); 2289 2290 } 2291 testMethodComments4()2292 public void testMethodComments4() throws Exception { 2293 2294 2295 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2296 StringBuffer buf= new StringBuffer(); 2297 buf.append("package test1;\n"); 2298 2299 buf.append("public class DD {\n"); 2300 buf.append(" // one line comment\n"); 2301 buf.append("\n"); 2302 buf.append(" private void foo(){\n"); 2303 buf.append(" } // another\n"); 2304 buf.append("\n"); 2305 buf.append(" /*\n"); 2306 buf.append(" *\n"); 2307 buf.append(" */\n"); 2308 buf.append(" private void foo1(){\n"); 2309 buf.append(" }\n"); 2310 buf.append("\n"); 2311 buf.append(" private void foo2(){\n"); 2312 buf.append(" }\n"); 2313 buf.append("}\n"); 2314 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2315 2316 CompilationUnit astRoot= createAST(cu); 2317 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2318 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2319 { 2320 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); 2321 ASTNode copy= rewrite.createCopyTarget(methodDecl); 2322 2323 rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY).insertLast(copy, null); 2324 2325 MethodDeclaration newMethodDecl= createNewMethod(astRoot.getAST(), "xoo", false); 2326 rewrite.replace(methodDecl, newMethodDecl, null); 2327 2328 //MethodDeclaration methodDecl2= findMethodDeclaration(type, "foo1"); 2329 //rewrite.markAsReplaced(methodDecl2, copy); 2330 } 2331 2332 String preview= evaluateRewrite(cu, rewrite); 2333 2334 buf= new StringBuffer(); 2335 buf.append("package test1;\n"); 2336 2337 buf.append("public class DD {\n"); 2338 buf.append(" // one line comment\n"); 2339 buf.append("\n"); 2340 buf.append(" private void xoo(String str) {\n"); 2341 buf.append(" }\n"); 2342 buf.append("\n"); 2343 buf.append(" /*\n"); 2344 buf.append(" *\n"); 2345 buf.append(" */\n"); 2346 buf.append(" private void foo1(){\n"); 2347 buf.append(" }\n"); 2348 buf.append("\n"); 2349 buf.append(" private void foo2(){\n"); 2350 buf.append(" }\n"); 2351 buf.append("\n"); 2352 buf.append(" private void foo(){\n"); 2353 buf.append(" } // another\n"); 2354 buf.append("}\n"); 2355 assertEqualString(preview, buf.toString()); 2356 2357 } 2358 2359 /** @deprecated using deprecated code */ testInsertFieldAfter_only_2()2360 public void testInsertFieldAfter_only_2() throws Exception { 2361 2362 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2363 StringBuffer buf= new StringBuffer(); 2364 buf.append("package test1;\n"); 2365 2366 buf.append("public class DD {\n"); 2367 buf.append(" private int fCount1;\n"); 2368 buf.append("\n"); 2369 buf.append(" /*\n"); 2370 buf.append(" *\n"); 2371 buf.append(" */\n"); 2372 buf.append(" private void foo1(){\n"); 2373 buf.append(" }\n"); 2374 buf.append("}\n"); 2375 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2376 2377 CompilationUnit astRoot= createAST(cu); 2378 AST ast= astRoot.getAST(); 2379 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2380 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2381 { 2382 VariableDeclarationFragment frag= ast.newVariableDeclarationFragment(); 2383 frag.setName(ast.newSimpleName("fColor")); 2384 FieldDeclaration newField= ast.newFieldDeclaration(frag); 2385 newField.setType(ast.newPrimitiveType(PrimitiveType.CHAR)); 2386 newField.setModifiers(Modifier.PRIVATE); 2387 2388 rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(newField, 1, null); 2389 } 2390 2391 String preview= evaluateRewrite(cu, rewrite); 2392 2393 buf= new StringBuffer(); 2394 buf.append("package test1;\n"); 2395 2396 buf.append("public class DD {\n"); 2397 buf.append(" private int fCount1;\n"); 2398 buf.append(" private char fColor;\n"); 2399 buf.append("\n"); 2400 buf.append(" /*\n"); 2401 buf.append(" *\n"); 2402 buf.append(" */\n"); 2403 buf.append(" private void foo1(){\n"); 2404 buf.append(" }\n"); 2405 buf.append("}\n"); 2406 assertEqualString(preview, buf.toString()); 2407 } 2408 2409 testVarArgs_since_3()2410 public void testVarArgs_since_3() throws Exception { 2411 2412 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2413 StringBuffer buf= new StringBuffer(); 2414 buf.append("package test1;\n"); 2415 buf.append("public class DD {\n"); 2416 buf.append(" private void foo1(String format, Object... args){\n"); 2417 buf.append(" }\n"); 2418 buf.append(" private void foo2(String format, Object[] args) {\n"); 2419 buf.append(" }\n"); 2420 buf.append("}\n"); 2421 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2422 2423 CompilationUnit astRoot= createAST(cu); 2424 AST ast= astRoot.getAST(); 2425 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2426 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2427 { 2428 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 2429 SingleVariableDeclaration param= (SingleVariableDeclaration) methodDecl.parameters().get(1); 2430 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null); 2431 } 2432 { 2433 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo2"); 2434 SingleVariableDeclaration param= (SingleVariableDeclaration) methodDecl.parameters().get(1); 2435 rewrite.set(param, SingleVariableDeclaration.TYPE_PROPERTY, ast.newPrimitiveType(PrimitiveType.INT), null); 2436 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.TRUE, null); 2437 } 2438 2439 String preview= evaluateRewrite(cu, rewrite); 2440 2441 buf= new StringBuffer(); 2442 buf.append("package test1;\n"); 2443 buf.append("public class DD {\n"); 2444 buf.append(" private void foo1(String format, Object args){\n"); 2445 buf.append(" }\n"); 2446 buf.append(" private void foo2(String format, int... args) {\n"); 2447 buf.append(" }\n"); 2448 buf.append("}\n"); 2449 assertEqualString(preview, buf.toString()); 2450 2451 this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ELLIPSIS, JavaCore.INSERT); 2452 2453 preview= evaluateRewrite(cu, rewrite); 2454 2455 buf= new StringBuffer(); 2456 buf.append("package test1;\n"); 2457 buf.append("public class DD {\n"); 2458 buf.append(" private void foo1(String format, Object args){\n"); 2459 buf.append(" }\n"); 2460 buf.append(" private void foo2(String format, int ... args) {\n"); 2461 buf.append(" }\n"); 2462 buf.append("}\n"); 2463 assertEqualString(preview, buf.toString()); 2464 } 2465 testAnnotationTypeMember_since_4()2466 public void testAnnotationTypeMember_since_4() throws Exception { 2467 2468 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2469 StringBuffer buf= new StringBuffer(); 2470 buf.append("package test1;\n"); 2471 buf.append("public @interface DD {\n"); 2472 buf.append(" public String value1();\n"); 2473 buf.append(" String value2() default 1;\n"); 2474 buf.append(" String value3() default 2;\n"); 2475 buf.append("}\n"); 2476 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2477 2478 CompilationUnit astRoot= createAST(cu); 2479 AST ast= astRoot.getAST(); 2480 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2481 AnnotationTypeDeclaration type= (AnnotationTypeDeclaration) findAbstractTypeDeclaration(astRoot, "DD"); 2482 { 2483 AnnotationTypeMemberDeclaration methodDecl= (AnnotationTypeMemberDeclaration) type.bodyDeclarations().get(0); 2484 rewrite.remove((ASTNode) methodDecl.modifiers().get(0), null); 2485 rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, ast.newPrimitiveType(PrimitiveType.BOOLEAN), null); 2486 rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.NAME_PROPERTY, ast.newSimpleName("test"), null); 2487 2488 rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.DEFAULT_PROPERTY, ast.newNumberLiteral("1"), null); 2489 } 2490 { 2491 AnnotationTypeMemberDeclaration methodDecl= (AnnotationTypeMemberDeclaration) type.bodyDeclarations().get(1); 2492 rewrite.getListRewrite(methodDecl, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY).insertFirst(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), null); 2493 rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.DEFAULT_PROPERTY, ast.newNumberLiteral("2"), null); 2494 } 2495 { 2496 AnnotationTypeMemberDeclaration methodDecl= (AnnotationTypeMemberDeclaration) type.bodyDeclarations().get(2); 2497 rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.DEFAULT_PROPERTY, null, null); 2498 } 2499 2500 String preview= evaluateRewrite(cu, rewrite); 2501 2502 buf= new StringBuffer(); 2503 buf.append("package test1;\n"); 2504 buf.append("public @interface DD {\n"); 2505 buf.append(" boolean test() default 1;\n"); 2506 buf.append(" public String value2() default 2;\n"); 2507 buf.append(" String value3();\n"); 2508 buf.append("}\n"); 2509 assertEqualString(preview, buf.toString()); 2510 } 2511 testEnumConstantDeclaration1_since_3()2512 public void testEnumConstantDeclaration1_since_3() throws Exception { 2513 2514 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2515 StringBuffer buf= new StringBuffer(); 2516 buf.append("package test1;\n"); 2517 buf.append("public enum DD {\n"); 2518 buf.append(" E1(1), E2, E3(), E4(1, 2)\n"); 2519 buf.append("}\n"); 2520 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2521 2522 CompilationUnit astRoot= createAST(cu); 2523 AST ast= astRoot.getAST(); 2524 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2525 EnumDeclaration type= (EnumDeclaration) findAbstractTypeDeclaration(astRoot, "DD"); 2526 { 2527 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(0); 2528 rewrite.set(enumConst, EnumConstantDeclaration.NAME_PROPERTY, ast.newSimpleName("X"), null); 2529 ListRewrite listRewrite= rewrite.getListRewrite(enumConst, EnumConstantDeclaration.ARGUMENTS_PROPERTY); 2530 listRewrite.remove((ASTNode) enumConst.arguments().get(0), null); 2531 } 2532 { 2533 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(1); 2534 ListRewrite listRewrite= rewrite.getListRewrite(enumConst, EnumConstantDeclaration.ARGUMENTS_PROPERTY); 2535 listRewrite.insertFirst(ast.newNumberLiteral("1"), null); 2536 } 2537 { 2538 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(2); 2539 ListRewrite listRewrite= rewrite.getListRewrite(enumConst, EnumConstantDeclaration.ARGUMENTS_PROPERTY); 2540 listRewrite.insertFirst(ast.newNumberLiteral("2"), null); 2541 } 2542 { 2543 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(3); 2544 ListRewrite listRewrite= rewrite.getListRewrite(enumConst, EnumConstantDeclaration.ARGUMENTS_PROPERTY); 2545 listRewrite.remove((ASTNode) enumConst.arguments().get(0), null); 2546 } 2547 2548 String preview= evaluateRewrite(cu, rewrite); 2549 2550 buf= new StringBuffer(); 2551 buf.append("package test1;\n"); 2552 buf.append("public enum DD {\n"); 2553 buf.append(" X, E2(1), E3(2), E4(2)\n"); 2554 buf.append("}\n"); 2555 assertEqualString(preview, buf.toString()); 2556 } 2557 testEnumConstantDeclaration2_since_3()2558 public void testEnumConstantDeclaration2_since_3() throws Exception { 2559 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2560 StringBuffer buf= new StringBuffer(); 2561 buf.append("package test1;\n"); 2562 buf.append("public enum DD {\n"); 2563 buf.append(" E1Add(1),\n"); 2564 buf.append(" E2Add,\n"); 2565 buf.append(" E3Add(1),\n"); 2566 buf.append(" E4Add(1),\n"); 2567 buf.append(" E5Add(1) {\n"); 2568 buf.append(" public void foo() {\n"); 2569 buf.append(" }\n"); 2570 buf.append(" },\n"); 2571 buf.append(" E1Remove(1) {\n"); 2572 buf.append(" public void foo() {\n"); 2573 buf.append(" }\n"); 2574 buf.append(" },\n"); 2575 buf.append(" E2Remove {\n"); 2576 buf.append(" public void foo() {\n"); 2577 buf.append(" }\n"); 2578 buf.append(" },\n"); 2579 buf.append(" E3Remove(1) {\n"); 2580 buf.append(" public void foo() {\n"); 2581 buf.append(" }\n"); 2582 buf.append(" },\n"); 2583 buf.append(" E4Remove(1) {\n"); 2584 buf.append(" public void foo() {\n"); 2585 buf.append(" }\n"); 2586 buf.append(" }\n"); 2587 buf.append("}\n"); 2588 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2589 2590 CompilationUnit astRoot= createAST(cu); 2591 AST ast= astRoot.getAST(); 2592 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2593 EnumDeclaration type= (EnumDeclaration) findAbstractTypeDeclaration(astRoot, "DD"); 2594 { 2595 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(0); 2596 assertNull(enumConst.getAnonymousClassDeclaration()); 2597 2598 AnonymousClassDeclaration classDecl= ast.newAnonymousClassDeclaration(); 2599 ListRewrite bodyRewrite= rewrite.getListRewrite(classDecl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); 2600 bodyRewrite.insertFirst(createNewMethod(ast, "test", false), null); 2601 2602 rewrite.set(enumConst, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, classDecl, null); 2603 } 2604 { 2605 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(1); 2606 assertNull(enumConst.getAnonymousClassDeclaration()); 2607 2608 ListRewrite argsRewrite= rewrite.getListRewrite(enumConst, EnumConstantDeclaration.ARGUMENTS_PROPERTY); 2609 argsRewrite.insertFirst(ast.newNumberLiteral("1"), null); 2610 2611 AnonymousClassDeclaration classDecl= ast.newAnonymousClassDeclaration(); 2612 ListRewrite bodyRewrite= rewrite.getListRewrite(classDecl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); 2613 bodyRewrite.insertFirst(createNewMethod(ast, "test", false), null); 2614 2615 rewrite.set(enumConst, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, classDecl, null); 2616 2617 } 2618 { 2619 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(2); 2620 assertNull(enumConst.getAnonymousClassDeclaration()); 2621 2622 rewrite.remove((ASTNode) enumConst.arguments().get(0), null); 2623 2624 AnonymousClassDeclaration classDecl= ast.newAnonymousClassDeclaration(); 2625 ListRewrite bodyRewrite= rewrite.getListRewrite(classDecl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); 2626 bodyRewrite.insertFirst(createNewMethod(ast, "test", false), null); 2627 2628 rewrite.set(enumConst, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, classDecl, null); 2629 } 2630 { 2631 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(3); 2632 assertNull(enumConst.getAnonymousClassDeclaration()); 2633 2634 AnonymousClassDeclaration classDecl= ast.newAnonymousClassDeclaration(); 2635 rewrite.set(enumConst, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, classDecl, null); 2636 } 2637 { 2638 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(4); 2639 2640 AnonymousClassDeclaration classDecl= enumConst.getAnonymousClassDeclaration(); 2641 assertNotNull(classDecl); 2642 2643 ListRewrite bodyRewrite= rewrite.getListRewrite(classDecl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); 2644 bodyRewrite.insertFirst(createNewMethod(ast, "test", false), null); 2645 } 2646 { 2647 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(5); 2648 2649 AnonymousClassDeclaration classDecl= enumConst.getAnonymousClassDeclaration(); 2650 assertNotNull(classDecl); 2651 2652 rewrite.remove(classDecl, null); 2653 } 2654 { 2655 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(6); 2656 2657 AnonymousClassDeclaration classDecl= enumConst.getAnonymousClassDeclaration(); 2658 assertNotNull(classDecl); 2659 2660 ListRewrite argsRewrite= rewrite.getListRewrite(enumConst, EnumConstantDeclaration.ARGUMENTS_PROPERTY); 2661 argsRewrite.insertFirst(ast.newNumberLiteral("1"), null); 2662 2663 rewrite.remove(classDecl, null); 2664 } 2665 { 2666 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(7); 2667 2668 AnonymousClassDeclaration classDecl= enumConst.getAnonymousClassDeclaration(); 2669 assertNotNull(classDecl); 2670 2671 rewrite.remove((ASTNode) enumConst.arguments().get(0), null); 2672 rewrite.remove(classDecl, null); 2673 } 2674 { 2675 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(8); 2676 2677 AnonymousClassDeclaration classDecl= enumConst.getAnonymousClassDeclaration(); 2678 assertNotNull(classDecl); 2679 2680 rewrite.remove((ASTNode) classDecl.bodyDeclarations().get(0), null); 2681 } 2682 2683 2684 String preview= evaluateRewrite(cu, rewrite); 2685 2686 buf= new StringBuffer(); 2687 buf.append("package test1;\n"); 2688 buf.append("public enum DD {\n"); 2689 buf.append(" E1Add(1) {\n"); 2690 buf.append(" private void test(String str) {\n"); 2691 buf.append(" }\n"); 2692 buf.append(" },\n"); 2693 buf.append(" E2Add(1) {\n"); 2694 buf.append(" private void test(String str) {\n"); 2695 buf.append(" }\n"); 2696 buf.append(" },\n"); 2697 buf.append(" E3Add {\n"); 2698 buf.append(" private void test(String str) {\n"); 2699 buf.append(" }\n"); 2700 buf.append(" },\n"); 2701 buf.append(" E4Add(1) {\n"); 2702 buf.append(" },\n"); 2703 buf.append(" E5Add(1) {\n"); 2704 buf.append(" private void test(String str) {\n"); 2705 buf.append(" }\n"); 2706 buf.append("\n"); 2707 buf.append(" public void foo() {\n"); 2708 buf.append(" }\n"); 2709 buf.append(" },\n"); 2710 buf.append(" E1Remove(1),\n"); 2711 buf.append(" E2Remove(1),\n"); 2712 buf.append(" E3Remove,\n"); 2713 buf.append(" E4Remove(1) {\n"); 2714 buf.append(" }\n"); 2715 buf.append("}\n"); 2716 assertEqualString(preview, buf.toString()); 2717 } 2718 testEnumConstantDeclaration_bug114119_since_3()2719 public void testEnumConstantDeclaration_bug114119_since_3() throws Exception { 2720 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2721 StringBuffer buf= new StringBuffer(); 2722 buf.append("package test1;\n"); 2723 buf.append("public enum DD {\n"); 2724 buf.append(" RED, BROWN(), GREEN(){};\n"); 2725 buf.append("}\n"); 2726 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2727 2728 CompilationUnit astRoot= createAST(cu); 2729 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2730 EnumDeclaration type= (EnumDeclaration) findAbstractTypeDeclaration(astRoot, "DD"); 2731 { 2732 EnumConstantDeclaration enumConst= (EnumConstantDeclaration) type.enumConstants().get(2); 2733 assertNotNull(enumConst.getAnonymousClassDeclaration()); 2734 2735 rewrite.remove(enumConst.getAnonymousClassDeclaration(), null); 2736 } 2737 2738 2739 String preview= evaluateRewrite(cu, rewrite); 2740 2741 buf= new StringBuffer(); 2742 buf.append("package test1;\n"); 2743 buf.append("public enum DD {\n"); 2744 buf.append(" RED, BROWN(), GREEN();\n"); 2745 buf.append("}\n"); 2746 assertEqualString(preview, buf.toString()); 2747 } 2748 testVarArgsAnnotations_since_8()2749 public void testVarArgsAnnotations_since_8() throws Exception { 2750 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2751 StringBuffer buf= new StringBuffer(); 2752 buf.append("package test1;\n"); 2753 buf.append("@interface Marker {\n"); 2754 buf.append("}\n"); 2755 buf.append("public class DD {\n"); 2756 buf.append(" public void foo1(String format, Object @Marker... args){\n"); 2757 buf.append(" }\n"); 2758 buf.append(" public void foo2(Object... args) {\n"); 2759 buf.append(" }\n"); 2760 buf.append(" public void foo3(Object @Marker ... args) {\n"); 2761 buf.append(" }\n"); 2762 buf.append(" public void foo4(Object @Marker... args) {\n"); 2763 buf.append(" }\n"); 2764 buf.append(" public void foo5(Object... args) {\n"); 2765 buf.append(" }\n"); 2766 buf.append(" public void foo6(Object args) {\n"); 2767 buf.append(" }\n"); 2768 buf.append(" public void foo7(Object @Marker... args) {\n"); 2769 buf.append(" }\n"); 2770 buf.append(" public void foo8(Object @Marker... args) {\n"); 2771 buf.append(" }\n"); 2772 buf.append(" public void foo9(@B @C int @A... a) {\n"); 2773 buf.append(" }\n"); 2774 buf.append(" public void foo10(Object args) {\n"); 2775 buf.append(" }\n"); 2776 buf.append(" public void foo11(Object @Marker... args) {\n"); 2777 buf.append(" }\n"); 2778 buf.append(" public void foo12(Object... args) {\n"); 2779 buf.append(" }\n"); 2780 buf.append("}\n"); 2781 ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null); 2782 2783 CompilationUnit astRoot= createAST(cu, false); 2784 AST ast= astRoot.getAST(); 2785 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2786 TypeDeclaration type= findTypeDeclaration(astRoot, "DD"); 2787 2788 { 2789 // Remove annotation from first method args - boundary condition - 2790 // - only one annotation should be present. 2791 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 2792 SingleVariableDeclaration param = (SingleVariableDeclaration) methodDecl.parameters().get(1); 2793 rewrite.remove((ASTNode)param.varargsAnnotations().get(0), null); 2794 2795 // Add one annotation to the second method - boundary condition 2796 // - no annotation should be present 2797 methodDecl= findMethodDeclaration(type, "foo2"); 2798 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2799 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 2800 markerAnnotation.setTypeName(ast.newSimpleName("X")); 2801 ListRewrite listRewrite= rewrite.getListRewrite(param, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY); 2802 listRewrite.insertFirst(markerAnnotation, null); 2803 2804 // Remove the varargs property - annotation(s) should disappear 2805 methodDecl= findMethodDeclaration(type, "foo3"); 2806 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2807 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null); 2808 2809 // Remove the varargs property - annotation(s) should disappear 2810 // - differs from the above due to the absence of a blank before ... 2811 methodDecl= findMethodDeclaration(type, "foo4"); 2812 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2813 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null); 2814 2815 // Remove the varargs property - Existing functionality unchanged without annotations 2816 methodDecl= findMethodDeclaration(type, "foo5"); 2817 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2818 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null); 2819 2820 // Add the varargs property and annotation 2821 methodDecl= findMethodDeclaration(type, "foo6"); 2822 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2823 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.TRUE, null); 2824 markerAnnotation= ast.newMarkerAnnotation(); 2825 markerAnnotation.setTypeName(ast.newSimpleName("X")); 2826 listRewrite= rewrite.getListRewrite(param, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY); 2827 listRewrite.insertFirst(markerAnnotation, null); 2828 2829 // Replace annotation 2830 methodDecl= findMethodDeclaration(type, "foo7"); 2831 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2832 markerAnnotation= ast.newMarkerAnnotation(); 2833 markerAnnotation.setTypeName(ast.newSimpleName("X")); 2834 rewrite.replace((ASTNode)param.varargsAnnotations().get(0), markerAnnotation, null); 2835 2836 // Reset and Set Varargs - output should not change. 2837 methodDecl= findMethodDeclaration(type, "foo8"); 2838 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2839 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null); 2840 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.TRUE, null); 2841 2842 // Add multiple (two) annotations, remove an existing annotation 2843 methodDecl= findMethodDeclaration(type, "foo9"); 2844 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2845 NormalAnnotation normalAnnotation = ast.newNormalAnnotation(); 2846 normalAnnotation.setTypeName(ast.newSimpleName("X")); 2847 listRewrite= rewrite.getListRewrite(param, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY); 2848 listRewrite.insertFirst(normalAnnotation, null); 2849 markerAnnotation= ast.newMarkerAnnotation(); 2850 markerAnnotation.setTypeName(ast.newSimpleName("Y")); 2851 listRewrite.insertFirst(markerAnnotation, null); 2852 rewrite.remove((ASTNode)param.varargsAnnotations().get(0), null); 2853 2854 // Add the varargs property 2855 methodDecl= findMethodDeclaration(type, "foo10"); 2856 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2857 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.TRUE, null); 2858 2859 // Remove the annotations and varargs property as well. 2860 methodDecl= findMethodDeclaration(type, "foo11"); 2861 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2862 rewrite.remove((ASTNode)param.varargsAnnotations().get(0), null); 2863 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null); 2864 2865 // Add an annotation but remove the varargs property - should not add the annotation. 2866 methodDecl= findMethodDeclaration(type, "foo12"); 2867 param = (SingleVariableDeclaration) methodDecl.parameters().get(0); 2868 rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null); 2869 markerAnnotation= ast.newMarkerAnnotation(); 2870 markerAnnotation.setTypeName(ast.newSimpleName("X")); 2871 listRewrite= rewrite.getListRewrite(param, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY); 2872 listRewrite.insertFirst(markerAnnotation, null); 2873 } 2874 2875 String preview= evaluateRewrite(cu, rewrite); 2876 2877 buf= new StringBuffer(); 2878 buf.append("package test1;\n"); 2879 buf.append("@interface Marker {\n"); 2880 buf.append("}\n"); 2881 buf.append("public class DD {\n"); 2882 buf.append(" public void foo1(String format, Object... args){\n"); 2883 buf.append(" }\n"); 2884 buf.append(" public void foo2(Object @X... args) {\n"); 2885 buf.append(" }\n"); 2886 buf.append(" public void foo3(Object args) {\n"); 2887 buf.append(" }\n"); 2888 buf.append(" public void foo4(Object args) {\n"); 2889 buf.append(" }\n"); 2890 buf.append(" public void foo5(Object args) {\n"); 2891 buf.append(" }\n"); 2892 buf.append(" public void foo6(Object @X... args) {\n"); 2893 buf.append(" }\n"); 2894 buf.append(" public void foo7(Object @X... args) {\n"); 2895 buf.append(" }\n"); 2896 buf.append(" public void foo8(Object @Marker... args) {\n"); 2897 buf.append(" }\n"); 2898 buf.append(" public void foo9(@B @C int @Y @X()... a) {\n"); 2899 buf.append(" }\n"); 2900 buf.append(" public void foo10(Object... args) {\n"); 2901 buf.append(" }\n"); 2902 buf.append(" public void foo11(Object args) {\n"); 2903 buf.append(" }\n"); 2904 buf.append(" public void foo12(Object args) {\n"); 2905 buf.append(" }\n"); 2906 buf.append("}\n"); 2907 assertEqualString(preview, buf.toString()); 2908 } 2909 testMethodDeclChangesBug77538()2910 public void testMethodDeclChangesBug77538() throws Exception { 2911 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2912 StringBuffer buf= new StringBuffer(); 2913 buf.append("// comment\n"); 2914 buf.append("public class A {\n"); 2915 buf.append(" public int foo() {\n"); 2916 buf.append(" return 0;\n"); 2917 buf.append(" }\n"); 2918 buf.append("}\n"); 2919 ICompilationUnit cu= pack1.createCompilationUnit("A.java", buf.toString(), false, null); 2920 2921 // Get method declaration and its body 2922 CompilationUnit astRoot= createAST(cu); 2923 AST ast= astRoot.getAST(); 2924 TypeDeclaration type= findTypeDeclaration(astRoot, "A"); 2925 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); 2926 Block body = methodDecl.getBody(); 2927 2928 // start record of the modifications 2929 astRoot.recordModifications(); 2930 2931 // Modify method body 2932 Block newBody = ast.newBlock(); 2933 methodDecl.setBody(newBody); 2934 VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); 2935 fragment.setName(ast.newSimpleName("lock")); 2936 fragment.setInitializer(ast.newQualifiedName(ast.newSimpleName("OS"), ast.newSimpleName("lock"))); 2937 VariableDeclarationExpression variableDeclarationExpression = ast.newVariableDeclarationExpression(fragment); 2938 variableDeclarationExpression.setType(ast.newSimpleType(ast.newSimpleName("Lock"))); 2939 ExpressionStatement expressionStatement = ast.newExpressionStatement(variableDeclarationExpression); 2940 newBody.statements().add(expressionStatement); 2941 TryStatement tryStatement = ast.newTryStatement(); 2942 MethodInvocation methodInvocation = ast.newMethodInvocation(); 2943 methodInvocation.setName(ast.newSimpleName("lock")); 2944 methodInvocation.setExpression(ast.newSimpleName("lock")); 2945 ExpressionStatement expressionStatement2 = ast.newExpressionStatement(methodInvocation); 2946 body.statements().add(0, expressionStatement2); 2947 tryStatement.setBody(body); 2948 Block finallyBlock = ast.newBlock(); 2949 tryStatement.setFinally(finallyBlock); 2950 methodInvocation = ast.newMethodInvocation(); 2951 methodInvocation.setName(ast.newSimpleName("unLock")); 2952 methodInvocation.setExpression(ast.newSimpleName("lock")); 2953 expressionStatement2 = ast.newExpressionStatement(methodInvocation); 2954 finallyBlock.statements().add(expressionStatement2); 2955 newBody.statements().add(tryStatement); 2956 2957 // Verify that body extended length does not become negative! 2958 assertFalse("Invalid extended length for "+body, astRoot.getExtendedLength(body)<0); 2959 } 2960 testAnnotations_since_3()2961 public void testAnnotations_since_3() throws Exception { 2962 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 2963 StringBuffer buf= new StringBuffer(); 2964 buf.append("package test1;\n"); 2965 buf.append("@An\n"); 2966 buf.append("@An()\n"); 2967 buf.append("@An(val=1, val=2)\n"); 2968 buf.append("@An(val=1, val=2)\n"); 2969 buf.append("@An(1)\n"); 2970 buf.append("class E {\n"); 2971 buf.append("}\n"); 2972 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 2973 2974 CompilationUnit astRoot= createAST(cu); 2975 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 2976 AST ast= astRoot.getAST(); 2977 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 2978 List modifiers= type.modifiers(); 2979 assertEquals(5, modifiers.size()); 2980 { 2981 MarkerAnnotation an= (MarkerAnnotation) modifiers.get(0); 2982 SimpleName newName= ast.newSimpleName("X"); 2983 rewrite.set(an, MarkerAnnotation.TYPE_NAME_PROPERTY, newName, null); 2984 } 2985 { 2986 NormalAnnotation an= (NormalAnnotation) modifiers.get(1); 2987 SimpleName newName= ast.newSimpleName("X"); 2988 rewrite.set(an, NormalAnnotation.TYPE_NAME_PROPERTY, newName, null); 2989 2990 ListRewrite listRewrite= rewrite.getListRewrite(an, NormalAnnotation.VALUES_PROPERTY); 2991 2992 MemberValuePair newPair= ast.newMemberValuePair(); 2993 newPair.setName(ast.newSimpleName("foo")); 2994 newPair.setValue(ast.newNumberLiteral("0")); 2995 2996 listRewrite.insertFirst(newPair, null); 2997 } 2998 { 2999 NormalAnnotation an= (NormalAnnotation) modifiers.get(2); 3000 SimpleName newName= ast.newSimpleName("X"); 3001 rewrite.set(an, NormalAnnotation.TYPE_NAME_PROPERTY, newName, null); 3002 3003 List values= an.values(); 3004 3005 ListRewrite listRewrite= rewrite.getListRewrite(an, NormalAnnotation.VALUES_PROPERTY); 3006 listRewrite.remove((ASTNode) values.get(0), null); 3007 3008 MemberValuePair p= (MemberValuePair) values.get(1); 3009 SimpleName newMember= ast.newSimpleName("Y"); 3010 SimpleName newValue= ast.newSimpleName("Z"); 3011 rewrite.set(p, MemberValuePair.NAME_PROPERTY, newMember, null); 3012 rewrite.set(p, MemberValuePair.VALUE_PROPERTY, newValue, null); 3013 3014 MemberValuePair newPair= ast.newMemberValuePair(); 3015 newPair.setName(ast.newSimpleName("foo")); 3016 newPair.setValue(ast.newNumberLiteral("0")); 3017 3018 listRewrite.insertLast(newPair, null); 3019 } 3020 { 3021 NormalAnnotation an= (NormalAnnotation) modifiers.get(3); 3022 SimpleName newName= ast.newSimpleName("X"); 3023 rewrite.set(an, NormalAnnotation.TYPE_NAME_PROPERTY, newName, null); 3024 3025 List values= an.values(); 3026 3027 ListRewrite listRewrite= rewrite.getListRewrite(an, NormalAnnotation.VALUES_PROPERTY); 3028 listRewrite.remove((ASTNode) values.get(0), null); 3029 listRewrite.remove((ASTNode) values.get(1), null); 3030 } 3031 { 3032 SingleMemberAnnotation an= (SingleMemberAnnotation) modifiers.get(4); 3033 SimpleName newName= ast.newSimpleName("X"); 3034 rewrite.set(an, SingleMemberAnnotation.TYPE_NAME_PROPERTY, newName, null); 3035 rewrite.set(an, SingleMemberAnnotation.VALUE_PROPERTY, ast.newBooleanLiteral(true), null); 3036 } 3037 3038 3039 String preview= evaluateRewrite(cu, rewrite); 3040 3041 buf= new StringBuffer(); 3042 buf.append("package test1;\n"); 3043 buf.append("@X\n"); 3044 buf.append("@X(foo = 0)\n"); 3045 buf.append("@X(Y=Z, foo = 0)\n"); 3046 buf.append("@X()\n"); 3047 buf.append("@X(true)\n"); 3048 buf.append("class E {\n"); 3049 buf.append("}\n"); 3050 assertEqualString(preview, buf.toString()); 3051 } 3052 testParameterAnnotations_since_3()3053 public void testParameterAnnotations_since_3() throws Exception { 3054 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 3055 StringBuffer buf= new StringBuffer(); 3056 buf.append("package test1;\n"); 3057 buf.append("class E {\n"); 3058 buf.append(" public void foo(@A int a, @B1 @B2 int b, int c, @D int d) {\n"); 3059 buf.append(" }\n"); 3060 buf.append("}\n"); 3061 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 3062 3063 CompilationUnit astRoot= createAST(cu); 3064 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 3065 AST ast= astRoot.getAST(); 3066 MethodDeclaration methodDecl= (MethodDeclaration) findTypeDeclaration(astRoot, "E").bodyDeclarations().get(0); 3067 List params= methodDecl.parameters(); 3068 assertEquals(4, params.size()); 3069 { 3070 SingleVariableDeclaration decl= (SingleVariableDeclaration) params.get(0); 3071 3072 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3073 markerAnnotation.setTypeName(ast.newSimpleName("X")); 3074 3075 ListRewrite listRewrite= rewrite.getListRewrite(decl, SingleVariableDeclaration.MODIFIERS2_PROPERTY); 3076 listRewrite.insertFirst(markerAnnotation, null); 3077 } 3078 { 3079 SingleVariableDeclaration decl= (SingleVariableDeclaration) params.get(1); 3080 3081 rewrite.remove((ASTNode) decl.modifiers().get(0), null); 3082 } 3083 { 3084 SingleVariableDeclaration decl= (SingleVariableDeclaration) params.get(2); 3085 3086 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3087 markerAnnotation.setTypeName(ast.newSimpleName("X")); 3088 3089 ListRewrite listRewrite= rewrite.getListRewrite(decl, SingleVariableDeclaration.MODIFIERS2_PROPERTY); 3090 listRewrite.insertFirst(markerAnnotation, null); 3091 } 3092 { 3093 SingleVariableDeclaration decl= (SingleVariableDeclaration) params.get(3); 3094 3095 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3096 markerAnnotation.setTypeName(ast.newSimpleName("X")); 3097 3098 rewrite.replace((ASTNode) decl.modifiers().get(0), markerAnnotation, null); 3099 } 3100 { 3101 SingleVariableDeclaration decl= ast.newSingleVariableDeclaration(); 3102 3103 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3104 markerAnnotation.setTypeName(ast.newSimpleName("X")); 3105 decl.modifiers().add(markerAnnotation); 3106 3107 Type type= ast.newPrimitiveType(PrimitiveType.INT); 3108 decl.setType(type); 3109 3110 decl.setName(ast.newSimpleName("e")); 3111 3112 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY); 3113 listRewrite.insertLast(decl, null); 3114 } 3115 3116 String preview= evaluateRewrite(cu, rewrite); 3117 3118 buf= new StringBuffer(); 3119 buf.append("package test1;\n"); 3120 buf.append("class E {\n"); 3121 buf.append(" public void foo(@X @A int a, @B2 int b, @X int c, @X int d, @X int e) {\n"); 3122 buf.append(" }\n"); 3123 buf.append("}\n"); 3124 assertEqualString(preview, buf.toString()); 3125 3126 this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PARAMETER, JavaCore.INSERT); 3127 3128 preview= evaluateRewrite(cu, rewrite); 3129 3130 buf= new StringBuffer(); 3131 buf.append("package test1;\n"); 3132 buf.append("class E {\n"); 3133 buf.append(" public void foo(@X @A int a, @B2 int b, @X int c, @X int d, @X int e) {\n"); 3134 buf.append(" }\n"); 3135 buf.append("}\n"); 3136 assertEqualString(preview, buf.toString()); 3137 } testExtraDimwithAnnotations_since_8()3138 public void testExtraDimwithAnnotations_since_8() throws Exception { 3139 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 3140 StringBuffer buf= new StringBuffer(); 3141 buf.append("package test1;\n"); 3142 buf.append("import java.lang.annotation.ElementType;\n"); 3143 buf.append("public abstract class E {\n"); 3144 buf.append(" public Object foo1()[] throws ArrayStoreException { return null; }\n"); 3145 buf.append(" public Object foo2()[] { return null; }\n"); 3146 buf.append(" public Object foo3() @Annot1 [] @Annot2 [] { return null; }\n"); 3147 buf.append(" public Object foo4()@Annot1 [] @Annot2 [] throws IllegalArgumentException { return null; }\n"); 3148 buf.append(" public Object foo5() @Annot1 [] @Annot2 [] { return null; }\n"); 3149 buf.append(" public Object foo6(int i) @Annot1 [] @Annot2 [] throws IllegalArgumentException { return null; }\n"); 3150 buf.append(" public Object foo7(int i) @Annot1 [] @Annot2 [] { return null; }\n"); 3151 buf.append("}\n"); 3152 buf.append("@java.lang.annotation.Target(value= {ElementType.TYPE_USE})\n"); 3153 buf.append("@interface Annot1 {}\n"); 3154 buf.append("@java.lang.annotation.Target(value= {ElementType.TYPE_USE})\n"); 3155 buf.append("@interface Annot2 {}\n"); 3156 3157 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 3158 CompilationUnit astRoot= createAST(cu); 3159 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 3160 AST ast= astRoot.getAST(); 3161 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 3162 3163 { 3164 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1"); 3165 3166 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY); 3167 Dimension dim= ast.newDimension(); 3168 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3169 markerAnnotation.setTypeName(ast.newSimpleName("Annot1")); 3170 dim.annotations().add(markerAnnotation); 3171 listRewrite.insertAt(dim, 1, null); 3172 3173 dim= ast.newDimension(); 3174 markerAnnotation= ast.newMarkerAnnotation(); 3175 markerAnnotation.setTypeName(ast.newSimpleName("Annot2")); 3176 dim.annotations().add(markerAnnotation); 3177 listRewrite.insertAt(dim, 2, null); 3178 3179 ASTNode exception = (ASTNode) methodDecl.thrownExceptionTypes().get(0); 3180 rewrite.getListRewrite(methodDecl, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY).remove(exception, null); 3181 } 3182 { 3183 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo2"); 3184 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY); 3185 3186 Dimension dim= ast.newDimension(); 3187 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3188 markerAnnotation.setTypeName(ast.newSimpleName("Annot1")); 3189 dim.annotations().add(markerAnnotation); 3190 3191 listRewrite.insertAt(dim, 1, null); 3192 3193 Type exception= ast.newSimpleType(ast.newSimpleName("ArrayStoreException")); 3194 rewrite.getListRewrite(methodDecl, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY).insertFirst(exception, null); 3195 } 3196 { 3197 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo3"); 3198 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY); 3199 3200 Dimension dim= ast.newDimension(); 3201 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3202 markerAnnotation.setTypeName(ast.newSimpleName("Annot1")); 3203 dim.annotations().add(markerAnnotation); 3204 3205 markerAnnotation= ast.newMarkerAnnotation(); 3206 markerAnnotation.setTypeName(ast.newSimpleName("Annot2")); 3207 dim.annotations().add(markerAnnotation); 3208 listRewrite.insertAt(dim, 1, null); 3209 } 3210 { 3211 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo4"); 3212 Dimension dim= (Dimension) methodDecl.extraDimensions().get(0); 3213 ListRewrite listRewrite= rewrite.getListRewrite(dim, Dimension.ANNOTATIONS_PROPERTY); 3214 3215 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3216 markerAnnotation.setTypeName(ast.newSimpleName("Annot2")); 3217 listRewrite.insertAt(markerAnnotation, 0, null); 3218 3219 dim= (Dimension) methodDecl.extraDimensions().get(1); 3220 listRewrite= rewrite.getListRewrite(dim, Dimension.ANNOTATIONS_PROPERTY); 3221 3222 markerAnnotation= ast.newMarkerAnnotation(); 3223 markerAnnotation.setTypeName(ast.newSimpleName("Annot1")); 3224 listRewrite.insertAt(markerAnnotation, 1, null); 3225 } 3226 { 3227 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo5"); 3228 Dimension dim= (Dimension) methodDecl.extraDimensions().get(0); 3229 Annotation annot= (Annotation) dim.annotations().get(0); 3230 ListRewrite listRewrite= rewrite.getListRewrite(dim, Dimension.ANNOTATIONS_PROPERTY); 3231 listRewrite.remove(annot, null); 3232 3233 dim= (Dimension) methodDecl.extraDimensions().get(1); 3234 listRewrite= rewrite.getListRewrite(dim, Dimension.ANNOTATIONS_PROPERTY); 3235 listRewrite.insertAt(annot, 1, null); 3236 } 3237 { 3238 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo6"); 3239 Dimension dim= (Dimension) methodDecl.extraDimensions().get(0); 3240 Annotation annot= (Annotation) dim.annotations().get(0); 3241 ListRewrite listRewrite= rewrite.getListRewrite(dim, Dimension.ANNOTATIONS_PROPERTY); 3242 listRewrite.remove(annot, null); 3243 3244 dim= (Dimension) methodDecl.extraDimensions().get(1); 3245 annot= (Annotation) dim.annotations().get(0); 3246 listRewrite= rewrite.getListRewrite(dim, Dimension.ANNOTATIONS_PROPERTY); 3247 listRewrite.remove(annot, null); 3248 } 3249 { 3250 MethodDeclaration methodDecl= findMethodDeclaration(type, "foo7"); 3251 ListRewrite listRewrite= rewrite.getListRewrite(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY); 3252 Dimension dim= (Dimension) methodDecl.extraDimensions().get(0); 3253 listRewrite.remove(dim, null); 3254 dim= (Dimension) methodDecl.extraDimensions().get(1); 3255 listRewrite.remove(dim, null); 3256 } 3257 3258 String preview= evaluateRewrite(cu, rewrite); 3259 buf= new StringBuffer(); 3260 buf.append("package test1;\n"); 3261 buf.append("import java.lang.annotation.ElementType;\n"); 3262 buf.append("public abstract class E {\n"); 3263 buf.append(" public Object foo1()[]@Annot1 []@Annot2 [] { return null; }\n"); 3264 buf.append(" public Object foo2()[]@Annot1 [] throws ArrayStoreException { return null; }\n"); 3265 buf.append(" public Object foo3() @Annot1 [] @Annot1 @Annot2 []@Annot2 [] { return null; }\n"); 3266 buf.append(" public Object foo4()@Annot2 @Annot1 [] @Annot2 @Annot1 [] throws IllegalArgumentException { return null; }\n"); 3267 buf.append(" public Object foo5() [] @Annot2 @Annot1 [] { return null; }\n"); 3268 buf.append(" public Object foo6(int i) [] [] throws IllegalArgumentException { return null; }\n"); 3269 buf.append(" public Object foo7(int i) { return null; }\n"); 3270 buf.append("}\n"); 3271 buf.append("@java.lang.annotation.Target(value= {ElementType.TYPE_USE})\n"); 3272 buf.append("@interface Annot1 {}\n"); 3273 buf.append("@java.lang.annotation.Target(value= {ElementType.TYPE_USE})\n"); 3274 buf.append("@interface Annot2 {}\n"); 3275 assertEqualString(preview, buf.toString()); 3276 } testReceiver1_since_8()3277 public void testReceiver1_since_8() throws Exception { 3278 IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); 3279 StringBuffer buf = new StringBuffer(); 3280 buf.append("package test1;\n"); 3281 buf.append("class X {\n"); 3282 buf.append(" public void foo(@A @B @C X this, int i, int j) {\n"); 3283 buf.append(" }\n"); 3284 buf.append("}\n"); 3285 ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null); 3286 CompilationUnit astRoot = createAST(cu); 3287 ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); 3288 AST ast = astRoot.getAST(); 3289 MethodDeclaration method = (MethodDeclaration) findTypeDeclaration(astRoot, "X").bodyDeclarations().get(0); 3290 Type receiverType = method.getReceiverType(); 3291 assertEquals("Invalid receiver type", ASTNode.SIMPLE_TYPE, receiverType.getNodeType()); 3292 MarkerAnnotation annot = ast.newMarkerAnnotation(); 3293 annot.setTypeName(ast.newSimpleName("Marker")); 3294 ListRewrite listRewrite = rewrite.getListRewrite(receiverType, SimpleType.ANNOTATIONS_PROPERTY); 3295 listRewrite.insertFirst(annot, null); 3296 3297 String preview = evaluateRewrite(cu, rewrite); 3298 buf = new StringBuffer(); 3299 buf.append("package test1;\n"); 3300 buf.append("class X {\n"); 3301 buf.append(" public void foo(@Marker @A @B @C X this, int i, int j) {\n"); 3302 buf.append(" }\n"); 3303 buf.append("}\n"); 3304 assertEqualString(preview, buf.toString()); 3305 } testReceiver2_since_8()3306 public void testReceiver2_since_8() throws Exception { 3307 IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); 3308 StringBuffer buf = new StringBuffer(); 3309 buf.append("package test1;\n"); 3310 buf.append("class X {\n"); 3311 buf.append(" class Y {\n"); 3312 buf.append(" public Y(@A X this, int i, @B boolean b, @A int j) {\n"); 3313 buf.append(" }\n"); 3314 buf.append(" }\n"); 3315 buf.append("}\n"); 3316 ICompilationUnit cu = pack1.createCompilationUnit("X.java", buf.toString(), false, null); 3317 CompilationUnit astRoot = createAST(cu); 3318 ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); 3319 AST ast = astRoot.getAST(); 3320 TypeDeclaration type = (TypeDeclaration) findTypeDeclaration(astRoot, "X").bodyDeclarations().get(0); 3321 MethodDeclaration method = (MethodDeclaration) type.bodyDeclarations().get(0); 3322 List params = method.parameters(); 3323 3324 SingleVariableDeclaration first = (SingleVariableDeclaration) params.get(0); 3325 SingleVariableDeclaration second = (SingleVariableDeclaration) params.get(1); 3326 SingleVariableDeclaration third = (SingleVariableDeclaration) params.get(2); 3327 rewrite.replace(first.getName(), ast.newSimpleName("i"), null); 3328 rewrite.replace(second.getName(), ast.newSimpleName("b"), null); 3329 3330 ASTNode copy1 = rewrite.createCopyTarget(first); 3331 ASTNode copy2 = rewrite.createCopyTarget(second); 3332 3333 rewrite.replace(first, copy2, null); 3334 rewrite.replace(second, copy1, null); 3335 rewrite.remove(third, null); 3336 3337 String preview = evaluateRewrite(cu, rewrite); 3338 buf = new StringBuffer(); 3339 buf.append("package test1;\n"); 3340 buf.append("class X {\n"); 3341 buf.append(" class Y {\n"); 3342 buf.append(" public Y(@A X this, @B boolean b, int i) {\n"); 3343 buf.append(" }\n"); 3344 buf.append(" }\n"); 3345 buf.append("}\n"); 3346 assertEqualString(preview, buf.toString()); 3347 } testReceiver3_since_8()3348 public void testReceiver3_since_8() throws Exception { 3349 IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); 3350 StringBuffer buf = new StringBuffer(); 3351 buf.append("package test1;\n"); 3352 buf.append("class X {\n"); 3353 buf.append(" public void foo(X this) {}\n"); 3354 buf.append(" public void foo() {}\n"); 3355 buf.append(" public void foo(X this,/*comment*/ int i) {}\n"); 3356 buf.append(" public void foo(int i, int j) {}\n"); 3357 buf.append(" public void foo(X this) {}\n"); 3358 buf.append(" public void foo(X this, float f1, float f2) {}\n"); 3359 buf.append(" public void foo(X this, int i) {}\n"); 3360 buf.append(" public void foo(X this, float f) {}\n"); 3361 buf.append(" public void foo1(X this, float f) {}\n"); 3362 buf.append("}\n"); 3363 ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null); 3364 CompilationUnit astRoot = createAST(cu); 3365 AST ast = astRoot.getAST(); 3366 ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); 3367 TypeDeclaration type = findTypeDeclaration(astRoot, "X"); 3368 MethodDeclaration method1 = (MethodDeclaration) type.bodyDeclarations().get(0); 3369 MethodDeclaration method2 = (MethodDeclaration) type.bodyDeclarations().get(1); 3370 MethodDeclaration method3 = (MethodDeclaration) type.bodyDeclarations().get(2); 3371 MethodDeclaration method4 = (MethodDeclaration) type.bodyDeclarations().get(3); 3372 MethodDeclaration method5 = (MethodDeclaration) type.bodyDeclarations().get(4); 3373 MethodDeclaration method6 = (MethodDeclaration) type.bodyDeclarations().get(5); 3374 MethodDeclaration method7 = (MethodDeclaration) type.bodyDeclarations().get(6); 3375 MethodDeclaration method8 = (MethodDeclaration) type.bodyDeclarations().get(7); 3376 MethodDeclaration method9 = (MethodDeclaration) type.bodyDeclarations().get(8); 3377 3378 SimpleType receiver1 = (SimpleType) method1.getReceiverType(); 3379 SimpleType receiver3 = (SimpleType) method3.getReceiverType(); 3380 SimpleType receiver5 = (SimpleType) method5.getReceiverType(); 3381 SimpleType receiver6 = (SimpleType) method6.getReceiverType(); 3382 SimpleType receiver8 = (SimpleType) method8.getReceiverType(); 3383 SimpleType receiver9 = (SimpleType) method9.getReceiverType(); 3384 3385 SimpleType receiverCopy = (SimpleType) rewrite.createCopyTarget(receiver1); 3386 rewrite.set(method2, MethodDeclaration.RECEIVER_TYPE_PROPERTY, receiverCopy, null); 3387 rewrite.remove(receiver1, null); 3388 3389 receiverCopy = (SimpleType) rewrite.createCopyTarget(receiver3); 3390 rewrite.set(method4, MethodDeclaration.RECEIVER_TYPE_PROPERTY, receiverCopy, null); 3391 rewrite.remove(receiver3, null); 3392 3393 ListRewrite listRewrite = rewrite.getListRewrite(method3, MethodDeclaration.MODIFIERS2_PROPERTY); 3394 listRewrite.insertLast(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD), null); 3395 3396 receiverCopy = ast.newSimpleType(ast.newSimpleName("XY")); 3397 rewrite.replace(receiver5, receiverCopy, null); 3398 3399 receiverCopy = ast.newSimpleType(ast.newSimpleName("XY")); 3400 rewrite.replace(receiver6, receiverCopy, null); 3401 SingleVariableDeclaration paramCopy = (SingleVariableDeclaration) rewrite.createCopyTarget((SingleVariableDeclaration) method6.parameters().get(0)); 3402 rewrite.remove((SingleVariableDeclaration) method6.parameters().get(0), null); 3403 3404 listRewrite = rewrite.getListRewrite(method7, MethodDeclaration.PARAMETERS_PROPERTY); 3405 listRewrite.insertLast(paramCopy, null); 3406 3407 rewrite.remove(receiver8, null); 3408 rewrite.remove((SingleVariableDeclaration) method8.parameters().get(0), null); 3409 3410 rewrite.remove(receiver9, null); 3411 rewrite.remove((SingleVariableDeclaration) method9.parameters().get(0), null); 3412 3413 String preview = evaluateRewrite(cu, rewrite); 3414 buf = new StringBuffer(); 3415 buf.append("package test1;\n"); 3416 buf.append("class X {\n"); 3417 buf.append(" public void foo() {}\n"); 3418 buf.append(" public void foo(X this) {}\n"); 3419 buf.append(" public final void foo(/*comment*/ int i) {}\n"); 3420 buf.append(" public void foo(X this, int i, int j) {}\n"); 3421 buf.append(" public void foo(XY this) {}\n"); 3422 buf.append(" public void foo(XY this, float f2) {}\n"); 3423 buf.append(" public void foo(X this, int i, float f1) {}\n"); 3424 buf.append(" public void foo() {}\n"); 3425 buf.append(" public void foo1() {}\n"); 3426 buf.append("}\n"); 3427 assertEqualString(preview, buf.toString()); 3428 } _testReceiver4_since_8()3429 public void _testReceiver4_since_8() throws Exception { 3430 IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); 3431 StringBuffer buf = new StringBuffer(); 3432 buf.append("package test1;\n"); 3433 buf.append("class XYZ {\n"); 3434 buf.append(" class Y {\n"); 3435 buf.append(" public Y(@A XYZ XYZ.this, @B int i) {}\n"); 3436 buf.append(" public Y(@A XYZ XYZ.this, @B int i, @C int j) {}\n"); 3437 buf.append(" public Y(@A XYZ XYZ.this, @B float e) {}\n"); 3438 buf.append(" public Y(@A XYZ XYZ.this, @B float e, @C float f) {}\n"); 3439 buf.append(" public Y(@A XYZ XYZ.this, @B int i, @C float f) {}\n"); 3440 buf.append(" public Y(@A XYZ XYZ.this, @B float f, @C int i) {}\n"); 3441 buf.append(" public Y(@A XYZ XYZ.this, @B boolean b1) {}\n"); 3442 buf.append(" public Y(@A XYZ XYZ.this, @B boolean b2, @C int i) {}\n"); 3443 buf.append(" public Y(@B boolean b, @C float f) {}\n"); 3444 buf.append(" public Y(@B boolean b, @C boolean c) {}\n"); 3445 buf.append(" public Y(@B boolean b, String str) {}\n"); 3446 buf.append(" public Y(@A XYZ XYZ.this, @B int i, @C int j, @D int k) {}\n"); 3447 buf.append(" public Y(@A XYZ XYZ.this) {}\n"); 3448 buf.append(" }\n"); 3449 buf.append("}\n"); 3450 ICompilationUnit cu = pack1.createCompilationUnit("XYZ.java", buf.toString(), false, null); 3451 CompilationUnit astRoot = createAST(cu); 3452 ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); 3453 AST ast = astRoot.getAST(); 3454 TypeDeclaration type = (TypeDeclaration) findTypeDeclaration(astRoot, "XYZ").bodyDeclarations().get(0); 3455 MethodDeclaration method1 = (MethodDeclaration) type.bodyDeclarations().get(0); // Remove the receiver type and qualifying name 3456 MethodDeclaration method2 = (MethodDeclaration) type.bodyDeclarations().get(1); // Remove the receiver but not the qualifier 3457 MethodDeclaration method3 = (MethodDeclaration) type.bodyDeclarations().get(2); // Remove the qualifier only 3458 MethodDeclaration method4 = (MethodDeclaration) type.bodyDeclarations().get(3); // Remove the qualifier and receiver annotation 3459 MethodDeclaration method5 = (MethodDeclaration) type.bodyDeclarations().get(4); // Remove the receiver type and all parameters 3460 MethodDeclaration method6 = (MethodDeclaration) type.bodyDeclarations().get(5); // Remove the receiver type and add a param 3461 MethodDeclaration method7 = (MethodDeclaration) type.bodyDeclarations().get(6); // Remove the qualifier and remove a param 3462 MethodDeclaration method8 = (MethodDeclaration) type.bodyDeclarations().get(7); // Remove the qualifier and replace a param 3463 MethodDeclaration method9 = (MethodDeclaration) type.bodyDeclarations().get(8); // Add a receiver type and qualifier with annotation 3464 MethodDeclaration method10 = (MethodDeclaration) type.bodyDeclarations().get(9); // Add a receiver type and qualifier with annotations and add one parameter 3465 MethodDeclaration method11 = (MethodDeclaration) type.bodyDeclarations().get(10); // Add a receiver type with qualifier & annotations and remove all parameters 3466 MethodDeclaration method12 = (MethodDeclaration) type.bodyDeclarations().get(11); // Keep the receiver type and qualifier, but alter parameters 3467 MethodDeclaration method13 = (MethodDeclaration) type.bodyDeclarations().get(12); // Keep the receiver type and qualifier, but alter parameters 3468 3469 rewrite.set(method1, MethodDeclaration.RECEIVER_TYPE_PROPERTY, null, null); 3470 rewrite.set(method1, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, null, null); 3471 rewrite.set(method2, MethodDeclaration.RECEIVER_TYPE_PROPERTY, null, null); 3472 rewrite.set(method3, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, null, null); 3473 3474 rewrite.set(method4, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, null, null); 3475 ListRewrite listRewrite = rewrite.getListRewrite(method4.getReceiverType(), SimpleType.ANNOTATIONS_PROPERTY); 3476 listRewrite.remove((ASTNode) ((AnnotatableType) method4.getReceiverType()).annotations().get(0), null); 3477 3478 rewrite.set(method5, MethodDeclaration.RECEIVER_TYPE_PROPERTY, null, null); 3479 List params = method5.parameters(); 3480 SingleVariableDeclaration first = (SingleVariableDeclaration) params.get(0); 3481 listRewrite = rewrite.getListRewrite(method5, MethodDeclaration.PARAMETERS_PROPERTY); 3482 listRewrite.remove(first, null); 3483 first = (SingleVariableDeclaration) params.get(1); 3484 listRewrite.remove(first, null); 3485 3486 rewrite.set(method6, MethodDeclaration.RECEIVER_TYPE_PROPERTY, null, null); 3487 listRewrite = rewrite.getListRewrite(method6, MethodDeclaration.PARAMETERS_PROPERTY); 3488 SingleVariableDeclaration paramCopy = ast.newSingleVariableDeclaration(); 3489 SimpleType typeCopy = ast.newSimpleType(ast.newSimpleName("Object")); 3490 MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); 3491 markerAnnotation.setTypeName(ast.newSimpleName("A")); 3492 typeCopy.annotations().add(markerAnnotation); 3493 paramCopy.setType(typeCopy); 3494 paramCopy.setName(ast.newSimpleName("obj")); 3495 listRewrite.insertFirst(paramCopy, null); 3496 3497 rewrite.set(method7, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, null, null); 3498 params = method7.parameters(); 3499 first = (SingleVariableDeclaration) params.get(0); 3500 listRewrite = rewrite.getListRewrite(method7, MethodDeclaration.PARAMETERS_PROPERTY); 3501 listRewrite.remove(first, null); 3502 params = method8.parameters(); 3503 rewrite.set(method8, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, null, null); 3504 listRewrite = rewrite.getListRewrite(method8, MethodDeclaration.PARAMETERS_PROPERTY); 3505 listRewrite.remove((SingleVariableDeclaration) params.get(0), null); 3506 listRewrite.remove((SingleVariableDeclaration) params.get(1), null); 3507 listRewrite.insertLast(paramCopy, null); 3508 3509 SimpleType receiverType = ast.newSimpleType(ast.newSimpleName("XYZ")); 3510 SimpleName qual = ast.newSimpleName("XYZ"); 3511 markerAnnotation= ast.newMarkerAnnotation(); 3512 markerAnnotation.setTypeName(ast.newSimpleName("A")); 3513 receiverType.annotations().add(markerAnnotation); 3514 3515 rewrite.set(method9, MethodDeclaration.RECEIVER_TYPE_PROPERTY, receiverType, null); 3516 rewrite.set(method9, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, qual, null); 3517 3518 rewrite.set(method10, MethodDeclaration.RECEIVER_TYPE_PROPERTY, receiverType, null); 3519 rewrite.set(method10, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, qual, null); 3520 listRewrite = rewrite.getListRewrite(method10, MethodDeclaration.PARAMETERS_PROPERTY); 3521 listRewrite.insertFirst(paramCopy, null); 3522 3523 rewrite.set(method11, MethodDeclaration.RECEIVER_TYPE_PROPERTY, receiverType, null); 3524 rewrite.set(method11, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, qual, null); 3525 listRewrite = rewrite.getListRewrite(method11, MethodDeclaration.PARAMETERS_PROPERTY); 3526 listRewrite.remove((ASTNode) method11.parameters().get(0), null); 3527 listRewrite.remove((ASTNode) method11.parameters().get(1), null); 3528 3529 listRewrite = rewrite.getListRewrite(method12, MethodDeclaration.PARAMETERS_PROPERTY); 3530 listRewrite.remove((ASTNode) method12.parameters().get(1), null); 3531 3532 listRewrite = rewrite.getListRewrite(method13, MethodDeclaration.PARAMETERS_PROPERTY); 3533 listRewrite.insertFirst(paramCopy, null); 3534 3535 String preview = evaluateRewrite(cu, rewrite); 3536 buf = new StringBuffer(); 3537 buf.append("package test1;\n"); 3538 buf.append("class XYZ {\n"); 3539 buf.append(" class Y {\n"); 3540 buf.append(" public Y(@B int i) {}\n"); 3541 buf.append(" public Y(@B int i, @C int j) {}\n"); 3542 buf.append(" public Y(@A XYZ this, @B float e) {}\n"); 3543 buf.append(" public Y(XYZ this, @B float e, @C float f) {}\n"); 3544 buf.append(" public Y() {}\n"); 3545 buf.append(" public Y(@A Object obj, @B float f, @C int i) {}\n"); 3546 buf.append(" public Y(@A XYZ this) {}\n"); 3547 buf.append(" public Y(@A XYZ this, @A Object obj) {}\n"); 3548 buf.append(" public Y(@A XYZ XYZ.this, @B boolean b, @C float f) {}\n"); 3549 buf.append(" public Y(@A XYZ XYZ.this, @A Object obj, @B boolean b, @C boolean c) {}\n"); 3550 buf.append(" public Y(@A XYZ XYZ.this) {}\n"); 3551 buf.append(" public Y(@A XYZ XYZ.this, @B int i, @D int k) {}\n"); 3552 buf.append(" public Y(@A XYZ XYZ.this, @A Object obj) {}\n"); 3553 buf.append(" }\n"); 3554 buf.append("}\n"); 3555 assertEqualString(preview, buf.toString()); 3556 } testReceiver5_since_8()3557 public void testReceiver5_since_8() throws Exception { 3558 IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); 3559 StringBuffer buf = new StringBuffer(); 3560 buf.append("package test1;\n"); 3561 buf.append("class XYZ {\n"); 3562 buf.append(" class Y {\n"); 3563 buf.append(" public Y(@A Y Y.this, @B int i) {}\n"); 3564 buf.append(" public Y(@A XYZ this, @B int i, @C int j) {}\n"); 3565 buf.append(" public Y(@A XYZ Y.this, @B float e) {}\n"); 3566 buf.append(" public Y(@A XYZ Y.this, @B float e, @C float f) {}\n"); 3567 buf.append(" }\n"); 3568 buf.append("}\n"); 3569 ICompilationUnit cu = pack1.createCompilationUnit("XYZ.java", buf.toString(), false, null); 3570 CompilationUnit astRoot = createAST(cu); 3571 ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); 3572 AST ast = astRoot.getAST(); 3573 TypeDeclaration type = (TypeDeclaration) findTypeDeclaration(astRoot, "XYZ").bodyDeclarations().get(0); 3574 MethodDeclaration method1 = (MethodDeclaration) type.bodyDeclarations().get(0); // Change receiver type's child/children 3575 MethodDeclaration method2 = (MethodDeclaration) type.bodyDeclarations().get(1); // Insert receiver qualifier 3576 MethodDeclaration method3 = (MethodDeclaration) type.bodyDeclarations().get(2); // Replace receiver qualifier 3577 MethodDeclaration method4 = (MethodDeclaration) type.bodyDeclarations().get(3); // Change receiver qualifier's children 3578 3579 Name newName = ast.newSimpleName("XYZ"); 3580 rewrite.replace(((SimpleType) method1.getReceiverType()).getName(), newName, null); 3581 rewrite.replace(method1.getReceiverQualifier(), newName, null); 3582 rewrite.set(method2, MethodDeclaration.RECEIVER_QUALIFIER_PROPERTY, newName, null); 3583 rewrite.replace(method3.getReceiverQualifier(), newName, null); 3584 SimpleName qualifier = method4.getReceiverQualifier(); 3585 rewrite.set(qualifier, SimpleName.IDENTIFIER_PROPERTY, "XYZ", null); 3586 3587 String preview = evaluateRewrite(cu, rewrite); 3588 buf = new StringBuffer(); 3589 buf.append("package test1;\n"); 3590 buf.append("class XYZ {\n"); 3591 buf.append(" class Y {\n"); 3592 buf.append(" public Y(@A XYZ XYZ.this, @B int i) {}\n"); 3593 buf.append(" public Y(@A XYZ XYZ.this, @B int i, @C int j) {}\n"); 3594 buf.append(" public Y(@A XYZ XYZ.this, @B float e) {}\n"); 3595 buf.append(" public Y(@A XYZ XYZ.this, @B float e, @C float f) {}\n"); 3596 buf.append(" }\n"); 3597 buf.append("}\n"); 3598 assertEqualString(preview, buf.toString()); 3599 } 3600 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=403985 testBug403985_since_8()3601 public void testBug403985_since_8() throws Exception { 3602 String contents = 3603 "public interface X {\n" + 3604 " static void foo(){}\n" + 3605 " public default void foo(int i){}\n" + 3606 " public default int foo2(int i) { return 0;}\n" + 3607 " public void foo3(int i);\n" + 3608 " public default int foo4(int i) { return 0;}\n" + 3609 "}\n"; 3610 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 3611 ICompilationUnit cu= pack1.createCompilationUnit("X.java", contents, false, null); 3612 CompilationUnit astRoot= createAST(cu); 3613 AST ast = astRoot.getAST(); 3614 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 3615 TypeDeclaration type= findTypeDeclaration(astRoot, "X"); 3616 MethodDeclaration[] methods = type.getMethods(); 3617 assertEquals("Incorrect no of methods", 5, methods.length); 3618 MethodDeclaration method = methods[0]; 3619 { // Change default method to static and vice versa 3620 ListRewrite listRewrite = rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY); 3621 ASTNode newMod = ast.newModifier(ModifierKeyword.DEFAULT_KEYWORD); 3622 listRewrite.replace((ASTNode) method.modifiers().get(0), newMod, null); 3623 3624 method = methods[1]; 3625 listRewrite = rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY); 3626 newMod = ast.newModifier(ModifierKeyword.STATIC_KEYWORD); 3627 listRewrite.replace((ASTNode) method.modifiers().get(1), newMod, null); 3628 } 3629 { // Remove default and the body 3630 method = methods[2]; 3631 ListRewrite listRewrite = rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY); 3632 listRewrite.remove((ASTNode) method.modifiers().get(1), null); 3633 rewrite.set(method, MethodDeclaration.BODY_PROPERTY, null, null); 3634 } 3635 { // Add a default and body 3636 method = methods[3]; 3637 ListRewrite listRewrite = rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY); 3638 ASTNode newMod = ast.newModifier(ModifierKeyword.DEFAULT_KEYWORD); 3639 listRewrite.insertAt(newMod, 1, null); 3640 Block newBlock = ast.newBlock(); 3641 rewrite.set(method, MethodDeclaration.BODY_PROPERTY, newBlock, null); 3642 } 3643 { // Alter parameters for a default method 3644 method = methods[4]; 3645 ListRewrite listRewrite = rewrite.getListRewrite(method, MethodDeclaration.PARAMETERS_PROPERTY); 3646 listRewrite.remove((ASTNode) method.parameters().get(0), null); 3647 listRewrite = rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY); 3648 listRewrite.remove((ASTNode) method.modifiers().get(0), null); 3649 } 3650 String preview = evaluateRewrite(cu, rewrite); 3651 contents = 3652 "public interface X {\n" + 3653 " default void foo(){}\n" + 3654 " public static void foo(int i){}\n" + 3655 " public int foo2(int i);\n" + 3656 " public default void foo3(int i) {\n }\n" + 3657 " default int foo4() { return 0;}\n" + 3658 "}\n"; 3659 assertEqualString(preview, contents); 3660 } 3661 testReceiverParam_since_8()3662 public void testReceiverParam_since_8() throws Exception { 3663 IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); 3664 StringBuffer buf = new StringBuffer(); 3665 buf.append("package test1;\n"); 3666 buf.append("public abstract class E {\n"); 3667 buf.append(" public void foo() {\n"); 3668 buf.append(" }\n"); 3669 buf.append("\n"); 3670 buf.append("}\n"); 3671 ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); 3672 3673 CompilationUnit astRoot = createAST(cu); 3674 AST ast = astRoot.getAST(); 3675 ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); 3676 TypeDeclaration type = findTypeDeclaration(astRoot, "E"); 3677 3678 MethodDeclaration newMethodDeclaration = ast.newMethodDeclaration(); 3679 SimpleName methodName = ast.newSimpleName("bar"); 3680 SimpleType simpleType = ast.newSimpleType(ast.newSimpleName("E")); 3681 MarkerAnnotation annotationC = ast.newMarkerAnnotation(); 3682 annotationC.setTypeName(ast.newSimpleName("C")); 3683 simpleType.annotations().add(annotationC); 3684 newMethodDeclaration.setName(methodName); 3685 newMethodDeclaration.setReceiverType(simpleType); 3686 3687 MethodDeclaration[] methods = type.getMethods(); 3688 MethodDeclaration methodDeclaration = methods[0]; 3689 methodDeclaration.setReceiverType(ast.newSimpleType(ast.newSimpleName("E"))); 3690 3691 ListRewrite listRewrite = rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); 3692 listRewrite.insertLast(newMethodDeclaration, null); 3693 3694 String preview = evaluateRewrite(cu, rewrite); 3695 3696 buf = new StringBuffer(); 3697 buf.append("package test1;\n"); 3698 buf.append("public abstract class E {\n"); 3699 buf.append(" public void foo() {\n"); 3700 buf.append(" }\n"); 3701 buf.append("\n"); 3702 buf.append(" void bar(@C E this);\n"); 3703 buf.append("\n"); 3704 buf.append("}\n"); 3705 3706 assertEqualString(preview, buf.toString()); 3707 } 3708 testReceiverParam_InnerClass_since_8()3709 public void testReceiverParam_InnerClass_since_8() throws Exception { 3710 IPackageFragment pack1 = this.sourceFolder.createPackageFragment( 3711 "test1", false, null); 3712 StringBuffer buf = new StringBuffer(); 3713 buf.append("package test1;\n"); 3714 buf.append("public class E {\n"); 3715 buf.append(" public void foo() {\n"); 3716 buf.append(" }\n"); 3717 buf.append("\n"); 3718 buf.append(" class Inner{\n"); 3719 buf.append(" }\n"); 3720 buf.append("}\n"); 3721 ICompilationUnit cu = pack1.createCompilationUnit("E.java", 3722 buf.toString(), false, null); 3723 3724 CompilationUnit astRoot = createAST(cu); 3725 AST ast = astRoot.getAST(); 3726 ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); 3727 TypeDeclaration type = findTypeDeclaration(astRoot, "E"); 3728 TypeDeclaration inner = (TypeDeclaration) type.bodyDeclarations().get(1); 3729 3730 MethodDeclaration newMethodDeclaration = ast.newMethodDeclaration(); 3731 SimpleName methodName = ast.newSimpleName("Inner"); 3732 SimpleType simpleType = ast.newSimpleType(ast.newSimpleName("E")); 3733 MarkerAnnotation annotationC = ast.newMarkerAnnotation(); 3734 annotationC.setTypeName(ast.newSimpleName("C")); 3735 simpleType.annotations().add(annotationC); 3736 newMethodDeclaration.setName(methodName); 3737 newMethodDeclaration.setConstructor(true); 3738 newMethodDeclaration.setReceiverType(simpleType); 3739 newMethodDeclaration.setReceiverQualifier(ast.newSimpleName("E")); 3740 newMethodDeclaration.setBody(ast.newBlock()); 3741 3742 ListRewrite listRewrite = rewrite.getListRewrite(inner, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); 3743 listRewrite.insertLast(newMethodDeclaration, null); 3744 3745 String preview = evaluateRewrite(cu, rewrite); 3746 3747 buf = new StringBuffer(); 3748 buf.append("package test1;\n"); 3749 buf.append("public class E {\n"); 3750 buf.append(" public void foo() {\n"); 3751 buf.append(" }\n"); 3752 buf.append("\n"); 3753 buf.append(" class Inner{\n"); 3754 buf.append("\n"); 3755 buf.append(" Inner(@C E E.this) {\n"); 3756 buf.append(" }\n"); 3757 buf.append(" }\n"); 3758 buf.append("}\n"); 3759 3760 assertEqualString(preview, buf.toString()); 3761 } testBug427622a_since_8()3762 public void testBug427622a_since_8() throws Exception { 3763 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 3764 StringBuffer buf= new StringBuffer(); 3765 buf.append("package test1;\n"); 3766 buf.append("import java.lang.annotation.*;\n"); 3767 buf.append("public abstract class E {\n"); 3768 buf.append(" public void test(){}\n"); 3769 buf.append(" class MyException extends Throwable {\n"); 3770 buf.append(" private static final long serialVersionUID=-3045365361549263819L;"); 3771 buf.append(" }\n"); 3772 buf.append("}\n"); 3773 buf.append("@Target (Element.TYPE_USE);\n"); 3774 buf.append("@interface Marker {}\n"); 3775 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 3776 3777 CompilationUnit astRoot= createAST(cu); 3778 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 3779 AST ast= astRoot.getAST(); 3780 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 3781 3782 { // add an annotated simpletype as throws annotation 3783 MethodDeclaration methodDecl = findMethodDeclaration(type, "test"); 3784 SimpleType newThrownException = (SimpleType) createNewExceptionType(ast, "MyException"); 3785 MarkerAnnotation annot = ast.newMarkerAnnotation(); 3786 annot.setTypeName(ast.newSimpleName("Marker")); 3787 ListRewrite listRewrite = rewrite.getListRewrite(newThrownException, SimpleType.ANNOTATIONS_PROPERTY); 3788 listRewrite.insertFirst(annot, null); 3789 listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY); 3790 listRewrite.insertFirst(newThrownException, null); 3791 } 3792 3793 String preview= evaluateRewrite(cu, rewrite); 3794 3795 buf= new StringBuffer(); 3796 buf.append("package test1;\n"); 3797 buf.append("import java.lang.annotation.*;\n"); 3798 buf.append("public abstract class E {\n"); 3799 buf.append(" public void test() throws @Marker MyException{}\n"); 3800 buf.append(" class MyException extends Throwable {\n"); 3801 buf.append(" private static final long serialVersionUID=-3045365361549263819L;"); 3802 buf.append(" }\n"); 3803 buf.append("}\n"); 3804 buf.append("@Target (Element.TYPE_USE);\n"); 3805 buf.append("@interface Marker {}\n"); 3806 3807 assertEqualString(preview, buf.toString()); 3808 3809 // still no new line if new line after annotation on parameter is enabled: 3810 this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PARAMETER, JavaCore.INSERT); 3811 3812 preview= evaluateRewrite(cu, rewrite); 3813 assertEqualString(preview, buf.toString()); 3814 3815 // do insert new line if new line after type annotation is enabled: 3816 this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PARAMETER, JavaCore.DO_NOT_INSERT); 3817 this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_TYPE_ANNOTATION, JavaCore.INSERT); 3818 3819 preview= evaluateRewrite(cu, rewrite); 3820 3821 buf= new StringBuffer(); 3822 buf.append("package test1;\n"); 3823 buf.append("import java.lang.annotation.*;\n"); 3824 buf.append("public abstract class E {\n"); 3825 buf.append(" public void test() throws @Marker\n"); 3826 buf.append(" MyException{}\n"); 3827 buf.append(" class MyException extends Throwable {\n"); 3828 buf.append(" private static final long serialVersionUID=-3045365361549263819L;"); 3829 buf.append(" }\n"); 3830 buf.append("}\n"); 3831 buf.append("@Target (Element.TYPE_USE);\n"); 3832 buf.append("@interface Marker {}\n"); 3833 3834 assertEqualString(preview, buf.toString()); 3835 } 3836 testBug427622b_since_8()3837 public void testBug427622b_since_8() throws Exception { 3838 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 3839 StringBuffer buf= new StringBuffer(); 3840 buf.append("package test1;\n"); 3841 buf.append("import java.lang.annotation.*;\n"); 3842 buf.append("public abstract class E {\n"); 3843 buf.append(" public void test() throws MyException{}\n"); 3844 buf.append(" class MyException extends Throwable {\n"); 3845 buf.append(" private static final long serialVersionUID=-3045365361549263819L;"); 3846 buf.append(" }\n"); 3847 buf.append("}\n"); 3848 buf.append("@Target (Element.TYPE_USE);\n"); 3849 buf.append("@interface Marker {}\n"); 3850 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 3851 3852 CompilationUnit astRoot= createAST(cu); 3853 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 3854 AST ast= astRoot.getAST(); 3855 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 3856 3857 { // add an annotation to the simpletype 3858 MethodDeclaration methodDecl = findMethodDeclaration(type, "test"); 3859 SimpleType newThrownException = (SimpleType) methodDecl.thrownExceptionTypes().get(0); 3860 MarkerAnnotation annot = ast.newMarkerAnnotation(); 3861 annot.setTypeName(ast.newSimpleName("Marker")); 3862 ListRewrite listRewrite = rewrite.getListRewrite(newThrownException, SimpleType.ANNOTATIONS_PROPERTY); 3863 listRewrite.insertFirst(annot, null); 3864 } 3865 3866 String preview= evaluateRewrite(cu, rewrite); 3867 3868 buf= new StringBuffer(); 3869 buf.append("package test1;\n"); 3870 buf.append("import java.lang.annotation.*;\n"); 3871 buf.append("public abstract class E {\n"); 3872 buf.append(" public void test() throws @Marker MyException{}\n"); 3873 buf.append(" class MyException extends Throwable {\n"); 3874 buf.append(" private static final long serialVersionUID=-3045365361549263819L;"); 3875 buf.append(" }\n"); 3876 buf.append("}\n"); 3877 buf.append("@Target (Element.TYPE_USE);\n"); 3878 buf.append("@interface Marker {}\n"); 3879 3880 assertEqualString(preview, buf.toString()); 3881 } testBug427622c_since_8()3882 public void testBug427622c_since_8() throws Exception { 3883 IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); 3884 StringBuffer buf= new StringBuffer(); 3885 buf.append("package test1;\n"); 3886 buf.append("import java.lang.annotation.*;\n"); 3887 buf.append("public abstract class E {\n"); 3888 buf.append(" public void test(){}\n"); 3889 buf.append(" class MyException extends Throwable {\n"); 3890 buf.append(" private static final long serialVersionUID=-3045365361549263819L;"); 3891 buf.append(" }\n"); 3892 buf.append("}\n"); 3893 buf.append("@Target (Element.TYPE_USE);\n"); 3894 buf.append("@interface Marker {}\n"); 3895 ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); 3896 3897 CompilationUnit astRoot= createAST(cu); 3898 ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); 3899 AST ast= astRoot.getAST(); 3900 TypeDeclaration type= findTypeDeclaration(astRoot, "E"); 3901 3902 { // add an annotated simpletype as throws annotation 3903 MethodDeclaration methodDecl = findMethodDeclaration(type, "test"); 3904 ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY); 3905 SimpleType newThrownException = (SimpleType) createNewExceptionType(ast, "MyException"); 3906 listRewrite.insertFirst(newThrownException, null); 3907 MarkerAnnotation annot = ast.newMarkerAnnotation(); 3908 annot.setTypeName(ast.newSimpleName("Marker")); 3909 listRewrite = rewrite.getListRewrite(newThrownException, SimpleType.ANNOTATIONS_PROPERTY); 3910 listRewrite.insertFirst(annot, null); 3911 } 3912 3913 String preview= evaluateRewrite(cu, rewrite); 3914 3915 buf= new StringBuffer(); 3916 buf.append("package test1;\n"); 3917 buf.append("import java.lang.annotation.*;\n"); 3918 buf.append("public abstract class E {\n"); 3919 buf.append(" public void test() throws @Marker MyException{}\n"); 3920 buf.append(" class MyException extends Throwable {\n"); 3921 buf.append(" private static final long serialVersionUID=-3045365361549263819L;"); 3922 buf.append(" }\n"); 3923 buf.append("}\n"); 3924 buf.append("@Target (Element.TYPE_USE);\n"); 3925 buf.append("@interface Marker {}\n"); 3926 3927 assertEqualString(preview, buf.toString()); 3928 } 3929 } 3930