Lines 14-23
Link Here
|
14 |
|
14 |
|
15 |
import junit.framework.Test; |
15 |
import junit.framework.Test; |
16 |
|
16 |
|
|
|
17 |
import org.eclipse.core.runtime.NullProgressMonitor; |
17 |
import org.eclipse.jdt.core.ICompilationUnit; |
18 |
import org.eclipse.jdt.core.ICompilationUnit; |
18 |
import org.eclipse.jdt.core.IPackageFragment; |
19 |
import org.eclipse.jdt.core.IPackageFragment; |
19 |
|
20 |
import org.eclipse.jdt.core.dom.AST; |
20 |
import org.eclipse.jdt.core.dom.*; |
21 |
import org.eclipse.jdt.core.dom.ASTNode; |
|
|
22 |
import org.eclipse.jdt.core.dom.ASTParser; |
23 |
import org.eclipse.jdt.core.dom.Block; |
24 |
import org.eclipse.jdt.core.dom.CatchClause; |
25 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
26 |
import org.eclipse.jdt.core.dom.Expression; |
27 |
import org.eclipse.jdt.core.dom.ExpressionStatement; |
28 |
import org.eclipse.jdt.core.dom.FieldAccess; |
29 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
30 |
import org.eclipse.jdt.core.dom.MethodInvocation; |
31 |
import org.eclipse.jdt.core.dom.Name; |
32 |
import org.eclipse.jdt.core.dom.PrimitiveType; |
33 |
import org.eclipse.jdt.core.dom.QualifiedName; |
34 |
import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
35 |
import org.eclipse.jdt.core.dom.Statement; |
36 |
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; |
37 |
import org.eclipse.jdt.core.dom.ThrowStatement; |
38 |
import org.eclipse.jdt.core.dom.TryStatement; |
39 |
import org.eclipse.jdt.core.dom.TypeDeclaration; |
21 |
import org.eclipse.jdt.core.tests.util.Util; |
40 |
import org.eclipse.jdt.core.tests.util.Util; |
22 |
|
41 |
|
23 |
public class ASTRewritingModifyingCopyTest extends ASTRewritingModifyingTest { |
42 |
public class ASTRewritingModifyingCopyTest extends ASTRewritingModifyingTest { |
Lines 342-371
Link Here
|
342 |
astRoot.recordModifications(); |
361 |
astRoot.recordModifications(); |
343 |
|
362 |
|
344 |
TypeDeclaration type = (TypeDeclaration) astRoot.types().get(0); |
363 |
TypeDeclaration type = (TypeDeclaration) astRoot.types().get(0); |
345 |
AST ast = type.getAST(); |
364 |
AST ast = type.getAST(); |
346 |
|
365 |
|
347 |
MethodDeclaration m = ast.newMethodDeclaration(); |
366 |
MethodDeclaration m = ast.newMethodDeclaration(); |
348 |
type.bodyDeclarations().add(m); |
367 |
type.bodyDeclarations().add(m); |
349 |
|
368 |
|
350 |
Block block = ast.newBlock(); |
369 |
Block block = ast.newBlock(); |
351 |
m.setName(ast.newSimpleName("foo")); |
370 |
m.setName(ast.newSimpleName("foo")); |
352 |
m.setReturnType(ast.newPrimitiveType(PrimitiveType.VOID)); |
371 |
m.setReturnType(ast.newPrimitiveType(PrimitiveType.VOID)); |
353 |
m.setBody(block); |
372 |
m.setBody(block); |
354 |
|
373 |
|
355 |
FieldAccess fa = ast.newFieldAccess(); |
374 |
FieldAccess fa = ast.newFieldAccess(); |
356 |
fa.setExpression(ast.newThisExpression()); |
375 |
fa.setExpression(ast.newThisExpression()); |
357 |
fa.setName(ast.newSimpleName("x")); |
376 |
fa.setName(ast.newSimpleName("x")); |
358 |
MethodInvocation mi = ast.newMethodInvocation(); |
377 |
MethodInvocation mi = ast.newMethodInvocation(); |
359 |
mi.setExpression(fa); |
378 |
mi.setExpression(fa); |
360 |
mi.setName(ast.newSimpleName("llall")); |
379 |
mi.setName(ast.newSimpleName("llall")); |
361 |
|
380 |
|
362 |
ExpressionStatement exp = ast.newExpressionStatement(mi); |
381 |
ExpressionStatement exp = ast.newExpressionStatement(mi); |
363 |
block.statements().add(exp); |
382 |
block.statements().add(exp); |
364 |
|
383 |
|
365 |
StructuralPropertyDescriptor loc = mi.getLocationInParent(); |
384 |
StructuralPropertyDescriptor loc = mi.getLocationInParent(); |
366 |
//This will cause the bug |
385 |
//This will cause the bug |
367 |
ASTNode node = ASTNode.copySubtree(ast, fa); |
386 |
ASTNode node = ASTNode.copySubtree(ast, fa); |
368 |
exp.setStructuralProperty(loc, node); |
387 |
exp.setStructuralProperty(loc, node); |
369 |
|
388 |
|
370 |
String preview = evaluateRewrite(cu, astRoot); |
389 |
String preview = evaluateRewrite(cu, astRoot); |
371 |
|
390 |
|
Lines 375-432
Link Here
|
375 |
buf.append(" void foo(){this.x;} }"); |
394 |
buf.append(" void foo(){this.x;} }"); |
376 |
assertEqualString(Util.convertToIndependantLineDelimiter(preview), Util.convertToIndependantLineDelimiter(buf.toString())); |
395 |
assertEqualString(Util.convertToIndependantLineDelimiter(preview), Util.convertToIndependantLineDelimiter(buf.toString())); |
377 |
} |
396 |
} |
|
|
397 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304656 |
398 |
public void test0008() throws Exception { |
399 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test0008", false, null); |
400 |
StringBuffer buf= new StringBuffer(); |
401 |
buf.append("package test0008;\n" + |
402 |
"\n" + |
403 |
"public class TestClass {\n" + |
404 |
" Thread t = new Thread(new Runnable(){\n" + |
405 |
"\n" + |
406 |
" @Override\n" + |
407 |
" public void run() {\n" + |
408 |
" try {\n" + |
409 |
" Thread.currentThread().sleep(1000);\n" + |
410 |
" } catch (InterruptedException e) {\n" + |
411 |
" e.printStackTrace();\n" + |
412 |
" }\n" + |
413 |
" }\n" + |
414 |
" \n" + |
415 |
" });\n" + |
416 |
"\n" + |
417 |
" public void testMethod(){\n" + |
418 |
" t.start();\n" + |
419 |
" }\n" + |
420 |
"\n" + |
421 |
" public static Thread staticTestMethod(Thread thread){\n" + |
422 |
" return thread;\n" + |
423 |
" }\n" + |
424 |
"}"); |
425 |
ICompilationUnit cu = pack1.createCompilationUnit("Test.java", buf.toString(), false, null); |
426 |
|
427 |
ASTParser astParser = ASTParser.newParser(AST.JLS3); |
428 |
astParser.setSource(cu); |
429 |
ASTNode root = astParser.createAST(new NullProgressMonitor()); |
430 |
AST ast = root.getAST(); |
431 |
|
432 |
CompilationUnit compilationUnit = (CompilationUnit) root; |
433 |
compilationUnit.recordModifications(); |
434 |
List types = compilationUnit.types(); |
435 |
for (int i = 0, max = types.size(); i < max; i++) { |
436 |
TypeDeclaration td = (TypeDeclaration) types.get(i); |
437 |
MethodDeclaration[] methods = td.getMethods(); |
438 |
for (int j = 0, max2 = methods.length; j < max2; j++) { |
439 |
MethodDeclaration md = methods[j]; |
440 |
if (md.getName().getFullyQualifiedName().equals("testMethod")) { |
441 |
List statements = md.getBody().statements(); |
442 |
ExpressionStatement es = (ExpressionStatement) statements.get(0); |
443 |
MethodInvocation mi = (MethodInvocation) es.getExpression(); |
444 |
Expression existingExpression = mi.getExpression(); |
445 |
MethodInvocation threadNameInvocation = ast.newMethodInvocation(); |
446 |
threadNameInvocation.setExpression(null); |
447 |
threadNameInvocation.setName(ast.newSimpleName("staticTestMethod")); |
448 |
existingExpression.delete(); |
449 |
threadNameInvocation.arguments().add(existingExpression); |
450 |
mi.setExpression(threadNameInvocation); |
451 |
|
452 |
|
453 |
Block code = md.getBody(); |
454 |
TryStatement tryStatement = ast.newTryStatement(); |
455 |
List catches = tryStatement.catchClauses(); |
456 |
// create catch clause |
457 |
CatchClause catchClause = ast.newCatchClause(); |
458 |
// exception to catch |
459 |
SingleVariableDeclaration svd = ast.newSingleVariableDeclaration(); |
460 |
svd.setType(ast.newSimpleType(ast.newName("Throwable"))); |
461 |
svd.setName(ast.newSimpleName("e")); |
462 |
catchClause.setException(svd); |
463 |
// code to run on catch |
464 |
Block catchBody = ast.newBlock(); |
465 |
List catchStatements = catchBody.statements(); |
466 |
// throw statement |
467 |
ThrowStatement throwStatement = ast.newThrowStatement(); |
468 |
Expression throwExpression = ast.newName("e"); |
469 |
throwStatement.setExpression(throwExpression); |
470 |
catchStatements.add(throwStatement); |
471 |
catches.add(catchClause); |
472 |
// create finally statement |
473 |
Block finallyBlock = ast.newBlock(); |
474 |
statements = code.statements(); |
475 |
tryStatement.setFinally(finallyBlock); |
476 |
code.delete(); |
477 |
tryStatement.setBody(code); |
478 |
Block tryBlock = ast.newBlock(); |
479 |
tryBlock.statements().add(tryStatement); |
480 |
md.setBody(tryBlock); |
481 |
} |
482 |
} |
483 |
|
484 |
} |
485 |
String preview = evaluateRewrite(cu, compilationUnit); |
486 |
|
487 |
buf= new StringBuffer(); |
488 |
buf.append("package test0008;\n"); |
489 |
buf.append("\n"); |
490 |
buf.append("public class TestClass {\n"); |
491 |
buf.append(" Thread t = new Thread(new Runnable(){\n"); |
492 |
buf.append("\n"); |
493 |
buf.append(" @Override\n"); |
494 |
buf.append(" public void run() {\n"); |
495 |
buf.append(" try {\n"); |
496 |
buf.append(" Thread.currentThread().sleep(1000);\n"); |
497 |
buf.append(" } catch (InterruptedException e) {\n"); |
498 |
buf.append(" e.printStackTrace();\n"); |
499 |
buf.append(" }\n"); |
500 |
buf.append(" }\n"); |
501 |
buf.append(" \n"); |
502 |
buf.append(" });\n"); |
503 |
buf.append("\n"); |
504 |
buf.append(" public void testMethod(){\n"); |
505 |
buf.append(" try {\n"); |
506 |
buf.append(" staticTestMethod(t).start();\n"); |
507 |
buf.append(" } catch (Throwable e) {\n"); |
508 |
buf.append(" } finally {\n"); |
509 |
buf.append(" }\n"); |
510 |
buf.append(" }\n"); |
511 |
buf.append("\n"); |
512 |
buf.append(" public static Thread staticTestMethod(Thread thread){\n"); |
513 |
buf.append(" return thread;\n"); |
514 |
buf.append(" }\n"); |
515 |
buf.append("}"); |
516 |
assertEqualString(Util.convertToIndependantLineDelimiter(preview), Util.convertToIndependantLineDelimiter(buf.toString())); |
517 |
} |
378 |
|
518 |
|
379 |
// public void test0007() throws Exception { |
519 |
// public void test0007() throws Exception { |
380 |
// IPackageFragment pack1= fSourceFolder.createPackageFragment("test0007", false, null); |
520 |
// IPackageFragment pack1= fSourceFolder.createPackageFragment("test0007", false, null); |
381 |
// StringBuffer buf= new StringBuffer(); |
521 |
// StringBuffer buf= new StringBuffer(); |
382 |
// buf.append("package test0007;\n"); |
522 |
// buf.append("package test0007;\n"); |
383 |
// buf.append("\n"); |
523 |
// buf.append("\n"); |
384 |
// buf.append("public class X {\n"); |
524 |
// buf.append("public class X {\n"); |
385 |
// buf.append(" /**\n"); |
525 |
// buf.append(" /**\n"); |
386 |
// buf.append(" * NOTHING\n"); |
526 |
// buf.append(" * NOTHING\n"); |
387 |
// buf.append(" */\n"); |
527 |
// buf.append(" */\n"); |
388 |
// buf.append(" void foo() {\n"); |
528 |
// buf.append(" void foo() {\n"); |
389 |
// buf.append(" \n"); |
529 |
// buf.append(" \n"); |
390 |
// buf.append(" }\n"); |
530 |
// buf.append(" }\n"); |
391 |
// buf.append(" void bar() {\n"); |
531 |
// buf.append(" void bar() {\n"); |
392 |
// buf.append(" \n"); |
532 |
// buf.append(" \n"); |
393 |
// buf.append(" }\n"); |
533 |
// buf.append(" }\n"); |
394 |
// buf.append("}\n"); |
534 |
// buf.append("}\n"); |
395 |
// ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null); |
535 |
// ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null); |
396 |
// |
536 |
// |
397 |
// CompilationUnit astRoot= parseCompilationUnit(cu, false); |
537 |
// CompilationUnit astRoot= parseCompilationUnit(cu, false); |
398 |
// |
538 |
// |
399 |
// astRoot.recordModifications(); |
539 |
// astRoot.recordModifications(); |
400 |
// |
540 |
// |
401 |
// AST a = astRoot.getAST(); |
541 |
// AST a = astRoot.getAST(); |
402 |
// |
542 |
// |
403 |
// List types = astRoot.types(); |
543 |
// List types = astRoot.types(); |
404 |
// TypeDeclaration typeDeclaration = (TypeDeclaration)types.get(0); |
544 |
// TypeDeclaration typeDeclaration = (TypeDeclaration)types.get(0); |
405 |
// MethodDeclaration methodDeclaration1 = typeDeclaration.getMethods()[0]; |
545 |
// MethodDeclaration methodDeclaration1 = typeDeclaration.getMethods()[0]; |
406 |
// MethodDeclaration methodDeclaration2 = typeDeclaration.getMethods()[1]; |
546 |
// MethodDeclaration methodDeclaration2 = typeDeclaration.getMethods()[1]; |
407 |
// Javadoc javadoc1 = methodDeclaration1.getJavadoc(); |
547 |
// Javadoc javadoc1 = methodDeclaration1.getJavadoc(); |
408 |
// Javadoc javadoc2 = (Javadoc)ASTNode.copySubtree(a, javadoc1); |
548 |
// Javadoc javadoc2 = (Javadoc)ASTNode.copySubtree(a, javadoc1); |
409 |
// methodDeclaration2.setJavadoc(javadoc2); |
549 |
// methodDeclaration2.setJavadoc(javadoc2); |
410 |
// |
550 |
// |
411 |
// String preview = evaluateRewrite(cu, astRoot); |
551 |
// String preview = evaluateRewrite(cu, astRoot); |
412 |
// |
552 |
// |
413 |
// buf= new StringBuffer(); |
553 |
// buf= new StringBuffer(); |
414 |
// buf.append("package test0007;\n"); |
554 |
// buf.append("package test0007;\n"); |
415 |
// buf.append("\n"); |
555 |
// buf.append("\n"); |
416 |
// buf.append("public class X {\n"); |
556 |
// buf.append("public class X {\n"); |
417 |
// buf.append(" /**\n"); |
557 |
// buf.append(" /**\n"); |
418 |
// buf.append(" * NOTHING\n"); |
558 |
// buf.append(" * NOTHING\n"); |
419 |
// buf.append(" */\n"); |
559 |
// buf.append(" */\n"); |
420 |
// buf.append(" void foo() {\n"); |
560 |
// buf.append(" void foo() {\n"); |
421 |
// buf.append(" \n"); |
561 |
// buf.append(" \n"); |
422 |
// buf.append(" }\n"); |
562 |
// buf.append(" }\n"); |
423 |
// buf.append(" /**\n"); |
563 |
// buf.append(" /**\n"); |
424 |
// buf.append(" * NOTHING\n"); |
564 |
// buf.append(" * NOTHING\n"); |
425 |
// buf.append(" */\n"); |
565 |
// buf.append(" */\n"); |
426 |
// buf.append(" void bar() {\n"); |
566 |
// buf.append(" void bar() {\n"); |
427 |
// buf.append(" \n"); |
567 |
// buf.append(" \n"); |
428 |
// buf.append(" }\n"); |
568 |
// buf.append(" }\n"); |
429 |
// buf.append("}\n"); |
569 |
// buf.append("}\n"); |
430 |
// assertEqualString(Util.convertToIndependantLineDelimiter(preview), Util.convertToIndependantLineDelimiter(buf.toString())); |
570 |
// assertEqualString(Util.convertToIndependantLineDelimiter(preview), Util.convertToIndependantLineDelimiter(buf.toString())); |
431 |
// } |
571 |
// } |
432 |
} |
572 |
} |