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.HashSet;
17 import java.util.List;
18 
19 import junit.framework.Test;
20 
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IPackageFragment;
23 import org.eclipse.jdt.core.dom.AST;
24 import org.eclipse.jdt.core.dom.ASTNode;
25 import org.eclipse.jdt.core.dom.Block;
26 import org.eclipse.jdt.core.dom.CatchClause;
27 import org.eclipse.jdt.core.dom.CompilationUnit;
28 import org.eclipse.jdt.core.dom.Expression;
29 import org.eclipse.jdt.core.dom.ExpressionStatement;
30 import org.eclipse.jdt.core.dom.IfStatement;
31 import org.eclipse.jdt.core.dom.InfixExpression;
32 import org.eclipse.jdt.core.dom.MethodDeclaration;
33 import org.eclipse.jdt.core.dom.MethodInvocation;
34 import org.eclipse.jdt.core.dom.ParenthesizedExpression;
35 import org.eclipse.jdt.core.dom.SimpleType;
36 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
37 import org.eclipse.jdt.core.dom.Statement;
38 import org.eclipse.jdt.core.dom.TryStatement;
39 import org.eclipse.jdt.core.dom.TypeDeclaration;
40 import org.eclipse.jdt.core.dom.TypeParameter;
41 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
42 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
43 import org.eclipse.jdt.internal.core.dom.rewrite.LineCommentEndOffsets;
44 
45 /**
46  *
47  */
48 @SuppressWarnings({"rawtypes", "unchecked"})
49 public class LineCommentOffsetsTest extends ASTRewritingTest {
50 
LineCommentOffsetsTest(String name)51 	public LineCommentOffsetsTest(String name) {
52 		super(name);
53 	}
LineCommentOffsetsTest(String name, int apiLevel)54 	public LineCommentOffsetsTest(String name, int apiLevel) {
55 		super(name, apiLevel);
56 	}
57 
suite()58 	public static Test suite() {
59 		return createSuite(LineCommentOffsetsTest.class);
60 	}
61 
testEmptyLineComments()62 	public void testEmptyLineComments() throws Exception {
63 
64 		StringBuffer buf= new StringBuffer();
65 		buf.append("\n");
66 
67 		LineCommentEndOffsets offsets= new LineCommentEndOffsets(null);
68 		boolean res= offsets.isEndOfLineComment(0);
69 		assertFalse(res);
70 		res= offsets.remove(0);
71 		assertFalse(res);
72 	}
73 
testRemove_since_3()74 	public void testRemove_since_3() throws Exception {
75 
76 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
77 
78 		StringBuffer buf= new StringBuffer();
79 		buf.append("package test1;//comment Y\n");
80 		buf.append("public class E//comment Y\n");
81 		buf.append("{//comment Y\n");
82 		buf.append("}//comment Y");
83 		String contents= buf.toString();
84 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", contents, false, null);
85 
86 		CompilationUnit astRoot= createAST(cu);
87 
88 		LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
89 
90 		int p1= contents.indexOf('Y') + 1;
91 		int p2= contents.indexOf('Y', p1) + 1;
92 		int p3= contents.indexOf('Y', p2) + 1;
93 		int p4= contents.indexOf('Y', p3) + 1;
94 
95 		assertFalse(offsets.isEndOfLineComment(0));
96 		assertTrue(offsets.isEndOfLineComment(p1));
97 		assertTrue(offsets.isEndOfLineComment(p2));
98 		assertTrue(offsets.isEndOfLineComment(p3));
99 		assertTrue(offsets.isEndOfLineComment(p4));
100 
101 		boolean res= offsets.remove(p2);
102 		assertTrue(res);
103 
104 		res= offsets.remove(p2);
105 		assertFalse(res);
106 
107 		assertFalse(offsets.isEndOfLineComment(0));
108 		assertTrue(offsets.isEndOfLineComment(p1));
109 		assertFalse(offsets.isEndOfLineComment(p2));
110 		assertTrue(offsets.isEndOfLineComment(p3));
111 		assertTrue(offsets.isEndOfLineComment(p4));
112 
113 		res= offsets.remove(p4);
114 		assertTrue(res);
115 
116 		assertFalse(offsets.isEndOfLineComment(0));
117 		assertTrue(offsets.isEndOfLineComment(p1));
118 		assertFalse(offsets.isEndOfLineComment(p2));
119 		assertTrue(offsets.isEndOfLineComment(p3));
120 		assertFalse(offsets.isEndOfLineComment(p4));
121 
122 		res= offsets.remove(p1);
123 		assertTrue(res);
124 
125 		assertFalse(offsets.isEndOfLineComment(0));
126 		assertFalse(offsets.isEndOfLineComment(p1));
127 		assertFalse(offsets.isEndOfLineComment(p2));
128 		assertTrue(offsets.isEndOfLineComment(p3));
129 		assertFalse(offsets.isEndOfLineComment(p4));
130 	}
131 
132 
133 
testLineCommentEndOffsets()134 	public void testLineCommentEndOffsets() throws Exception {
135 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
136 
137 
138 		StringBuffer buf= new StringBuffer();
139 		buf.append("package test1;\n");
140 		buf.append("/* comment */\n");
141 		buf.append("// comment Y\n");
142 		buf.append("public class E {\n");
143 		buf.append("    public void foo() {\n");
144 		buf.append("        while (i == 0) {\n");
145 		buf.append("            foo();\n");
146 		buf.append("            i++; // comment Y\n");
147 		buf.append("            i++;\n");
148 		buf.append("        }// comment// comment Y\n");
149 		buf.append("        return;\n");
150 		buf.append("    }\n");
151 		buf.append("} // comment Y");
152 		String content= buf.toString();
153 
154 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", content, false, null);
155 		CompilationUnit astRoot= createAST(cu);
156 
157 		LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
158 		HashSet expectedOffsets= new HashSet();
159 
160 		for (int i= 0; i < content.length(); i++) {
161 			char ch= content.charAt(i);
162 			if (ch == 'Y') {
163 				expectedOffsets.add(Integer.valueOf(i + 1));
164 			}
165 		}
166 
167 		int count= 0;
168 
169 		char[] charContent= content.toCharArray();
170 		for (int i= 0; i <= content.length() + 5; i++) {
171 			boolean expected= i > 0 && i <= content.length() && charContent[i - 1] == 'Y';
172 			boolean actual= offsets.isEndOfLineComment(i, charContent);
173 			assertEquals(expected, actual);
174 
175 			actual= offsets.isEndOfLineComment(i);
176 			assertEquals(expected, actual);
177 
178 			if (expected) {
179 				count++;
180 			}
181 
182 		}
183 		assertEquals(4, count);
184 	}
185 
testLineCommentEndOffsetsMixedLineDelimiter()186 	public void testLineCommentEndOffsetsMixedLineDelimiter() throws Exception {
187 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
188 
189 		StringBuffer buf= new StringBuffer();
190 		buf.append("package test1;\n");
191 		buf.append("/* comment */\r\n");
192 		buf.append("// comment Y\n");
193 		buf.append("public class E {\r\n");
194 		buf.append("    public void foo() {\n");
195 		buf.append("        while (i == 0) {\n");
196 		buf.append("            foo();\n");
197 		buf.append("            i++; // comment Y\r\n");
198 		buf.append("            i++;\n");
199 		buf.append("        }// comment// comment Y\r");
200 		buf.append("        return;\n");
201 		buf.append("    }\r\n");
202 		buf.append("} // comment Y");
203 		String content= buf.toString();
204 
205 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", content, false, null);
206 		CompilationUnit astRoot= createAST(cu);
207 
208 		LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
209 		HashSet expectedOffsets= new HashSet();
210 
211 		for (int i= 0; i < content.length(); i++) {
212 			char ch= content.charAt(i);
213 			if (ch == 'Y') {
214 				expectedOffsets.add(Integer.valueOf(i + 1));
215 			}
216 		}
217 
218 		int count= 0;
219 
220 		char[] charContent= content.toCharArray();
221 		for (int i= 0; i <= content.length() + 5; i++) {
222 			boolean expected= i > 0 && i <= content.length() && charContent[i - 1] == 'Y';
223 			boolean actual= offsets.isEndOfLineComment(i, charContent);
224 			assertEquals(expected, actual);
225 			if (expected) {
226 				count++;
227 			}
228 
229 		}
230 		assertEquals(4, count);
231 	}
232 
testCommentInLists_since_3()233 	public void testCommentInLists_since_3() throws Exception {
234 
235 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
236 		StringBuffer buf= new StringBuffer();
237 		buf.append("package test1;\n");
238 		buf.append("public class E implements A //comment\n");
239 		buf.append("{\n");
240 		buf.append("}\n");
241 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
242 
243 		CompilationUnit astRoot= createAST(cu);
244 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
245 
246 		AST ast= astRoot.getAST();
247 
248 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
249 		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
250 
251 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
252 		SimpleType newInterface= ast.newSimpleType(ast.newSimpleName("B"));
253 		listRewrite.insertLast(newInterface, null);
254 
255 		String preview= evaluateRewrite(cu, rewrite);
256 
257 		buf= new StringBuffer();
258 		buf.append("package test1;\n");
259 		buf.append("public class E implements A //comment\n");
260 		buf.append(", B\n");
261 		buf.append("{\n");
262 		buf.append("}\n");
263 		assertEqualString(preview, buf.toString());
264 	}
265 
testCommentInType_since_3()266 	public void testCommentInType_since_3() throws Exception {
267 
268 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
269 		StringBuffer buf= new StringBuffer();
270 		buf.append("package test1;\n");
271 		buf.append("public class E //comment\n");
272 		buf.append("{\n");
273 		buf.append("}\n");
274 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
275 
276 		CompilationUnit astRoot= createAST(cu);
277 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
278 
279 		AST ast= astRoot.getAST();
280 
281 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
282 		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
283 
284 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
285 		SimpleType newInterface= ast.newSimpleType(ast.newSimpleName("B"));
286 		listRewrite.insertLast(newInterface, null);
287 
288 		String preview= evaluateRewrite(cu, rewrite);
289 
290 		buf= new StringBuffer();
291 		buf.append("package test1;\n");
292 		buf.append("public class E //comment\n");
293 		buf.append(" implements B\n");
294 		buf.append("{\n");
295 		buf.append("}\n");
296 		assertEqualString(preview, buf.toString());
297 	}
298 
testBug103340_since_3()299 	public void testBug103340_since_3() throws Exception {
300 
301 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
302 		StringBuffer buf= new StringBuffer();
303 		buf.append("package test1;\n");
304 		buf.append("public class E //implements List\n");
305 		buf.append("{\n");
306 		buf.append("}\n");
307 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
308 
309 		CompilationUnit astRoot= createAST(cu);
310 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
311 
312 		AST ast= astRoot.getAST();
313 
314 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
315 		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
316 
317 		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
318 		TypeParameter newType= ast.newTypeParameter();
319 		newType.setName(ast.newSimpleName("X"));
320 		listRewrite.insertLast(newType, null);
321 
322 		String preview= evaluateRewrite(cu, rewrite);
323 
324 		buf= new StringBuffer();
325 		buf.append("package test1;\n");
326 		buf.append("public class E //implements List\n");
327 		buf.append("<X>\n");
328 		buf.append("{\n");
329 		buf.append("}\n");
330 		assertEqualString(preview, buf.toString());
331 	}
332 
testBug95839_since_3()333 	public void testBug95839_since_3() throws Exception {
334 
335 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
336 		StringBuffer buf= new StringBuffer();
337 		buf.append("package test1;\n");
338 		buf.append("public class E {\n");
339 		buf.append("  void foo() {\n");
340 		buf.append("    object.method(\n");
341 		buf.append("      param1, // text about param1\n");
342 		buf.append("      param2  // text about param2\n");
343 		buf.append("    );\n");
344 		buf.append("  }\n");
345 		buf.append("}\n");
346 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
347 
348 		CompilationUnit astRoot= createAST(cu);
349 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
350 
351 		AST ast= astRoot.getAST();
352 
353 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
354 		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
355 		ExpressionStatement statement= (ExpressionStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
356 		MethodInvocation inv= (MethodInvocation) statement.getExpression();
357 
358 		ListRewrite listRewrite= rewrite.getListRewrite(inv, MethodInvocation.ARGUMENTS_PROPERTY);
359 		listRewrite.insertLast(ast.newSimpleName("param3"), null);
360 
361 		String preview= evaluateRewrite(cu, rewrite);
362 
363 		buf= new StringBuffer();
364 		buf.append("package test1;\n");
365 		buf.append("public class E {\n");
366 		buf.append("  void foo() {\n");
367 		buf.append("    object.method(\n");
368 		buf.append("      param1, // text about param1\n");
369 		buf.append("      param2  // text about param2\n");
370 		buf.append(", param3\n");
371 		buf.append("    );\n");
372 		buf.append("  }\n");
373 		buf.append("}\n");
374 		assertEqualString(preview, buf.toString());
375 	}
376 
testBug114418_since_3()377 	public void testBug114418_since_3() throws Exception {
378 
379 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
380 		StringBuffer buf= new StringBuffer();
381 		buf.append("package test1;\n");
382 		buf.append("public class E {\n");
383 		buf.append("  void foo() {\n");
384 		buf.append("    try {\n");
385 		buf.append("    } catch (IOException e) {\n");
386 		buf.append("    }\n");
387 		buf.append("    // comment\n");
388 		buf.append("  }\n");
389 		buf.append("}\n");
390 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
391 
392 		CompilationUnit astRoot= createAST(cu);
393 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
394 
395 		AST ast= astRoot.getAST();
396 
397 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
398 		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
399 		TryStatement statement= (TryStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
400 
401 		ListRewrite listRewrite= rewrite.getListRewrite(statement, TryStatement.CATCH_CLAUSES_PROPERTY);
402 		CatchClause clause= ast.newCatchClause();
403 		SingleVariableDeclaration newSingleVariableDeclaration= ast.newSingleVariableDeclaration();
404 		newSingleVariableDeclaration.setName(ast.newSimpleName("e"));
405 		newSingleVariableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("MyException")));
406 		clause.setException(newSingleVariableDeclaration);
407 
408 		listRewrite.insertLast(clause, null);
409 
410 		String preview= evaluateRewrite(cu, rewrite);
411 
412 		buf= new StringBuffer();
413 		buf.append("package test1;\n");
414 		buf.append("public class E {\n");
415 		buf.append("  void foo() {\n");
416 		buf.append("    try {\n");
417 		buf.append("    } catch (IOException e) {\n");
418 		buf.append("    }\n");
419 		buf.append("    // comment\n");
420 		buf.append(" catch (MyException e) {\n");
421 		buf.append("    }\n");
422 		buf.append("  }\n");
423 		buf.append("}\n");
424 		assertEqualString(preview, buf.toString());
425 	}
426 
testBug128818_since_3()427 	public void testBug128818_since_3() throws Exception {
428 
429 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
430 		StringBuffer buf= new StringBuffer();
431 		buf.append("package test1;\n");
432 		buf.append("public class E {\n");
433 		buf.append("  void foo() {\n");
434 		buf.append("    if (true) {\n");
435 		buf.append("    } // comment\n");
436 		buf.append("    else\n");
437 		buf.append("      return;\n");
438 		buf.append("  }\n");
439 		buf.append("}\n");
440 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
441 
442 		CompilationUnit astRoot= createAST(cu);
443 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
444 
445 		AST ast= astRoot.getAST();
446 
447 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
448 		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
449 		IfStatement statement= (IfStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
450 
451 		rewrite.set(statement, IfStatement.ELSE_STATEMENT_PROPERTY, ast.newBlock(), null);
452 
453 		String preview= evaluateRewrite(cu, rewrite);
454 
455 		buf= new StringBuffer();
456 		buf.append("package test1;\n");
457 		buf.append("public class E {\n");
458 		buf.append("  void foo() {\n");
459 		buf.append("    if (true) {\n");
460 		buf.append("    } // comment\n");
461 		buf.append(" else {\n");
462 		buf.append("    }\n");
463 		buf.append("  }\n");
464 		buf.append("}\n");
465 		assertEqualString(preview, buf.toString());
466 	}
467 
testBug128422_since_3()468 	public void testBug128422_since_3() throws Exception {
469 
470 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
471 		StringBuffer buf= new StringBuffer();
472 		buf.append("package test1;\n");
473 		buf.append("public class E {\n");
474 		buf.append("  void foo() {\n");
475 		buf.append("    if (i != 0 //I don't like 0\n");
476 		buf.append("                 && i != 10) {\n");
477 		buf.append("    }\n");
478 		buf.append("  }\n");
479 		buf.append("}\n");
480 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
481 
482 		CompilationUnit astRoot= createAST(cu);
483 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
484 
485 		AST ast= astRoot.getAST();
486 
487 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
488 		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
489 		IfStatement statement= (IfStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
490 		Expression expression= ((InfixExpression) statement.getExpression()).getLeftOperand();
491 
492 		ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression();
493 		parenthesizedExpression.setExpression( (Expression) rewrite.createCopyTarget(expression));
494 		rewrite.replace(expression, parenthesizedExpression, null);
495 
496 		String preview= evaluateRewrite(cu, rewrite);
497 
498 		buf= new StringBuffer();
499 		buf.append("package test1;\n");
500 		buf.append("public class E {\n");
501 		buf.append("  void foo() {\n");
502 		buf.append("    if ((i != 0 //I don't like 0\n");
503 		buf.append(")\n");
504 		buf.append("                 && i != 10) {\n");
505 		buf.append("    }\n");
506 		buf.append("  }\n");
507 		buf.append("}\n");
508 		assertEqualString(preview, buf.toString());
509 	}
510 
testBug128422b_since_3()511 	public void testBug128422b_since_3() throws Exception {
512 
513 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
514 		StringBuffer buf= new StringBuffer();
515 		buf.append("package test1;\n");
516 		buf.append("public class E {\n");
517 		buf.append("  void foo() {\n");
518 		buf.append("    foo(); //comment\n");
519 		buf.append("    foo();\n");
520 		buf.append("  }\n");
521 		buf.append("}\n");
522 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
523 
524 		CompilationUnit astRoot= createAST(cu);
525 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
526 
527 		AST ast= astRoot.getAST();
528 
529 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
530 		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
531 		MethodDeclaration method= (MethodDeclaration) type.bodyDeclarations().get(0);
532 		List statements= method.getBody().statements();
533 		ASTNode copy= rewrite.createCopyTarget((ASTNode) statements.get(0));
534 
535 		Block newBlock= ast.newBlock();
536 		newBlock.statements().add(newStatement(ast));
537 		newBlock.statements().add(copy);
538 		newBlock.statements().add(newStatement(ast));
539 
540 		rewrite.getListRewrite(method.getBody(), Block.STATEMENTS_PROPERTY).insertLast(newBlock, null);
541 
542 		String preview= evaluateRewrite(cu, rewrite);
543 
544 		buf= new StringBuffer();
545 		buf.append("package test1;\n");
546 		buf.append("public class E {\n");
547 		buf.append("  void foo() {\n");
548 		buf.append("    foo(); //comment\n");
549 		buf.append("    foo();\n");
550 		buf.append("    {\n");
551 		buf.append("        bar();\n");
552 		buf.append("        foo(); //comment\n");
553 		buf.append("        bar();\n");
554 		buf.append("    }\n");
555 		buf.append("  }\n");
556 		buf.append("}\n");
557 		assertEqualString(preview, buf.toString());
558 	}
559 
newStatement(AST ast)560 	private Statement newStatement(AST ast) {
561 		MethodInvocation inv= ast.newMethodInvocation();
562 		inv.setName(ast.newSimpleName("bar"));
563 		return ast.newExpressionStatement(inv);
564 	}
565 
testCommentAtEnd_since_3()566 	public void testCommentAtEnd_since_3() throws Exception {
567 
568 		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
569 		StringBuffer buf= new StringBuffer();
570 		buf.append("package test1;\n");
571 		buf.append("public class E \n");
572 		buf.append("{\n");
573 		buf.append("}//comment");
574 		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
575 
576 		CompilationUnit astRoot= createAST(cu);
577 		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
578 
579 		AST ast= astRoot.getAST();
580 
581 		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
582 
583 		ListRewrite listRewrite= rewrite.getListRewrite(astRoot, CompilationUnit.TYPES_PROPERTY);
584 		TypeDeclaration newType= ast.newTypeDeclaration();
585 		newType.setName(ast.newSimpleName("B"));
586 		listRewrite.insertLast(newType, null);
587 
588 		String preview= evaluateRewrite(cu, rewrite);
589 
590 		buf= new StringBuffer();
591 		buf.append("package test1;\n");
592 		buf.append("public class E \n");
593 		buf.append("{\n");
594 		buf.append("}//comment\n");
595 		buf.append("\n");
596 		buf.append("class B {\n");
597 		buf.append("}");
598 		assertEqualString(preview, buf.toString());
599 	}
600 
601 
602 
603 }
604