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