1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 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.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 
20 import junit.framework.Test;
21 
22 import org.eclipse.jdt.core.ICompilationUnit;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.dom.AST;
25 import org.eclipse.jdt.core.dom.ASTNode;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
27 import org.eclipse.jdt.core.dom.FieldDeclaration;
28 import org.eclipse.jdt.core.dom.MethodDeclaration;
29 import org.eclipse.jdt.core.dom.PrimitiveType;
30 import org.eclipse.jdt.core.dom.TypeDeclaration;
31 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
32 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
33 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
34 import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer;
35 import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange;
36 
37 @SuppressWarnings({"rawtypes", "unchecked"})
38 public class ASTRewritingInsertBoundTest extends ASTRewritingTest {
39 
ASTRewritingInsertBoundTest(String name)40 	public ASTRewritingInsertBoundTest(String name) {
41 		super(name);
42 	}
43 
ASTRewritingInsertBoundTest(String name, int apiLevel)44 	public ASTRewritingInsertBoundTest(String name, int apiLevel) {
45 		super(name, apiLevel);
46 	}
47 
suite()48 	public static Test suite() {
49 		return createSuite(ASTRewritingInsertBoundTest.class);
50 	}
51 
52 	/** @deprecated using deprecated code */
newMethodDeclaration(AST ast, String name)53 	private MethodDeclaration newMethodDeclaration(AST ast, String name) {
54 		MethodDeclaration decl= ast.newMethodDeclaration();
55 		decl.setName(ast.newSimpleName(name));
56 		decl.setBody(null);
57 		decl.setReturnType(ast.newPrimitiveType(PrimitiveType.VOID));
58 		return decl;
59 	}
60 
newFieldDeclaration(AST ast, String name)61 	private FieldDeclaration newFieldDeclaration(AST ast, String name) {
62 		VariableDeclarationFragment frag= ast.newVariableDeclarationFragment();
63 		frag.setName(ast.newSimpleName(name));
64 		FieldDeclaration decl= ast.newFieldDeclaration(frag);
65 		decl.setType(ast.newPrimitiveType(PrimitiveType.INT));
66 		return decl;
67 	}
68 
69 
70 	/** @deprecated using deprecated code */
testInsert1_only_2()71 	public void testInsert1_only_2() throws Exception {
72 		// insert first and last
73 
74 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
75 		StringBuffer buf= new StringBuffer();
76 		buf.append("package test1;\n");
77 		buf.append("public class C {\n");
78 		buf.append("//c1\n");
79 		buf.append("\n");
80 		buf.append("    public void foo1();\n");
81 		buf.append("\n");
82 		buf.append("//c2\n");
83 		buf.append("\n");
84 		buf.append("    public void foo2();\n");
85 		buf.append("\n");
86 		buf.append("//c3\n");
87 		buf.append("\n");
88 		buf.append("    public void foo3();\n");
89 		buf.append("\n");
90 		buf.append("//c4\n");
91 		buf.append("}\n");
92 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
93 
94 		CompilationUnit astRoot= createAST(cu);
95 		AST ast= astRoot.getAST();
96 
97 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
98 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
99 
100 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
101 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
102 
103 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
104 		listRewrite.insertFirst(decl1, null);
105 		listRewrite.insertLast(decl2, null);
106 
107 		String preview= evaluateRewrite(cu, rewrite);
108 
109 		buf= new StringBuffer();
110 		buf.append("package test1;\n");
111 		buf.append("public class C {\n");
112 		buf.append("//c1\n");
113 		buf.append("\n");
114 		buf.append("    void new1();\n");
115 		buf.append("\n");
116 		buf.append("    public void foo1();\n");
117 		buf.append("\n");
118 		buf.append("//c2\n");
119 		buf.append("\n");
120 		buf.append("    public void foo2();\n");
121 		buf.append("\n");
122 		buf.append("//c3\n");
123 		buf.append("\n");
124 		buf.append("    public void foo3();\n");
125 		buf.append("\n");
126 		buf.append("    void new2();\n");
127 		buf.append("\n");
128 		buf.append("//c4\n");
129 		buf.append("}\n");
130 
131 		assertEqualString(preview, buf.toString());
132 
133 	}
134 
135 	/** @deprecated using deprecated code */
testInsert3_only_2()136 	public void testInsert3_only_2() throws Exception {
137 		// insert 2 x before
138 
139 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
140 		StringBuffer buf= new StringBuffer();
141 		buf.append("package test1;\n");
142 		buf.append("public class C {\n");
143 		buf.append("//c1\n");
144 		buf.append("\n");
145 		buf.append("    public void foo1();\n");
146 		buf.append("\n");
147 		buf.append("//c2\n");
148 		buf.append("\n");
149 		buf.append("    public void foo2();\n");
150 		buf.append("\n");
151 		buf.append("//c3\n");
152 		buf.append("\n");
153 		buf.append("    public void foo3();\n");
154 		buf.append("\n");
155 		buf.append("//c4\n");
156 		buf.append("}\n");
157 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
158 
159 		CompilationUnit astRoot= createAST(cu);
160 		AST ast= astRoot.getAST();
161 
162 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
163 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
164 		List decls= type.bodyDeclarations();
165 
166 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
167 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
168 
169 		ASTNode middleDecl= (ASTNode) decls.get(1);
170 		ASTNode lastDecl= (ASTNode) decls.get(2);
171 
172 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
173 		listRewrite.insertBefore(decl1, middleDecl, null);
174 		listRewrite.insertBefore(decl2, lastDecl, null);
175 
176 		String preview= evaluateRewrite(cu, rewrite);
177 
178 		buf= new StringBuffer();
179 		buf.append("package test1;\n");
180 		buf.append("public class C {\n");
181 		buf.append("//c1\n");
182 		buf.append("\n");
183 		buf.append("    public void foo1();\n");
184 		buf.append("\n");
185 		buf.append("//c2\n");
186 		buf.append("\n");
187 		buf.append("    void new1();\n");
188 		buf.append("\n");
189 		buf.append("    public void foo2();\n");
190 		buf.append("\n");
191 		buf.append("//c3\n");
192 		buf.append("\n");
193 		buf.append("    void new2();\n");
194 		buf.append("\n");
195 		buf.append("    public void foo3();\n");
196 		buf.append("\n");
197 		buf.append("//c4\n");
198 		buf.append("}\n");
199 
200 		assertEqualString(preview, buf.toString());
201 
202 	}
203 
204 
testInsert2_only_2()205 	public void testInsert2_only_2() throws Exception {
206 		// insert 2 x first and 2 x last
207 
208 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
209 		StringBuffer buf= new StringBuffer();
210 		buf.append("package test1;\n");
211 		buf.append("public class C {\n");
212 		buf.append("//c1\n");
213 		buf.append("\n");
214 		buf.append("    public void foo1();\n");
215 		buf.append("\n");
216 		buf.append("//c2\n");
217 		buf.append("\n");
218 		buf.append("    public void foo2();\n");
219 		buf.append("\n");
220 		buf.append("//c3\n");
221 		buf.append("\n");
222 		buf.append("    public void foo3();\n");
223 		buf.append("\n");
224 		buf.append("//c4\n");
225 		buf.append("}\n");
226 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
227 
228 		CompilationUnit astRoot= createAST(cu);
229 		AST ast= astRoot.getAST();
230 
231 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
232 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
233 
234 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
235 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
236 		MethodDeclaration decl3= newMethodDeclaration(ast, "new3");
237 		MethodDeclaration decl4= newMethodDeclaration(ast, "new4");
238 
239 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
240 		listRewrite.insertFirst(decl1, null);
241 		listRewrite.insertAfter(decl2, decl1, null);
242 		listRewrite.insertLast(decl3, null);
243 		listRewrite.insertAfter(decl4, decl3, null);
244 
245 		String preview= evaluateRewrite(cu, rewrite);
246 
247 		buf= new StringBuffer();
248 		buf.append("package test1;\n");
249 		buf.append("public class C {\n");
250 		buf.append("//c1\n");
251 		buf.append("\n");
252 		buf.append("    void new1();\n");
253 		buf.append("\n");
254 		buf.append("    void new2();\n");
255 		buf.append("\n");
256 		buf.append("    public void foo1();\n");
257 		buf.append("\n");
258 		buf.append("//c2\n");
259 		buf.append("\n");
260 		buf.append("    public void foo2();\n");
261 		buf.append("\n");
262 		buf.append("//c3\n");
263 		buf.append("\n");
264 		buf.append("    public void foo3();\n");
265 		buf.append("\n");
266 		buf.append("    void new3();\n");
267 		buf.append("\n");
268 		buf.append("    void new4();\n");
269 		buf.append("\n");
270 		buf.append("//c4\n");
271 		buf.append("}\n");
272 
273 		assertEqualString(preview, buf.toString());
274 
275 	}
276 
testInsert1Before_only_2_3_4()277 	public void testInsert1Before_only_2_3_4() throws Exception {
278 		// insert first and last
279 
280 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
281 		StringBuffer buf= new StringBuffer();
282 		buf.append("package test1;\n");
283 		buf.append("public class C {\n");
284 		buf.append("//c1\n");
285 		buf.append("\n");
286 		buf.append("    public int x1;\n");
287 		buf.append("\n");
288 		buf.append("//c2\n");
289 		buf.append("\n");
290 		buf.append("    public int x2;\n");
291 		buf.append("\n");
292 		buf.append("//c3\n");
293 		buf.append("\n");
294 		buf.append("    public int x3;\n");
295 		buf.append("\n");
296 		buf.append("//c4\n");
297 		buf.append("}\n");
298 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
299 
300 		CompilationUnit astRoot= createAST(cu);
301 		AST ast= astRoot.getAST();
302 
303 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
304 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
305 
306 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
307 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
308 
309 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
310 		listRewrite.insertFirst(decl1, null);
311 		listRewrite.insertLast(decl2, null);
312 
313 		String preview= evaluateRewrite(cu, rewrite);
314 
315 		buf= new StringBuffer();
316 		buf.append("package test1;\n");
317 		buf.append("public class C {\n");
318 		buf.append("//c1\n");
319 		buf.append("\n");
320 		buf.append("    int new1;\n");
321 		buf.append("\n");
322 		buf.append("    public int x1;\n");
323 		buf.append("\n");
324 		buf.append("//c2\n");
325 		buf.append("\n");
326 		buf.append("    public int x2;\n");
327 		buf.append("\n");
328 		buf.append("//c3\n");
329 		buf.append("\n");
330 		buf.append("    public int x3;\n");
331 		buf.append("\n");
332 		buf.append("    int new2;\n");
333 		buf.append("\n");
334 		buf.append("//c4\n");
335 		buf.append("}\n");
336 		assertEqualString(preview, buf.toString());
337 
338 	}
339 
testInsert2Before_only_2_3_4()340 	public void testInsert2Before_only_2_3_4() throws Exception {
341 		// insert 2x first and 2 x last
342 
343 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
344 		StringBuffer buf= new StringBuffer();
345 		buf.append("package test1;\n");
346 		buf.append("public class C {\n");
347 		buf.append("//c1\n");
348 		buf.append("\n");
349 		buf.append("    public int x1;\n");
350 		buf.append("\n");
351 		buf.append("//c2\n");
352 		buf.append("\n");
353 		buf.append("    public int x2;\n");
354 		buf.append("\n");
355 		buf.append("//c3\n");
356 		buf.append("\n");
357 		buf.append("    public int x3;\n");
358 		buf.append("\n");
359 		buf.append("//c4\n");
360 		buf.append("}\n");
361 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
362 
363 		CompilationUnit astRoot= createAST(cu);
364 		AST ast= astRoot.getAST();
365 
366 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
367 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
368 
369 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
370 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
371 		FieldDeclaration decl3= newFieldDeclaration(ast, "new3");
372 		FieldDeclaration decl4= newFieldDeclaration(ast, "new4");
373 
374 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
375 		listRewrite.insertFirst(decl1, null);
376 		listRewrite.insertAfter(decl2, decl1, null);
377 		listRewrite.insertLast(decl3, null);
378 		listRewrite.insertAfter(decl4, decl3, null);
379 
380 
381 		String preview= evaluateRewrite(cu, rewrite);
382 
383 		buf= new StringBuffer();
384 		buf.append("package test1;\n");
385 		buf.append("public class C {\n");
386 		buf.append("//c1\n");
387 		buf.append("\n");
388 		buf.append("    int new1;\n");
389 		buf.append("\n");
390 		buf.append("    int new2;\n");
391 		buf.append("\n");
392 		buf.append("    public int x1;\n");
393 		buf.append("\n");
394 		buf.append("//c2\n");
395 		buf.append("\n");
396 		buf.append("    public int x2;\n");
397 		buf.append("\n");
398 		buf.append("//c3\n");
399 		buf.append("\n");
400 		buf.append("    public int x3;\n");
401 		buf.append("\n");
402 		buf.append("    int new3;\n");
403 		buf.append("\n");
404 		buf.append("    int new4;\n");
405 		buf.append("\n");
406 		buf.append("//c4\n");
407 		buf.append("}\n");
408 
409 		assertEqualString(preview, buf.toString());
410 
411 	}
412 
testInsert3Before_only_2_3_4()413 	public void testInsert3Before_only_2_3_4() throws Exception {
414 		// insert 2 x after
415 
416 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
417 		StringBuffer buf= new StringBuffer();
418 		buf.append("package test1;\n");
419 		buf.append("public class C {\n");
420 		buf.append("//c1\n");
421 		buf.append("\n");
422 		buf.append("    public int x1;\n");
423 		buf.append("\n");
424 		buf.append("//c2\n");
425 		buf.append("\n");
426 		buf.append("    public int x2;\n");
427 		buf.append("\n");
428 		buf.append("//c3\n");
429 		buf.append("\n");
430 		buf.append("    public int x3;\n");
431 		buf.append("\n");
432 		buf.append("//c4\n");
433 		buf.append("}\n");
434 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
435 
436 		CompilationUnit astRoot= createAST(cu);
437 		AST ast= astRoot.getAST();
438 
439 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
440 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
441 		List decls= type.bodyDeclarations();
442 
443 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
444 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
445 
446 		ASTNode firstDecl= (ASTNode) decls.get(0);
447 		ASTNode middleDecl= (ASTNode) decls.get(1);
448 
449 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
450 		listRewrite.insertAfter(decl1, firstDecl, null);
451 		listRewrite.insertAfter(decl2, middleDecl, null);
452 
453 		String preview= evaluateRewrite(cu, rewrite);
454 
455 		buf= new StringBuffer();
456 		buf.append("package test1;\n");
457 		buf.append("public class C {\n");
458 		buf.append("//c1\n");
459 		buf.append("\n");
460 		buf.append("    public int x1;\n");
461 		buf.append("\n");
462 		buf.append("    int new1;\n");
463 		buf.append("\n");
464 		buf.append("//c2\n");
465 		buf.append("\n");
466 		buf.append("    public int x2;\n");
467 		buf.append("\n");
468 		buf.append("    int new2;\n");
469 		buf.append("\n");
470 		buf.append("//c3\n");
471 		buf.append("\n");
472 		buf.append("    public int x3;\n");
473 		buf.append("\n");
474 		buf.append("//c4\n");
475 		buf.append("}\n");
476 		assertEqualString(preview, buf.toString());
477 
478 	}
479 
testRemove1()480 	public void testRemove1() throws Exception {
481 		// remove first and last
482 
483 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
484 		StringBuffer buf= new StringBuffer();
485 		buf.append("package test1;\n");
486 		buf.append("public class C {\n");
487 		buf.append("//c1\n");
488 		buf.append("\n");
489 		buf.append("    public void foo1();\n");
490 		buf.append("\n");
491 		buf.append("//c2\n");
492 		buf.append("\n");
493 		buf.append("    public void foo2();\n");
494 		buf.append("\n");
495 		buf.append("//c3\n");
496 		buf.append("\n");
497 		buf.append("    public void foo3();\n");
498 		buf.append("\n");
499 		buf.append("//c4\n");
500 		buf.append("}\n");
501 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
502 
503 		CompilationUnit astRoot= createAST(cu);
504 
505 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
506 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
507 		List decls= type.bodyDeclarations();
508 
509 		rewrite.remove((ASTNode) decls.get(0), null);
510 		rewrite.remove((ASTNode) decls.get(2), null);
511 
512 		String preview= evaluateRewrite(cu, rewrite);
513 
514 		buf= new StringBuffer();
515 		buf.append("package test1;\n");
516 		buf.append("public class C {\n");
517 		buf.append("//c1\n");
518 		buf.append("\n");
519 		buf.append("    \n");
520 		buf.append("\n");
521 		buf.append("//c2\n");
522 		buf.append("\n");
523 		buf.append("    public void foo2();\n");
524 		buf.append("\n");
525 		buf.append("//c3\n");
526 		buf.append("\n");
527 		buf.append("\n");
528 		buf.append("//c4\n");
529 		buf.append("}\n");
530 
531 		assertEqualString(preview, buf.toString());
532 
533 	}
534 
testRemove2()535 	public void testRemove2() throws Exception {
536 		// remove second
537 
538 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
539 		StringBuffer buf= new StringBuffer();
540 		buf.append("package test1;\n");
541 		buf.append("public class C {\n");
542 		buf.append("//c1\n");
543 		buf.append("\n");
544 		buf.append("    public void foo1();\n");
545 		buf.append("\n");
546 		buf.append("//c2\n");
547 		buf.append("\n");
548 		buf.append("    public void foo2();\n");
549 		buf.append("\n");
550 		buf.append("//c3\n");
551 		buf.append("\n");
552 		buf.append("    public void foo3();\n");
553 		buf.append("\n");
554 		buf.append("//c4\n");
555 		buf.append("}\n");
556 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
557 
558 		CompilationUnit astRoot= createAST(cu);
559 
560 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
561 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
562 		List decls= type.bodyDeclarations();
563 
564 		rewrite.remove((ASTNode) decls.get(1), null);
565 
566 		String preview= evaluateRewrite(cu, rewrite);
567 
568 		buf= new StringBuffer();
569 		buf.append("package test1;\n");
570 		buf.append("public class C {\n");
571 		buf.append("//c1\n");
572 		buf.append("\n");
573 		buf.append("    public void foo1();\n");
574 		buf.append("\n");
575 		buf.append("//c2\n");
576 		buf.append("\n");
577 		buf.append("    \n");
578 		buf.append("\n");
579 		buf.append("//c3\n");
580 		buf.append("\n");
581 		buf.append("    public void foo3();\n");
582 		buf.append("\n");
583 		buf.append("//c4\n");
584 		buf.append("}\n");
585 
586 		assertEqualString(preview, buf.toString());
587 
588 	}
589 
testRemove3()590 	public void testRemove3() throws Exception {
591 		// remove 2nd and 3rd
592 
593 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
594 		StringBuffer buf= new StringBuffer();
595 		buf.append("package test1;\n");
596 		buf.append("public class C {\n");
597 		buf.append("//c1\n");
598 		buf.append("\n");
599 		buf.append("    public void foo1();\n");
600 		buf.append("\n");
601 		buf.append("//c2\n");
602 		buf.append("\n");
603 		buf.append("    public void foo2();\n");
604 		buf.append("\n");
605 		buf.append("//c3\n");
606 		buf.append("\n");
607 		buf.append("    public void foo3();\n");
608 		buf.append("\n");
609 		buf.append("//c4\n");
610 		buf.append("}\n");
611 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
612 
613 		CompilationUnit astRoot= createAST(cu);
614 
615 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
616 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
617 		List decls= type.bodyDeclarations();
618 
619 		rewrite.remove((ASTNode) decls.get(1), null);
620 		rewrite.remove((ASTNode) decls.get(2), null);
621 
622 		String preview= evaluateRewrite(cu, rewrite);
623 
624 		buf= new StringBuffer();
625 		buf.append("package test1;\n");
626 		buf.append("public class C {\n");
627 		buf.append("//c1\n");
628 		buf.append("\n");
629 		buf.append("    public void foo1();\n");
630 		buf.append("\n");
631 		buf.append("//c2\n");
632 		buf.append("\n");
633 		buf.append("\n");
634 		buf.append("//c3\n");
635 		buf.append("\n");
636 		buf.append("\n");
637 		buf.append("//c4\n");
638 		buf.append("}\n");
639 
640 		assertEqualString(preview, buf.toString());
641 
642 	}
643 
testRemove4()644 	public void testRemove4() throws Exception {
645 		// remove all
646 
647 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
648 		StringBuffer buf= new StringBuffer();
649 		buf.append("package test1;\n");
650 		buf.append("public class C {\n");
651 		buf.append("//c1\n");
652 		buf.append("\n");
653 		buf.append("    public void foo1();\n");
654 		buf.append("\n");
655 		buf.append("//c2\n");
656 		buf.append("\n");
657 		buf.append("    public void foo2();\n");
658 		buf.append("\n");
659 		buf.append("//c3\n");
660 		buf.append("\n");
661 		buf.append("    public void foo3();\n");
662 		buf.append("\n");
663 		buf.append("//c4\n");
664 		buf.append("}\n");
665 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
666 
667 		CompilationUnit astRoot= createAST(cu);
668 
669 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
670 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
671 		List decls= type.bodyDeclarations();
672 
673 		rewrite.remove((ASTNode) decls.get(0), null);
674 		rewrite.remove((ASTNode) decls.get(1), null);
675 		rewrite.remove((ASTNode) decls.get(2), null);
676 
677 		String preview= evaluateRewrite(cu, rewrite);
678 
679 		buf= new StringBuffer();
680 		buf.append("package test1;\n");
681 		buf.append("public class C {\n");
682 		buf.append("//c1\n");
683 		buf.append("\n");
684 		buf.append("    \n");
685 		buf.append("\n");
686 		buf.append("//c2\n");
687 		buf.append("\n");
688 		buf.append("    \n");
689 		buf.append("\n");
690 		buf.append("//c3\n");
691 		buf.append("\n");
692 		buf.append("    \n");
693 		buf.append("\n");
694 		buf.append("//c4\n");
695 		buf.append("}\n");
696 
697 		assertEqualString(preview, buf.toString());
698 
699 	}
700 
701 
testRemoveInsert1_only_2()702 	public void testRemoveInsert1_only_2() throws Exception {
703 		// remove first add before first, remove last add after last
704 
705 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
706 		StringBuffer buf= new StringBuffer();
707 		buf.append("package test1;\n");
708 		buf.append("public class C {\n");
709 		buf.append("//c1\n");
710 		buf.append("\n");
711 		buf.append("    public void foo1();\n");
712 		buf.append("\n");
713 		buf.append("//c2\n");
714 		buf.append("\n");
715 		buf.append("    public void foo2();\n");
716 		buf.append("\n");
717 		buf.append("//c3\n");
718 		buf.append("\n");
719 		buf.append("    public void foo3();\n");
720 		buf.append("\n");
721 		buf.append("//c4\n");
722 		buf.append("}\n");
723 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
724 
725 		CompilationUnit astRoot= createAST(cu);
726 		AST ast= astRoot.getAST();
727 
728 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
729 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
730 		List decls= type.bodyDeclarations();
731 
732 		rewrite.remove((ASTNode) decls.get(0), null);
733 		rewrite.remove((ASTNode) decls.get(2), null);
734 
735 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
736 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
737 
738 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
739 		listRewrite.insertFirst(decl1, null);
740 		listRewrite.insertLast(decl2, null);
741 
742 		String preview= evaluateRewrite(cu, rewrite);
743 
744 		buf= new StringBuffer();
745 		buf.append("package test1;\n");
746 		buf.append("public class C {\n");
747 		buf.append("//c1\n");
748 		buf.append("\n");
749 		buf.append("    void new1();\n");
750 		buf.append("\n");
751 		buf.append("    \n");
752 		buf.append("\n");
753 		buf.append("//c2\n");
754 		buf.append("\n");
755 		buf.append("    public void foo2();\n");
756 		buf.append("\n");
757 		buf.append("//c3\n");
758 		buf.append("\n");
759 		buf.append("    void new2();\n");
760 		buf.append("\n");
761 		buf.append("//c4\n");
762 		buf.append("}\n");
763 
764 		assertEqualString(preview, buf.toString());
765 
766 	}
767 
testRemoveInsert2_only_2()768 	public void testRemoveInsert2_only_2() throws Exception {
769 		// remove first add 2x first, remove last add 2x  last
770 
771 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
772 		StringBuffer buf= new StringBuffer();
773 		buf.append("package test1;\n");
774 		buf.append("public class C {\n");
775 		buf.append("//c1\n");
776 		buf.append("\n");
777 		buf.append("    public void foo1();\n");
778 		buf.append("\n");
779 		buf.append("//c2\n");
780 		buf.append("\n");
781 		buf.append("    public void foo2();\n");
782 		buf.append("\n");
783 		buf.append("//c3\n");
784 		buf.append("\n");
785 		buf.append("    public void foo3();\n");
786 		buf.append("\n");
787 		buf.append("//c4\n");
788 		buf.append("}\n");
789 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
790 
791 		CompilationUnit astRoot= createAST(cu);
792 		AST ast= astRoot.getAST();
793 
794 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
795 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
796 		List decls= type.bodyDeclarations();
797 
798 		rewrite.remove((ASTNode) decls.get(0), null);
799 		rewrite.remove((ASTNode) decls.get(2), null);
800 
801 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
802 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
803 		MethodDeclaration decl3= newMethodDeclaration(ast, "new3");
804 		MethodDeclaration decl4= newMethodDeclaration(ast, "new4");
805 
806 		ASTNode firstDecl= (ASTNode) decls.get(0);
807 
808 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
809 		listRewrite.insertBefore(decl1, firstDecl, null);
810 		listRewrite.insertAfter(decl2, firstDecl, null);
811 		listRewrite.insertLast(decl3, null);
812 		listRewrite.insertAfter(decl4, decl3, null);
813 
814 
815 		String preview= evaluateRewrite(cu, rewrite);
816 
817 		buf= new StringBuffer();
818 		buf.append("package test1;\n");
819 		buf.append("public class C {\n");
820 		buf.append("//c1\n");
821 		buf.append("\n");
822 		buf.append("    void new1();\n");
823 		buf.append("\n");
824 		buf.append("    void new2();\n");
825 		buf.append("\n");
826 		buf.append("    \n");
827 		buf.append("\n");
828 		buf.append("//c2\n");
829 		buf.append("\n");
830 		buf.append("    public void foo2();\n");
831 		buf.append("\n");
832 		buf.append("//c3\n");
833 		buf.append("\n");
834 		buf.append("    void new3();\n");
835 		buf.append("\n");
836 		buf.append("    void new4();\n");
837 		buf.append("\n");
838 		buf.append("//c4\n");
839 		buf.append("}\n");
840 
841 		assertEqualString(preview, buf.toString());
842 
843 	}
844 
testRemoveInsert3_only_2()845 	public void testRemoveInsert3_only_2() throws Exception {
846 		// remove middle, add before, add after
847 
848 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
849 		StringBuffer buf= new StringBuffer();
850 		buf.append("package test1;\n");
851 		buf.append("public class C {\n");
852 		buf.append("//c1\n");
853 		buf.append("\n");
854 		buf.append("    public void foo1();\n");
855 		buf.append("\n");
856 		buf.append("//c2\n");
857 		buf.append("\n");
858 		buf.append("    public void foo2();\n");
859 		buf.append("\n");
860 		buf.append("//c3\n");
861 		buf.append("\n");
862 		buf.append("    public void foo3();\n");
863 		buf.append("\n");
864 		buf.append("//c4\n");
865 		buf.append("}\n");
866 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
867 
868 		CompilationUnit astRoot= createAST(cu);
869 		AST ast= astRoot.getAST();
870 
871 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
872 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
873 		List decls= type.bodyDeclarations();
874 
875 		rewrite.remove((ASTNode) decls.get(1), null);
876 
877 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
878 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
879 
880 		ASTNode middleDecl= (ASTNode) decls.get(1);
881 
882 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
883 		listRewrite.insertBefore(decl1, middleDecl, null);
884 		listRewrite.insertAfter(decl2, middleDecl, null);
885 
886 		String preview= evaluateRewrite(cu, rewrite);
887 
888 		buf= new StringBuffer();
889 		buf.append("package test1;\n");
890 		buf.append("public class C {\n");
891 		buf.append("//c1\n");
892 		buf.append("\n");
893 		buf.append("    public void foo1();\n");
894 		buf.append("\n");
895 		buf.append("//c2\n");
896 		buf.append("\n");
897 		buf.append("    void new1();\n");
898 		buf.append("\n");
899 		buf.append("    void new2();\n");
900 		buf.append("\n");
901 		buf.append("    \n");
902 		buf.append("\n");
903 		buf.append("//c3\n");
904 		buf.append("\n");
905 		buf.append("    public void foo3();\n");
906 		buf.append("\n");
907 		buf.append("//c4\n");
908 		buf.append("}\n");
909 
910 		assertEqualString(preview, buf.toString());
911 
912 	}
913 
914 
testRemoveInsert1Before_only_2_3_4()915 	public void testRemoveInsert1Before_only_2_3_4() throws Exception {
916 		// remove first add before first, remove last add after last
917 
918 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
919 		StringBuffer buf= new StringBuffer();
920 		buf.append("package test1;\n");
921 		buf.append("public class C {\n");
922 		buf.append("//c1\n");
923 		buf.append("\n");
924 		buf.append("    public int x1;\n");
925 		buf.append("\n");
926 		buf.append("//c2\n");
927 		buf.append("\n");
928 		buf.append("    public int x2;\n");
929 		buf.append("\n");
930 		buf.append("//c3\n");
931 		buf.append("\n");
932 		buf.append("    public int x3;\n");
933 		buf.append("\n");
934 		buf.append("//c4\n");
935 		buf.append("}\n");
936 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
937 
938 		CompilationUnit astRoot= createAST(cu);
939 		AST ast= astRoot.getAST();
940 
941 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
942 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
943 		List decls= type.bodyDeclarations();
944 
945 		rewrite.remove((ASTNode) decls.get(0), null);
946 		rewrite.remove((ASTNode) decls.get(2), null);
947 
948 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
949 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
950 
951 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
952 		listRewrite.insertFirst(decl1, null);
953 		listRewrite.insertLast(decl2, null);
954 
955 
956 		String preview= evaluateRewrite(cu, rewrite);
957 
958 		buf= new StringBuffer();
959 		buf.append("package test1;\n");
960 		buf.append("public class C {\n");
961 		buf.append("//c1\n");
962 		buf.append("\n");
963 		buf.append("    int new1;\n");
964 		buf.append("\n");
965 		buf.append("    \n");
966 		buf.append("\n");
967 		buf.append("//c2\n");
968 		buf.append("\n");
969 		buf.append("    public int x2;\n");
970 		buf.append("\n");
971 		buf.append("//c3\n");
972 		buf.append("\n");
973 		buf.append("    int new2;\n");
974 		buf.append("\n");
975 		buf.append("//c4\n");
976 		buf.append("}\n");
977 
978 		assertEqualString(preview, buf.toString());
979 
980 	}
981 
testRemoveInsert2Before_only_2_3_4()982 	public void testRemoveInsert2Before_only_2_3_4() throws Exception {
983 		// remove first add 2x first, remove last add 2x  last
984 
985 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
986 		StringBuffer buf= new StringBuffer();
987 		buf.append("package test1;\n");
988 		buf.append("public class C {\n");
989 		buf.append("//c1\n");
990 		buf.append("\n");
991 		buf.append("    public int x1;\n");
992 		buf.append("\n");
993 		buf.append("//c2\n");
994 		buf.append("\n");
995 		buf.append("    public int x2;\n");
996 		buf.append("\n");
997 		buf.append("//c3\n");
998 		buf.append("\n");
999 		buf.append("    public int x3;\n");
1000 		buf.append("\n");
1001 		buf.append("//c4\n");
1002 		buf.append("}\n");
1003 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1004 
1005 		CompilationUnit astRoot= createAST(cu);
1006 		AST ast= astRoot.getAST();
1007 
1008 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1009 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1010 		List decls= type.bodyDeclarations();
1011 
1012 		rewrite.remove((ASTNode) decls.get(0), null);
1013 		rewrite.remove((ASTNode) decls.get(2), null);
1014 
1015 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
1016 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
1017 		FieldDeclaration decl3= newFieldDeclaration(ast, "new3");
1018 		FieldDeclaration decl4= newFieldDeclaration(ast, "new4");
1019 
1020 		ASTNode firstDecl= (ASTNode) decls.get(0);
1021 
1022 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1023 		listRewrite.insertBefore(decl1, firstDecl, null);
1024 		listRewrite.insertAfter(decl2, firstDecl, null);
1025 		listRewrite.insertLast(decl3, null);
1026 		listRewrite.insertAfter(decl4, decl3, null);
1027 
1028 		String preview= evaluateRewrite(cu, rewrite);
1029 
1030 		buf= new StringBuffer();
1031 		buf.append("package test1;\n");
1032 		buf.append("public class C {\n");
1033 		buf.append("//c1\n");
1034 		buf.append("\n");
1035 		buf.append("    int new1;\n");
1036 		buf.append("\n");
1037 		buf.append("    int new2;\n");
1038 		buf.append("\n");
1039 		buf.append("    \n");
1040 		buf.append("\n");
1041 		buf.append("//c2\n");
1042 		buf.append("\n");
1043 		buf.append("    public int x2;\n");
1044 		buf.append("\n");
1045 		buf.append("//c3\n");
1046 		buf.append("\n");
1047 		buf.append("    int new3;\n");
1048 		buf.append("\n");
1049 		buf.append("    int new4;\n");
1050 		buf.append("\n");
1051 		buf.append("//c4\n");
1052 		buf.append("}\n");
1053 
1054 		assertEqualString(preview, buf.toString());
1055 
1056 	}
1057 
testRemoveInsert3Before_only_2_3_4()1058 	public void testRemoveInsert3Before_only_2_3_4() throws Exception {
1059 		// remove middle, add before, add after
1060 
1061 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1062 		StringBuffer buf= new StringBuffer();
1063 		buf.append("package test1;\n");
1064 		buf.append("public class C {\n");
1065 		buf.append("//c1\n");
1066 		buf.append("\n");
1067 		buf.append("    public int x1;\n");
1068 		buf.append("\n");
1069 		buf.append("//c2\n");
1070 		buf.append("\n");
1071 		buf.append("    public int x2;\n");
1072 		buf.append("\n");
1073 		buf.append("//c3\n");
1074 		buf.append("\n");
1075 		buf.append("    public int x3;\n");
1076 		buf.append("\n");
1077 		buf.append("//c4\n");
1078 		buf.append("}\n");
1079 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1080 
1081 		CompilationUnit astRoot= createAST(cu);
1082 		AST ast= astRoot.getAST();
1083 
1084 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1085 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1086 		List decls= type.bodyDeclarations();
1087 
1088 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
1089 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
1090 
1091 		ASTNode middleDecl= (ASTNode) decls.get(1);
1092 
1093 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1094 		listRewrite.remove(middleDecl, null);
1095 
1096 		listRewrite.insertBefore(decl1, middleDecl, null);
1097 		listRewrite.insertAfter(decl2, middleDecl, null);
1098 
1099 		String preview= evaluateRewrite(cu, rewrite);
1100 
1101 		buf= new StringBuffer();
1102 		buf.append("package test1;\n");
1103 		buf.append("public class C {\n");
1104 		buf.append("//c1\n");
1105 		buf.append("\n");
1106 		buf.append("    public int x1;\n");
1107 		buf.append("\n");
1108 		buf.append("//c2\n");
1109 		buf.append("\n");
1110 		buf.append("    int new1;\n");
1111 		buf.append("\n");
1112 		buf.append("    int new2;\n");
1113 		buf.append("\n");
1114 		buf.append("    \n");
1115 		buf.append("\n");
1116 		buf.append("//c3\n");
1117 		buf.append("\n");
1118 		buf.append("    public int x3;\n");
1119 		buf.append("\n");
1120 		buf.append("//c4\n");
1121 		buf.append("}\n");
1122 
1123 		assertEqualString(preview, buf.toString());
1124 
1125 	}
1126 
testRemoveInsert4_only_2()1127 	public void testRemoveInsert4_only_2() throws Exception {
1128 		// remove first and add after first, remove last and add before last
1129 
1130 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1131 		StringBuffer buf= new StringBuffer();
1132 		buf.append("package test1;\n");
1133 		buf.append("public class C {\n");
1134 		buf.append("//c1\n");
1135 		buf.append("\n");
1136 		buf.append("    public void foo1();\n");
1137 		buf.append("\n");
1138 		buf.append("//c2\n");
1139 		buf.append("\n");
1140 		buf.append("    public void foo2();\n");
1141 		buf.append("\n");
1142 		buf.append("//c3\n");
1143 		buf.append("\n");
1144 		buf.append("    public void foo3();\n");
1145 		buf.append("\n");
1146 		buf.append("//c4\n");
1147 		buf.append("}\n");
1148 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1149 
1150 		CompilationUnit astRoot= createAST(cu);
1151 		AST ast= astRoot.getAST();
1152 
1153 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1154 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1155 		List decls= type.bodyDeclarations();
1156 
1157 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
1158 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
1159 
1160 		ASTNode firstDecl= (ASTNode) decls.get(0);
1161 		ASTNode lastDecl= (ASTNode) decls.get(2);
1162 
1163 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1164 
1165 		listRewrite.remove(firstDecl, null);
1166 		listRewrite.remove(lastDecl, null);
1167 
1168 		listRewrite.insertAfter(decl1, firstDecl, null);
1169 		listRewrite.insertBefore(decl2, lastDecl, null);
1170 
1171 		String preview= evaluateRewrite(cu, rewrite);
1172 
1173 		buf= new StringBuffer();
1174 		buf.append("package test1;\n");
1175 		buf.append("public class C {\n");
1176 		buf.append("//c1\n");
1177 		buf.append("\n");
1178 		buf.append("    void new1();\n");
1179 		buf.append("\n");
1180 		buf.append("    \n");
1181 		buf.append("\n");
1182 		buf.append("//c2\n");
1183 		buf.append("\n");
1184 		buf.append("    public void foo2();\n");
1185 		buf.append("\n");
1186 		buf.append("//c3\n");
1187 		buf.append("\n");
1188 		buf.append("    void new2();\n");
1189 		buf.append("\n");
1190 		buf.append("//c4\n");
1191 		buf.append("}\n");
1192 
1193 		assertEqualString(preview, buf.toString());
1194 
1195 	}
1196 
testRemoveInsert4Before_only_2_3_4()1197 	public void testRemoveInsert4Before_only_2_3_4() throws Exception {
1198 		// remove first and add after first, remove last and add before last
1199 
1200 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1201 		StringBuffer buf= new StringBuffer();
1202 		buf.append("package test1;\n");
1203 		buf.append("public class C {\n");
1204 		buf.append("//c1\n");
1205 		buf.append("\n");
1206 		buf.append("    public int x1;\n");
1207 		buf.append("\n");
1208 		buf.append("//c2\n");
1209 		buf.append("\n");
1210 		buf.append("    public int x2;\n");
1211 		buf.append("\n");
1212 		buf.append("//c3\n");
1213 		buf.append("\n");
1214 		buf.append("    public int x3;\n");
1215 		buf.append("\n");
1216 		buf.append("//c4\n");
1217 		buf.append("}\n");
1218 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1219 
1220 		CompilationUnit astRoot= createAST(cu);
1221 		AST ast= astRoot.getAST();
1222 
1223 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1224 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1225 		List decls= type.bodyDeclarations();
1226 
1227 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
1228 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
1229 
1230 		ASTNode firstDecl= (ASTNode) decls.get(0);
1231 		ASTNode lastDecl= (ASTNode) decls.get(2);
1232 
1233 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1234 		listRewrite.remove(firstDecl, null);
1235 		listRewrite.remove(lastDecl, null);
1236 
1237 		listRewrite.insertAfter(decl1, firstDecl, null);
1238 		listRewrite.insertBefore(decl2, lastDecl, null);
1239 
1240 		String preview= evaluateRewrite(cu, rewrite);
1241 
1242 		buf= new StringBuffer();
1243 		buf.append("package test1;\n");
1244 		buf.append("public class C {\n");
1245 		buf.append("//c1\n");
1246 		buf.append("\n");
1247 		buf.append("    int new1;\n");
1248 		buf.append("\n");
1249 		buf.append("    \n");
1250 		buf.append("\n");
1251 		buf.append("//c2\n");
1252 		buf.append("\n");
1253 		buf.append("    public int x2;\n");
1254 		buf.append("\n");
1255 		buf.append("//c3\n");
1256 		buf.append("\n");
1257 		buf.append("    int new2;\n");
1258 		buf.append("\n");
1259 		buf.append("//c4\n");
1260 		buf.append("}\n");
1261 
1262 		assertEqualString(preview, buf.toString());
1263 
1264 	}
1265 
testRemoveInsert5_only_2()1266 	public void testRemoveInsert5_only_2() throws Exception {
1267 		// remove first and add after and before first, remove last and add after and before last
1268 
1269 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1270 		StringBuffer buf= new StringBuffer();
1271 		buf.append("package test1;\n");
1272 		buf.append("public class C {\n");
1273 		buf.append("//c1\n");
1274 		buf.append("\n");
1275 		buf.append("    public void foo1();\n");
1276 		buf.append("\n");
1277 		buf.append("//c2\n");
1278 		buf.append("\n");
1279 		buf.append("    public void foo2();\n");
1280 		buf.append("\n");
1281 		buf.append("//c3\n");
1282 		buf.append("\n");
1283 		buf.append("    public void foo3();\n");
1284 		buf.append("\n");
1285 		buf.append("//c4\n");
1286 		buf.append("}\n");
1287 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1288 
1289 		CompilationUnit astRoot= createAST(cu);
1290 		AST ast= astRoot.getAST();
1291 
1292 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1293 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1294 		List decls= type.bodyDeclarations();
1295 
1296 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
1297 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
1298 		MethodDeclaration decl3= newMethodDeclaration(ast, "new3");
1299 		MethodDeclaration decl4= newMethodDeclaration(ast, "new4");
1300 
1301 		ASTNode firstDecl= (ASTNode) decls.get(0);
1302 		ASTNode lastDecl= (ASTNode) decls.get(2);
1303 
1304 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1305 
1306 		rewrite.remove(firstDecl, null);
1307 		rewrite.remove(lastDecl, null);
1308 
1309 		listRewrite.insertBefore(decl1, firstDecl, null);
1310 		listRewrite.insertAfter(decl2, firstDecl, null);
1311 		listRewrite.insertBefore(decl3, lastDecl, null);
1312 		listRewrite.insertAfter(decl4, lastDecl, null);
1313 
1314 
1315 		String preview= evaluateRewrite(cu, rewrite);
1316 
1317 		buf= new StringBuffer();
1318 		buf.append("package test1;\n");
1319 		buf.append("public class C {\n");
1320 		buf.append("//c1\n");
1321 		buf.append("\n");
1322 		buf.append("    void new1();\n");
1323 		buf.append("\n");
1324 		buf.append("    void new2();\n");
1325 		buf.append("\n");
1326 		buf.append("    \n");
1327 		buf.append("\n");
1328 		buf.append("//c2\n");
1329 		buf.append("\n");
1330 		buf.append("    public void foo2();\n");
1331 		buf.append("\n");
1332 		buf.append("//c3\n");
1333 		buf.append("\n");
1334 		buf.append("    void new3();\n");
1335 		buf.append("\n");
1336 		buf.append("    void new4();\n");
1337 		buf.append("\n");
1338 		buf.append("//c4\n");
1339 		buf.append("}\n");
1340 
1341 		assertEqualString(preview, buf.toString());
1342 
1343 	}
1344 
testRemoveInsert5Before_only_2_3_4()1345 	public void testRemoveInsert5Before_only_2_3_4() throws Exception {
1346 		// remove first and add after first, remove last and add before last
1347 
1348 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1349 		StringBuffer buf= new StringBuffer();
1350 		buf.append("package test1;\n");
1351 		buf.append("public class C {\n");
1352 		buf.append("//c1\n");
1353 		buf.append("\n");
1354 		buf.append("    public int x1;\n");
1355 		buf.append("\n");
1356 		buf.append("//c2\n");
1357 		buf.append("\n");
1358 		buf.append("    public int x2;\n");
1359 		buf.append("\n");
1360 		buf.append("//c3\n");
1361 		buf.append("\n");
1362 		buf.append("    public int x3;\n");
1363 		buf.append("\n");
1364 		buf.append("//c4\n");
1365 		buf.append("}\n");
1366 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1367 
1368 		CompilationUnit astRoot= createAST(cu);
1369 		AST ast= astRoot.getAST();
1370 
1371 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1372 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1373 		List decls= type.bodyDeclarations();
1374 
1375 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
1376 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
1377 		FieldDeclaration decl3= newFieldDeclaration(ast, "new3");
1378 		FieldDeclaration decl4= newFieldDeclaration(ast, "new4");
1379 
1380 		ASTNode firstDecl= (ASTNode) decls.get(0);
1381 		ASTNode lastDecl= (ASTNode) decls.get(2);
1382 
1383 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1384 
1385 		rewrite.remove(firstDecl, null);
1386 		rewrite.remove(lastDecl, null);
1387 
1388 		listRewrite.insertBefore(decl1, firstDecl, null);
1389 		listRewrite.insertAfter(decl2, firstDecl, null);
1390 		listRewrite.insertBefore(decl3, lastDecl, null);
1391 		listRewrite.insertAfter(decl4, lastDecl, null);
1392 
1393 		String preview= evaluateRewrite(cu, rewrite);
1394 
1395 		buf= new StringBuffer();
1396 		buf.append("package test1;\n");
1397 		buf.append("public class C {\n");
1398 		buf.append("//c1\n");
1399 		buf.append("\n");
1400 		buf.append("    int new1;\n");
1401 		buf.append("\n");
1402 		buf.append("    int new2;\n");
1403 		buf.append("\n");
1404 		buf.append("    \n");
1405 		buf.append("\n");
1406 		buf.append("//c2\n");
1407 		buf.append("\n");
1408 		buf.append("    public int x2;\n");
1409 		buf.append("\n");
1410 		buf.append("//c3\n");
1411 		buf.append("\n");
1412 		buf.append("    int new3;\n");
1413 		buf.append("\n");
1414 		buf.append("    int new4;\n");
1415 		buf.append("\n");
1416 		buf.append("//c4\n");
1417 		buf.append("}\n");
1418 
1419 		assertEqualString(preview, buf.toString());
1420 
1421 	}
1422 
1423 
testRemoveInsert6_only_2()1424 	public void testRemoveInsert6_only_2() throws Exception {
1425 		// remove all, add before first and after last
1426 
1427 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1428 		StringBuffer buf= new StringBuffer();
1429 		buf.append("package test1;\n");
1430 		buf.append("public class C {\n");
1431 		buf.append("//c1\n");
1432 		buf.append("\n");
1433 		buf.append("    public void foo1();\n");
1434 		buf.append("\n");
1435 		buf.append("//c2\n");
1436 		buf.append("\n");
1437 		buf.append("    public void foo2();\n");
1438 		buf.append("\n");
1439 		buf.append("//c3\n");
1440 		buf.append("\n");
1441 		buf.append("    public void foo3();\n");
1442 		buf.append("\n");
1443 		buf.append("//c4\n");
1444 		buf.append("}\n");
1445 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1446 
1447 		CompilationUnit astRoot= createAST(cu);
1448 		AST ast= astRoot.getAST();
1449 
1450 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1451 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1452 		List decls= type.bodyDeclarations();
1453 
1454 		rewrite.remove((ASTNode) decls.get(0), null);
1455 		rewrite.remove((ASTNode) decls.get(1), null);
1456 		rewrite.remove((ASTNode) decls.get(2), null);
1457 
1458 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
1459 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
1460 
1461 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1462 		listRewrite.insertFirst(decl1, null);
1463 		listRewrite.insertLast(decl2, null);
1464 
1465 		String preview= evaluateRewrite(cu, rewrite);
1466 
1467 		buf= new StringBuffer();
1468 		buf.append("package test1;\n");
1469 		buf.append("public class C {\n");
1470 		buf.append("//c1\n");
1471 		buf.append("\n");
1472 		buf.append("    void new1();\n");
1473 		buf.append("\n");
1474 		buf.append("    \n");
1475 		buf.append("\n");
1476 		buf.append("//c2\n");
1477 		buf.append("\n");
1478 		buf.append("    \n");
1479 		buf.append("\n");
1480 		buf.append("//c3\n");
1481 		buf.append("\n");
1482 		buf.append("    void new2();\n");
1483 		buf.append("\n");
1484 		buf.append("//c4\n");
1485 		buf.append("}\n");
1486 
1487 		assertEqualString(preview, buf.toString());
1488 
1489 	}
1490 
testRemoveInsert6Before_only_2_3_4()1491 	public void testRemoveInsert6Before_only_2_3_4() throws Exception {
1492 		// remove all, add before first and after last
1493 
1494 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1495 		StringBuffer buf= new StringBuffer();
1496 		buf.append("package test1;\n");
1497 		buf.append("public class C {\n");
1498 		buf.append("//c1\n");
1499 		buf.append("\n");
1500 		buf.append("    public int x1;\n");
1501 		buf.append("\n");
1502 		buf.append("//c2\n");
1503 		buf.append("\n");
1504 		buf.append("    public int x2;\n");
1505 		buf.append("\n");
1506 		buf.append("//c3\n");
1507 		buf.append("\n");
1508 		buf.append("    public int x3;\n");
1509 		buf.append("\n");
1510 		buf.append("//c4\n");
1511 		buf.append("}\n");
1512 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1513 
1514 		CompilationUnit astRoot= createAST(cu);
1515 		AST ast= astRoot.getAST();
1516 
1517 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1518 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1519 		List decls= type.bodyDeclarations();
1520 
1521 		rewrite.remove((ASTNode) decls.get(0), null);
1522 		rewrite.remove((ASTNode) decls.get(1), null);
1523 		rewrite.remove((ASTNode) decls.get(2), null);
1524 
1525 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
1526 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
1527 
1528 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1529 		listRewrite.insertFirst(decl1, null);
1530 		listRewrite.insertLast(decl2, null);
1531 
1532 		String preview= evaluateRewrite(cu, rewrite);
1533 
1534 		buf= new StringBuffer();
1535 		buf.append("package test1;\n");
1536 		buf.append("public class C {\n");
1537 		buf.append("//c1\n");
1538 		buf.append("\n");
1539 		buf.append("    int new1;\n");
1540 		buf.append("\n");
1541 		buf.append("    \n");
1542 		buf.append("\n");
1543 		buf.append("//c2\n");
1544 		buf.append("\n");
1545 		buf.append("    \n");
1546 		buf.append("\n");
1547 		buf.append("//c3\n");
1548 		buf.append("\n");
1549 		buf.append("    int new2;\n");
1550 		buf.append("\n");
1551 		buf.append("//c4\n");
1552 		buf.append("}\n");
1553 
1554 		assertEqualString(preview, buf.toString());
1555 
1556 	}
1557 
1558 
testRemoveInsert7_only_2()1559 	public void testRemoveInsert7_only_2() throws Exception {
1560 		// remove all, add after first and before last
1561 
1562 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1563 		StringBuffer buf= new StringBuffer();
1564 		buf.append("package test1;\n");
1565 		buf.append("public class C {\n");
1566 		buf.append("//c1\n");
1567 		buf.append("\n");
1568 		buf.append("    public void foo1();\n");
1569 		buf.append("\n");
1570 		buf.append("//c2\n");
1571 		buf.append("\n");
1572 		buf.append("    public void foo2();\n");
1573 		buf.append("\n");
1574 		buf.append("//c3\n");
1575 		buf.append("\n");
1576 		buf.append("    public void foo3();\n");
1577 		buf.append("\n");
1578 		buf.append("//c4\n");
1579 		buf.append("}\n");
1580 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1581 
1582 		CompilationUnit astRoot= createAST(cu);
1583 		AST ast= astRoot.getAST();
1584 
1585 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1586 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1587 		List decls= type.bodyDeclarations();
1588 
1589 		rewrite.remove((ASTNode) decls.get(0), null);
1590 		rewrite.remove((ASTNode) decls.get(1), null);
1591 		rewrite.remove((ASTNode) decls.get(2), null);
1592 
1593 		MethodDeclaration decl1= newMethodDeclaration(ast, "new1");
1594 		MethodDeclaration decl2= newMethodDeclaration(ast, "new2");
1595 
1596 		ASTNode middleDecl= (ASTNode) decls.get(1);
1597 
1598 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1599 		listRewrite.insertBefore(decl1, middleDecl, null);
1600 		listRewrite.insertAfter(decl2, middleDecl, null);
1601 
1602 		String preview= evaluateRewrite(cu, rewrite);
1603 
1604 		buf= new StringBuffer();
1605 		buf.append("package test1;\n");
1606 		buf.append("public class C {\n");
1607 		buf.append("//c1\n");
1608 		buf.append("\n");
1609 		buf.append("    void new1();\n");
1610 		buf.append("\n");
1611 		buf.append("    \n");
1612 		buf.append("\n");
1613 		buf.append("//c2\n");
1614 		buf.append("\n");
1615 		buf.append("    void new2();\n");
1616 		buf.append("\n");
1617 		buf.append("//c3\n");
1618 		buf.append("\n");
1619 		buf.append("    \n");
1620 		buf.append("\n");
1621 		buf.append("//c4\n");
1622 		buf.append("}\n");
1623 
1624 		assertEqualString(preview, buf.toString());
1625 
1626 	}
1627 
testRemoveInsert7Before_only_2_3_4()1628 	public void testRemoveInsert7Before_only_2_3_4() throws Exception {
1629 		// remove all, add after first and before last
1630 
1631 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1632 		StringBuffer buf= new StringBuffer();
1633 		buf.append("package test1;\n");
1634 		buf.append("public class C {\n");
1635 		buf.append("//c1\n");
1636 		buf.append("\n");
1637 		buf.append("    public int x1;\n");
1638 		buf.append("\n");
1639 		buf.append("//c2\n");
1640 		buf.append("\n");
1641 		buf.append("    public int x2;\n");
1642 		buf.append("\n");
1643 		buf.append("//c3\n");
1644 		buf.append("\n");
1645 		buf.append("    public int x3;\n");
1646 		buf.append("\n");
1647 		buf.append("//c4\n");
1648 		buf.append("}\n");
1649 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1650 
1651 		CompilationUnit astRoot= createAST(cu);
1652 		AST ast= astRoot.getAST();
1653 
1654 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1655 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1656 		List decls= type.bodyDeclarations();
1657 
1658 		rewrite.remove((ASTNode) decls.get(0), null);
1659 		rewrite.remove((ASTNode) decls.get(1), null);
1660 		rewrite.remove((ASTNode) decls.get(2), null);
1661 
1662 		FieldDeclaration decl1= newFieldDeclaration(ast, "new1");
1663 		FieldDeclaration decl2= newFieldDeclaration(ast, "new2");
1664 
1665 		ASTNode middleDecl= (ASTNode) decls.get(1);
1666 
1667 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
1668 		listRewrite.insertBefore(decl1, middleDecl, null);
1669 		listRewrite.insertAfter(decl2, middleDecl, null);
1670 
1671 		String preview= evaluateRewrite(cu, rewrite);
1672 
1673 		buf= new StringBuffer();
1674 		buf.append("package test1;\n");
1675 		buf.append("public class C {\n");
1676 		buf.append("//c1\n");
1677 		buf.append("\n");
1678 		buf.append("    int new1;\n");
1679 		buf.append("\n");
1680 		buf.append("    \n");
1681 		buf.append("\n");
1682 		buf.append("//c2\n");
1683 		buf.append("\n");
1684 		buf.append("    int new2;\n");
1685 		buf.append("\n");
1686 		buf.append("//c3\n");
1687 		buf.append("\n");
1688 		buf.append("    \n");
1689 		buf.append("\n");
1690 		buf.append("//c4\n");
1691 		buf.append("}\n");
1692 
1693 		assertEqualString(preview, buf.toString());
1694 
1695 	}
1696 
1697 
testTargetSourceRangeComputer()1698 	public void testTargetSourceRangeComputer () throws Exception {
1699 		// remove all, add after first and before last
1700 
1701 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1702 		StringBuffer buf= new StringBuffer();
1703 		buf.append("package test1;\n");
1704 		buf.append("public class C {\n");
1705 		buf.append("    //c1\n");
1706 		buf.append("\n");
1707 		buf.append("    public int x1;\n");
1708 		buf.append("\n");
1709 		buf.append("    //c2\n");
1710 		buf.append("\n");
1711 		buf.append("    public int x2;\n");
1712 		buf.append("\n");
1713 		buf.append("    //c3\n");
1714 		buf.append("\n");
1715 		buf.append("    public int x3;\n");
1716 		buf.append("\n");
1717 		buf.append("    //c4\n");
1718 		buf.append("}\n");
1719 		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1720 
1721 		CompilationUnit astRoot= createAST(cu);
1722 
1723 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1724 
1725 		TypeDeclaration type= findTypeDeclaration(astRoot, "C");
1726 		FieldDeclaration[] decls= type.getFields();
1727 
1728 		final Map extendedRanges= new HashMap();
1729 
1730 		FieldDeclaration f1= decls[0];
1731 		int off1= buf.indexOf("//c1");
1732 		int end1= f1.getStartPosition() + f1.getLength();
1733 		extendedRanges.put(f1, new SourceRange(off1, end1 - off1));
1734 
1735 		rewrite.setTargetSourceRangeComputer(new TargetSourceRangeComputer() {
1736 			public SourceRange computeSourceRange(ASTNode node) {
1737 				SourceRange range= (SourceRange) extendedRanges.get(node);
1738 				if (range != null)
1739 					return range;
1740 				return super.computeSourceRange(node);
1741 			}
1742 		});
1743 
1744 		rewrite.remove(f1, null);
1745 
1746 		String preview= evaluateRewrite(cu, rewrite);
1747 		// Note that c1 is absent because source range has been calculated from c1
1748 		buf= new StringBuffer();
1749 		buf.append("package test1;\n");
1750 		buf.append("public class C {\n");
1751 		buf.append("    \n");
1752 		buf.append("\n");
1753 		buf.append("    //c2\n");
1754 		buf.append("\n");
1755 		buf.append("    public int x2;\n");
1756 		buf.append("\n");
1757 		buf.append("    //c3\n");
1758 		buf.append("\n");
1759 		buf.append("    public int x3;\n");
1760 		buf.append("\n");
1761 		buf.append("    //c4\n");
1762 		buf.append("}\n");
1763 		assertEqualString(preview, buf.toString());
1764 
1765 	}
1766 
1767 }
1768 
1769 
1770 
1771