|                                                                                                              1
 19  package org.netbeans.api.java.source.gen;
 20
 21  import java.io.File
  ; 22  import java.util.Collections
  ; 23  import com.sun.source.tree.*;
 24  import java.io.IOException
  ; 25  import javax.lang.model.element.Modifier;
 26  import javax.lang.model.type.TypeKind;
 27  import org.netbeans.api.java.source.CancellableTask;
 28  import org.netbeans.api.java.source.JavaSource;
 29  import static org.netbeans.api.java.source.JavaSource.*;
 30  import org.netbeans.api.java.source.TestUtilities;
 31  import org.netbeans.api.java.source.TreeMaker;
 32  import org.netbeans.api.java.source.WorkingCopy;
 33  import org.netbeans.junit.NbTestSuite;
 34  import org.openide.filesystems.FileUtil;
 35
 36
 42  public class BodyStatementTest extends GeneratorTestMDRCompat {
 43
 44
 45      public BodyStatementTest(String
  name) { 46          super(name);
 47      }
 48
 49      public static NbTestSuite suite() {
 50          NbTestSuite suite = new NbTestSuite();
 51          suite.addTestSuite(BodyStatementTest.class);
 52          return suite;
 89      }
 90
 91
 94      public void testNullLiteral() throws Exception
  { 95          testFile = new File
  (getWorkDir(), "Test.java"); 96          TestUtilities.copyStringToFile(testFile,
 97              "package hierbas.del.litoral;\n\n" +
 98              "import java.io.*;\n\n" +
 99              "public class Test {\n" +
 100             "    public void taragui() {\n" +
 101             "        ;\n" +
 102             "    }\n" +
 103             "}\n"
 104             );
 105         String
  golden = 106             "package hierbas.del.litoral;\n\n" +
 107             "import java.io.*;\n\n" +
 108             "public class Test {\n" +
 109             "    public void taragui() {\n" +
 110             "        ;\n" +
 111             "    System.err.println(null);\n" +
 112             "}\n" +
 113             "}\n";
 114         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 115         CancellableTask task = new CancellableTask<WorkingCopy>() {
 116
 117             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 118                 workingCopy.toPhase(Phase.RESOLVED);
 119                 TreeMaker make = workingCopy.getTreeMaker();
 120
 121                                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 123                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 124                 ExpressionStatementTree statement = make.ExpressionStatement(
 125                     make.MethodInvocation(
 126                         Collections.<ExpressionTree>emptyList(),
 127                         make.MemberSelect(
 128                             make.MemberSelect(
 129                                 make.Identifier("System"),
 130                                 "err"
 131                             ),
 132                             "println"
 133                         ),
 134                         Collections.singletonList(
 135                             make.Literal(null)
 136                         )
 137                     )
 138                 );
 139                 BlockTree copy = make.addBlockStatement(method.getBody(), statement);
 140                 workingCopy.rewrite(method.getBody(), copy);
 141             }
 142
 143             public void cancel() {
 144             }
 145         };
 146         testSource.runModificationTask(task).commit();
 147         String
  res = TestUtilities.copyFileToString(testFile); 148         System.err.println(res);
 149         assertEquals(golden, res);
 150     }
 151
 152
 155     public void testBooleanLiteral() throws Exception
  { 156         testFile = new File
  (getWorkDir(), "Test.java"); 157         TestUtilities.copyStringToFile(testFile,
 158             "package hierbas.del.litoral;\n\n" +
 159             "import java.io.*;\n\n" +
 160             "public class Test {\n" +
 161             "    public void taragui() {\n" +
 162             "        ;\n" +
 163             "    }\n" +
 164             "}\n"
 165             );
 166         String
  golden = 167             "package hierbas.del.litoral;\n\n" +
 168             "import java.io.*;\n\n" +
 169             "public class Test {\n" +
 170             "    public void taragui() {\n" +
 171             "        ;\n" +
 172             "    System.err.println(true);\n" +
 173             "}\n" +
 174             "}\n";
 175         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 176         CancellableTask task = new CancellableTask<WorkingCopy>() {
 177
 178             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 179                 workingCopy.toPhase(Phase.RESOLVED);
 180                 TreeMaker make = workingCopy.getTreeMaker();
 181
 182                                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 184                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 185                 ExpressionStatementTree statement = make.ExpressionStatement(
 186                     make.MethodInvocation(
 187                         Collections.<ExpressionTree>emptyList(),
 188                         make.MemberSelect(
 189                             make.MemberSelect(
 190                                 make.Identifier("System"),
 191                                 "err"
 192                             ),
 193                             "println"
 194                         ),
 195                         Collections.singletonList(
 196                             make.Literal(Boolean.TRUE)
 197                         )
 198                     )
 199                 );
 200                 BlockTree copy = make.addBlockStatement(method.getBody(), statement);
 201                 workingCopy.rewrite(method.getBody(), copy);
 202             }
 203
 204             public void cancel() {
 205             }
 206         };
 207         testSource.runModificationTask(task).commit();
 208         String
  res = TestUtilities.copyFileToString(testFile); 209         System.err.println(res);
 210         assertEquals(golden, res);
 211     }
 212
 213
 216     public void testRenameInIfStatement() throws Exception
  { 217         testFile = new File
  (getWorkDir(), "Test.java"); 218         TestUtilities.copyStringToFile(testFile,
 219             "package personal;\n" +
 220             "\n" +
 221             "import javax.swing.text.Element;\n" +
 222             "\n" +
 223             "public class Test {\n" +
 224             "    public void action666(Element el) {\n" +
 225             "        if (el.getName().equalsIgnoreCase(\"flaskuvElement\")) {\n" +
 226             "            System.err.println(\"Win!\");\n" +
 227             "        }\n" +
 228             "    }\n" +
 229             "}\n");
 230         String
  golden = 231             "package personal;\n" +
 232             "\n" +
 233             "import javax.swing.text.Element;\n" +
 234             "\n" +
 235             "public class Test {\n" +
 236             "    public void action666(Element element) {\n" +
 237             "        if (element.getName().equalsIgnoreCase(\"flaskuvElement\")) {\n" +
 238             "            System.err.println(\"Win!\");\n" +
 239             "        }\n" +
 240             "    }\n" +
 241             "}\n";
 242         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 243         CancellableTask task = new CancellableTask<WorkingCopy>() {
 244
 245             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 246                 workingCopy.toPhase(Phase.RESOLVED);
 247                 TreeMaker make = workingCopy.getTreeMaker();
 248                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 249                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 250                                 VariableTree vt = method.getParameters().get(0);
 252                 VariableTree parCopy = make.setLabel(vt, "element");
 253                 workingCopy.rewrite(vt, parCopy);
 254                                                 IfTree statementTree = (IfTree) method.getBody().getStatements().get(0);
 257                 ParenthesizedTree condition = (ParenthesizedTree) statementTree.getCondition();
 258                 MethodInvocationTree invocation = (MethodInvocationTree) condition.getExpression();
 259                 MemberSelectTree select = (MemberSelectTree) invocation.getMethodSelect();
 260                 invocation = (MethodInvocationTree) select.getExpression();
 261                 select = (MemberSelectTree) invocation.getMethodSelect();
 262                 IdentifierTree identToRename = (IdentifierTree) select.getExpression();
 263                 IdentifierTree copy = make.setLabel(identToRename, "element");
 264                 workingCopy.rewrite(identToRename, copy);
 265             }
 266
 267             public void cancel() {
 268             }
 269         };
 270         testSource.runModificationTask(task).commit();
 271         String
  res = TestUtilities.copyFileToString(testFile); 272         System.err.println(res);
 273         assertEquals(golden, res);
 274     }
 275
 276
 279     public void testRenameInLocalDecl() throws Exception
  { 280         testFile = new File
  (getWorkDir(), "Test.java"); 281         TestUtilities.copyStringToFile(testFile,
 282             "package personal;\n" +
 283             "\n" +
 284             "import javax.swing.text.Element;\n" +
 285             "\n" +
 286             "public class Test {\n" +
 287             "    public void action666(Element el) {\n" +
 288             "        String name = el.getName();\n" +
 289             "    }\n" +
 290             "}\n");
 291         String
  golden = 292             "package personal;\n" +
 293             "\n" +
 294             "import javax.swing.text.Element;\n" +
 295             "\n" +
 296             "public class Test {\n" +
 297             "    public void action666(Element element) {\n" +
 298             "        String name = element.getName();\n" +
 299             "    }\n" +
 300             "}\n";
 301         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 302         CancellableTask task = new CancellableTask<WorkingCopy>() {
 303
 304             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 305                 workingCopy.toPhase(Phase.RESOLVED);
 306                 TreeMaker make = workingCopy.getTreeMaker();
 307                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 308                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 309                                 VariableTree vt = method.getParameters().get(0);
 311                 VariableTree parCopy = make.setLabel(vt, "element");
 312                 workingCopy.rewrite(vt, parCopy);
 313                                 VariableTree statementTree = (VariableTree) method.getBody().getStatements().get(0);
 315                 MethodInvocationTree invocation = (MethodInvocationTree) statementTree.getInitializer();
 316                 MemberSelectTree select = (MemberSelectTree) invocation.getMethodSelect();
 317                 IdentifierTree identToRename = (IdentifierTree) select.getExpression();
 318                 IdentifierTree copy = make.setLabel(identToRename, "element");
 319                 workingCopy.rewrite(identToRename, copy);
 320             }
 321
 322             public void cancel() {
 323             }
 324         };
 325         testSource.runModificationTask(task).commit();
 326         String
  res = TestUtilities.copyFileToString(testFile); 327         System.err.println(res);
 328         assertEquals(golden, res);
 329     }
 330
 331
 334     public void testRenameInInvocationPars() throws Exception
  { 335         testFile = new File
  (getWorkDir(), "Test.java"); 336         TestUtilities.copyStringToFile(testFile,
 337             "package personal;\n" +
 338             "\n" +
 339             "import javax.swing.text.Element;\n" +
 340             "import java.util.Collections;\n" +
 341             "\n" +
 342             "public class Test {\n" +
 343             "    public void action666(Element el) {\n" +
 344             "        Collections.singleton(el);\n" +
 345             "    }\n" +
 346             "}\n");
 347         String
  golden = 348             "package personal;\n" +
 349             "\n" +
 350             "import javax.swing.text.Element;\n" +
 351             "import java.util.Collections;\n" +
 352             "\n" +
 353             "public class Test {\n" +
 354             "    public void action666(Element element) {\n" +
 355             "        Collections.singleton(element);\n" +
 356             "    }\n" +
 357             "}\n";
 358         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 359         CancellableTask task = new CancellableTask<WorkingCopy>() {
 360
 361             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 362                 workingCopy.toPhase(Phase.RESOLVED);
 363                 TreeMaker make = workingCopy.getTreeMaker();
 364                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 365                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 366                                 VariableTree vt = method.getParameters().get(0);
 368                 VariableTree parCopy = make.setLabel(vt, "element");
 369                 workingCopy.rewrite(vt, parCopy);
 370                                                 ExpressionStatementTree expressionStmt = (ExpressionStatementTree) method.getBody().getStatements().get(0);
 373                 MethodInvocationTree invocation = (MethodInvocationTree) expressionStmt.getExpression();
 374                 IdentifierTree identToRename = (IdentifierTree) invocation.getArguments().get(0);
 375                 IdentifierTree copy = make.setLabel(identToRename, "element");
 376                 workingCopy.rewrite(identToRename, copy);
 377             }
 378
 379             public void cancel() {
 380             }
 381         };
 382         testSource.runModificationTask(task).commit();
 383         String
  res = TestUtilities.copyFileToString(testFile); 384         System.err.println(res);
 385         assertEquals(golden, res);
 386     }
 387
 388
 391     public void testAddMethodToAnnInTry() throws Exception
  { 392         testFile = new File
  (getWorkDir(), "Test.java"); 393         TestUtilities.copyStringToFile(testFile,
 394             "package personal;\n" +
 395             "\n" +
 396             "import javax.swing.text.Element;\n" +
 397             "import java.util.Collections;\n" +
 398             "\n" +
 399             "public class Test {\n" +
 400             "   public void method() {\n" +
 401             "        try {\n" +
 402             "            new Runnable() {\n" +
 403             "            };\n" +
 404             "        } finally {\n" +
 405             "            System.err.println(\"Got a problem.\");\n" +
 406             "        }\n" +
 407             "    }\n" +
 408             "}\n" +
 409             "}\n");
 410
 411          String
  golden = 412             "package personal;\n" +
 413             "\n" +
 414             "import javax.swing.text.Element;\n" +
 415             "import java.util.Collections;\n" +
 416             "\n" +
 417             "public class Test {\n" +
 418             "   public void method() {\n" +
 419             "        try {\n" +
 420             "            new Runnable() {\n" +
 421             "                public void run() {\n" +
 422             "                }\n\n" +
 423             "            };\n" +
 424             "        } finally {\n" +
 425             "            System.err.println(\"Got a problem.\");\n" +
 426             "        }\n" +
 427             "    }\n" +
 428             "}\n" +
 429             "}\n";
 430         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 431         CancellableTask task = new CancellableTask<WorkingCopy>() {
 432
 433             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 434                 workingCopy.toPhase(Phase.RESOLVED);
 435                 TreeMaker make = workingCopy.getTreeMaker();
 436                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 437                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 438                                 TryTree tryStmt = (TryTree) method.getBody().getStatements().get(0);
 440                 ExpressionStatementTree exprStmt = (ExpressionStatementTree) tryStmt.getBlock().getStatements().get(0);
 441                 NewClassTree newClassTree = (NewClassTree) exprStmt.getExpression();
 442                 ClassTree anonClassTree = newClassTree.getClassBody();
 443                 MethodTree methodToAdd = make.Method(
 444                     make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)),
 445                     "run",
 446                     make.PrimitiveType(TypeKind.VOID),
 447                     Collections.<TypeParameterTree>emptyList(),
 448                     Collections.<VariableTree>emptyList(),
 449                     Collections.<ExpressionTree>emptyList(),
 450                     make.Block(Collections.<StatementTree>emptyList(), false),
 451                     null
 452                 );
 453                 ClassTree copy = make.addClassMember(anonClassTree, methodToAdd);
 454                 workingCopy.rewrite(anonClassTree, copy);
 455             }
 456
 457             public void cancel() {
 458             }
 459         };
 460         testSource.runModificationTask(task).commit();
 461         String
  res = TestUtilities.copyFileToString(testFile); 462         System.err.println(res);
 463         assertEquals(golden, res);
 464     }
 465
 466
 469     public void testReturnNotDoubled() throws Exception
  { 470         testFile = new File
  (getWorkDir(), "Test.java"); 471         TestUtilities.copyStringToFile(testFile,
 472             "package personal;\n" +
 473             "\n" +
 474             "import javax.swing.text.Element;\n" +
 475             "import java.util.Collections;\n" +
 476             "\n" +
 477             "public class Test {\n" +
 478             "   public Object method() {\n" +
 479             "        try {\n" +
 480             "            new Runnable() {\n" +
 481             "            }\n" +
 482             "            return null;\n" +
 483             "        } finally {\n" +
 484             "            System.err.println(\"Got a problem.\");\n" +
 485             "        }\n" +
 486             "    }\n" +
 487             "}\n");
 488
 489          String
  golden = 490             "package personal;\n" +
 491             "\n" +
 492             "import javax.swing.text.Element;\n" +
 493             "import java.util.Collections;\n" +
 494             "\n" +
 495             "public class Test {\n" +
 496             "   public Object method() {\n" +
 497             "        try {\n" +
 498             "            new Runnable() {\n" +
 499             "                public void run() {\n" +
 500             "                }\n\n" +
 501             "            }\n" +
 502             "            return null;\n" +
 503             "        } finally {\n" +
 504             "            System.err.println(\"Got a problem.\");\n" +
 505             "        }\n" +
 506             "    }\n" +
 507             "}\n";
 508         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 509         CancellableTask task = new CancellableTask<WorkingCopy>() {
 510
 511             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 512                 workingCopy.toPhase(Phase.RESOLVED);
 513                 TreeMaker make = workingCopy.getTreeMaker();
 514                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 515                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 516                                 TryTree tryStmt = (TryTree) method.getBody().getStatements().get(0);
 518                 BlockTree tryBlock = (BlockTree) tryStmt.getBlock();
 519                 ExpressionStatementTree exprStmt = (ExpressionStatementTree) tryStmt.getBlock().getStatements().get(0);
 520                 NewClassTree newClassTree = (NewClassTree) exprStmt.getExpression();
 521                 ClassTree anonClassTree = newClassTree.getClassBody();
 522                 MethodTree methodToAdd = make.Method(
 523                     make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)),
 524                     "run",
 525                     make.PrimitiveType(TypeKind.VOID),
 526                     Collections.<TypeParameterTree>emptyList(),
 527                     Collections.<VariableTree>emptyList(),
 528                     Collections.<ExpressionTree>emptyList(),
 529                     make.Block(Collections.<StatementTree>emptyList(), false),
 530                     null
 531                 );
 532                 ClassTree copy = make.addClassMember(anonClassTree, methodToAdd);
 533                 workingCopy.rewrite(anonClassTree, copy);
 534             }
 535
 536             public void cancel() {
 537             }
 538         };
 539         testSource.runModificationTask(task).commit();
 540         String
  res = TestUtilities.copyFileToString(testFile); 541         System.err.println(res);
 542         assertEquals(golden, res);
 543     }
 544
 545
 548     public void testForNotRegen() throws Exception
  { 549         testFile = new File
  (getWorkDir(), "Test.java"); 550         TestUtilities.copyStringToFile(testFile,
 551             "package personal;\n" +
 552             "\n" +
 553             "import javax.swing.text.Element;\n" +
 554             "import java.util.Collections;\n" +
 555             "\n" +
 556             "public class Test {\n" +
 557             "   public Object method() {\n" +
 558             "        for (int var2 = 0; var2 < 10; var2++) {\n" +
 559             "           // comment\n" +
 560             "           System.out.println(var2); // What a ... comment\n" +
 561             "           // comment\n" +
 562             "           List l;\n" +
 563             "        }\n" +
 564             "    }\n" +
 565             "}\n");
 566
 567          String
  golden = 568             "package personal;\n" +
 569             "\n" +
 570             "import javax.swing.text.Element;\n" +
 571             "import java.util.Collections;\n" +
 572             "\n" +
 573             "public class Test {\n" +
 574             "   public Object method() {\n" +
 575             "        for (int newVar = 0; newVar < 10; newVar++) {\n" +
 576             "           // comment\n" +
 577             "           System.out.println(newVar); // What a ... comment\n" +
 578             "           // comment\n" +
 579             "           List l;\n" +
 580             "        }\n" +
 581             "    }\n" +
 582             "}\n";
 583         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 584         CancellableTask task = new CancellableTask<WorkingCopy>() {
 585
 586             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 587                 workingCopy.toPhase(Phase.RESOLVED);
 588                 TreeMaker make = workingCopy.getTreeMaker();
 589                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 590                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 591                 ForLoopTree forLoop = (ForLoopTree) method.getBody().getStatements().get(0);
 592                                 VariableTree initalizer = (VariableTree) forLoop.getInitializer().get(0);
 594                 workingCopy.rewrite(initalizer, make.setLabel(initalizer, "newVar"));
 595
 596                                 BinaryTree condition = (BinaryTree) forLoop.getCondition();
 598                 IdentifierTree ident = (IdentifierTree) condition.getLeftOperand();
 599                 workingCopy.rewrite(ident, make.setLabel(ident, "newVar"));
 600
 601                 ExpressionStatementTree update = (ExpressionStatementTree) forLoop.getUpdate().get(0);
 602                 UnaryTree unary = (UnaryTree) update.getExpression();
 603                 ident = (IdentifierTree) unary.getExpression();
 604                 workingCopy.rewrite(ident, make.setLabel(ident, "newVar"));
 605
 606                                 BlockTree block = (BlockTree) forLoop.getStatement();
 608                 ExpressionStatementTree systemOut = (ExpressionStatementTree) block.getStatements().get(0);
 609                 MethodInvocationTree mit = (MethodInvocationTree) systemOut.getExpression();
 610                 ident = (IdentifierTree) mit.getArguments().get(0);
 611                 workingCopy.rewrite(ident, make.setLabel(ident, "newVar"));
 612             }
 613
 614             public void cancel() {
 615             }
 616         };
 617         testSource.runModificationTask(task).commit();
 618         String
  res = TestUtilities.copyFileToString(testFile); 619         System.err.println(res);
 620         assertEquals(golden, res);
 621     }
 622
 623
 626     public void testAssignLeft() throws Exception
  { 627         testFile = new File
  (getWorkDir(), "Test.java"); 628         TestUtilities.copyStringToFile(testFile,
 629             "package personal;\n" +
 630             "\n" +
 631             "public class Test {\n" +
 632             "    public Object method() {\n" +
 633             "        this.key = key;\n" +
 634             "    }\n" +
 635             "}\n");
 636
 637          String
  golden = 638             "package personal;\n" +
 639             "\n" +
 640             "public class Test {\n" +
 641             "    public Object method() {\n" +
 642             "        this.key2 = key;\n" +
 643             "    }\n" +
 644             "}\n";
 645         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 646         CancellableTask task = new CancellableTask<WorkingCopy>() {
 647
 648             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 649                 workingCopy.toPhase(Phase.RESOLVED);
 650                 TreeMaker make = workingCopy.getTreeMaker();
 651                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 652                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 653                 ExpressionStatementTree est = (ExpressionStatementTree) method.getBody().getStatements().get(0);
 654                 AssignmentTree assignment = (AssignmentTree) est.getExpression();
 655                 MemberSelectTree mstCopy = make.setLabel((MemberSelectTree) assignment.getVariable(), "key2");
 656                 workingCopy.rewrite(assignment.getVariable(), mstCopy);
 657             }
 658
 659             public void cancel() {
 660             }
 661         };
 662         testSource.runModificationTask(task).commit();
 663         String
  res = TestUtilities.copyFileToString(testFile); 664         System.err.println(res);
 665         assertEquals(golden, res);
 666     }
 667
 668
 671     public void testAssignRight() throws Exception
  { 672         testFile = new File
  (getWorkDir(), "Test.java"); 673         TestUtilities.copyStringToFile(testFile,
 674             "package personal;\n" +
 675             "\n" +
 676             "public class Test {\n" +
 677             "    public Object method() {\n" +
 678             "        this.key = key;\n" +
 679             "    }\n" +
 680             "}\n");
 681
 682          String
  golden = 683             "package personal;\n" +
 684             "\n" +
 685             "public class Test {\n" +
 686             "    public Object method() {\n" +
 687             "        this.key = key2;\n" +
 688             "    }\n" +
 689             "}\n";
 690         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 691         CancellableTask task = new CancellableTask<WorkingCopy>() {
 692
 693             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 694                 workingCopy.toPhase(Phase.RESOLVED);
 695                 TreeMaker make = workingCopy.getTreeMaker();
 696                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 697                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 698                 ExpressionStatementTree est = (ExpressionStatementTree) method.getBody().getStatements().get(0);
 699                 AssignmentTree assignment = (AssignmentTree) est.getExpression();
 700                 IdentifierTree copy = make.setLabel((IdentifierTree) assignment.getExpression(), "key2");
 701                 workingCopy.rewrite(assignment.getExpression(), copy);
 702             }
 703
 704             public void cancel() {
 705             }
 706         };
 707         testSource.runModificationTask(task).commit();
 708         String
  res = TestUtilities.copyFileToString(testFile); 709         System.err.println(res);
 710         assertEquals(golden, res);
 711     }
 712
 713
 716     public void testAssignBoth() throws Exception
  { 717         testFile = new File
  (getWorkDir(), "Test.java"); 718         TestUtilities.copyStringToFile(testFile,
 719             "package personal;\n" +
 720             "\n" +
 721             "public class Test {\n" +
 722             "    public Object method() {\n" +
 723             "        this.key = key;\n" +
 724             "    }\n" +
 725             "}\n");
 726
 727          String
  golden = 728             "package personal;\n" +
 729             "\n" +
 730             "public class Test {\n" +
 731             "    public Object method() {\n" +
 732             "        this.key2 = key2;\n" +
 733             "    }\n" +
 734             "}\n";
 735         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 736         CancellableTask task = new CancellableTask<WorkingCopy>() {
 737
 738             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 739                 workingCopy.toPhase(Phase.RESOLVED);
 740                 TreeMaker make = workingCopy.getTreeMaker();
 741                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 742                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 743                 ExpressionStatementTree est = (ExpressionStatementTree) method.getBody().getStatements().get(0);
 744                 AssignmentTree assignment = (AssignmentTree) est.getExpression();
 745                 MemberSelectTree mstCopy = make.setLabel((MemberSelectTree) assignment.getVariable(), "key2");
 746                 workingCopy.rewrite(assignment.getVariable(), mstCopy);
 747                 IdentifierTree copy = make.setLabel((IdentifierTree) assignment.getExpression(), "key2");
 748                 workingCopy.rewrite(assignment.getExpression(), copy);
 749             }
 750
 751             public void cancel() {
 752             }
 753         };
 754         testSource.runModificationTask(task).commit();
 755         String
  res = TestUtilities.copyFileToString(testFile); 756         System.err.println(res);
 757         assertEquals(golden, res);
 758     }
 759
 760
 763     public void testReturn() throws Exception
  { 764         testFile = new File
  (getWorkDir(), "Test.java"); 765         TestUtilities.copyStringToFile(testFile,
 766             "package personal;\n" +
 767             "\n" +
 768             "public class Test {\n" +
 769             "    public Object method() {\n" +
 770             "        return nullanen;\n" +
 771             "    }\n" +
 772             "}\n");
 773
 774          String
  golden = 775             "package personal;\n" +
 776             "\n" +
 777             "public class Test {\n" +
 778             "    public Object method() {\n" +
 779             "        return nullanen2;\n" +
 780             "    }\n" +
 781             "}\n";
 782         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 783         CancellableTask task = new CancellableTask<WorkingCopy>() {
 784
 785             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 786                 workingCopy.toPhase(Phase.RESOLVED);
 787                 TreeMaker make = workingCopy.getTreeMaker();
 788                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 789                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 790                 ReturnTree rejturn = (ReturnTree) method.getBody().getStatements().get(0);
 791                 workingCopy.rewrite(rejturn.getExpression(), make.Identifier("nullanen2"));
 792             }
 793
 794             public void cancel() {
 795             }
 796         };
 797         testSource.runModificationTask(task).commit();
 798         String
  res = TestUtilities.copyFileToString(testFile); 799         System.err.println(res);
 800         assertEquals(golden, res);
 801     }
 802
 803
 806     public void testPlusBinary() throws Exception
  { 807         testFile = new File
  (getWorkDir(), "Test.java"); 808         TestUtilities.copyStringToFile(testFile,
 809             "package personal;\n" +
 810             "\n" +
 811             "public class Test {\n" +
 812             "    public Object method() {\n" +
 813             "        return \"[\" + key + \"; \" + value + \"]\"\n" +
 814             "    }\n" +
 815             "}\n");
 816
 817          String
  golden = 818             "package personal;\n" +
 819             "\n" +
 820             "public class Test {\n" +
 821             "    public Object method() {\n" +
 822             "        return \"[\" + key2 + \"; \" + value + \"]\"\n" +
 823             "    }\n" +
 824             "}\n";
 825         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 826         CancellableTask task = new CancellableTask<WorkingCopy>() {
 827
 828             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 829                 workingCopy.toPhase(Phase.RESOLVED);
 830                 TreeMaker make = workingCopy.getTreeMaker();
 831                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 832                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 833                 ReturnTree rejturn = (ReturnTree) method.getBody().getStatements().get(0);
 834                 BinaryTree in = (BinaryTree) rejturn.getExpression();
 835                 for (int i = 0; i < 3; i++) {
 836                     in = (BinaryTree) in.getLeftOperand();
 837                 }
 838                 IdentifierTree ident = (IdentifierTree) in.getRightOperand();
 839                 workingCopy.rewrite(ident, make.Identifier("key2"));
 840             }
 841
 842             public void cancel() {
 843             }
 844         };
 845         testSource.runModificationTask(task).commit();
 846         String
  res = TestUtilities.copyFileToString(testFile); 847         System.err.println(res);
 848         assertEquals(golden, res);
 849     }
 850
 851
 854     public void testRenameInWhile() throws Exception
  { 855         testFile = new File
  (getWorkDir(), "Test.java"); 856         TestUtilities.copyStringToFile(testFile,
 857             "package personal;\n" +
 858             "\n" +
 859             "public class Test {\n" +
 860             "    public Object method() {\n" +
 861             "        int i = 0;\n" +
 862             "        while (i < 10) {\n" +
 863             "            i = i + 1;\n" +
 864             "        }\n" +
 865             "    }\n" +
 866             "}\n");
 867
 868          String
  golden = 869             "package personal;\n" +
 870             "\n" +
 871             "public class Test {\n" +
 872             "    public Object method() {\n" +
 873             "        int counter = 0;\n" +
 874             "        while (counter < 10) {\n" +
 875             "            counter = counter + 1;\n" +
 876             "        }\n" +
 877             "    }\n" +
 878             "}\n";
 879         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 880         CancellableTask task = new CancellableTask<WorkingCopy>() {
 881
 882             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 883                 workingCopy.toPhase(Phase.RESOLVED);
 884                 TreeMaker make = workingCopy.getTreeMaker();
 885                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 886                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 887                 VariableTree var = (VariableTree) method.getBody().getStatements().get(0);
 888                 workingCopy.rewrite(var, make.setLabel(var, "counter"));
 889
 890                 WhileLoopTree whileLoop = (WhileLoopTree) method.getBody().getStatements().get(1);
 891                 ParenthesizedTree paren = (ParenthesizedTree) whileLoop.getCondition();
 892                 BinaryTree lessThan = (BinaryTree) paren.getExpression();
 893                 IdentifierTree left = (IdentifierTree) lessThan.getLeftOperand();
 894                 workingCopy.rewrite(left, make.setLabel(left, "counter"));
 895
 896                 ExpressionStatementTree expr = (ExpressionStatementTree) ((BlockTree) whileLoop.getStatement()).getStatements().get(0);
 897                 AssignmentTree assign = (AssignmentTree) expr.getExpression();
 898                 left = (IdentifierTree) assign.getVariable();
 899                 workingCopy.rewrite(left, make.setLabel(left, "counter"));
 900                 BinaryTree right = (BinaryTree) assign.getExpression();
 901                 left = (IdentifierTree) right.getLeftOperand();
 902                 workingCopy.rewrite(left, make.setLabel(left, "counter"));
 903             }
 904
 905             public void cancel() {
 906             }
 907         };
 908         testSource.runModificationTask(task).commit();
 909         String
  res = TestUtilities.copyFileToString(testFile); 910         System.err.println(res);
 911         assertEquals(golden, res);
 912     }
 913
 914
 917     public void testRenameInDoWhile() throws Exception
  { 918         testFile = new File
  (getWorkDir(), "Test.java"); 919         TestUtilities.copyStringToFile(testFile,
 920             "package personal;\n" +
 921             "\n" +
 922             "public class Test {\n" +
 923             "    public Object method() {\n" +
 924             "        int i = 0;\n" +
 925             "        do {\n" +
 926             "            i++;\n" +
 927             "        } while (i > 10);\n" +
 928             "    }\n" +
 929             "}\n");
 930
 931          String
  golden = 932             "package personal;\n" +
 933             "\n" +
 934             "public class Test {\n" +
 935             "    public Object method() {\n" +
 936             "        int counter = 0;\n" +
 937             "        do {\n" +
 938             "            counter++;\n" +
 939             "        } while (counter > 10);\n" +
 940             "    }\n" +
 941             "}\n";
 942         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 943         CancellableTask task = new CancellableTask<WorkingCopy>() {
 944
 945             public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 946                 workingCopy.toPhase(Phase.RESOLVED);
 947                 TreeMaker make = workingCopy.getTreeMaker();
 948                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 949                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
 950                 VariableTree var = (VariableTree) method.getBody().getStatements().get(0);
 951                 workingCopy.rewrite(var, make.setLabel(var, "counter"));
 952
 953                 DoWhileLoopTree doWhileLoop = (DoWhileLoopTree) method.getBody().getStatements().get(1);
 954                 ParenthesizedTree paren = (ParenthesizedTree) doWhileLoop.getCondition();
 955                 BinaryTree lessThan = (BinaryTree) paren.getExpression();
 956                 IdentifierTree left = (IdentifierTree) lessThan.getLeftOperand();
 957                 workingCopy.rewrite(left, make.setLabel(left, "counter"));
 958
 959                 ExpressionStatementTree expr = (ExpressionStatementTree) ((BlockTree) doWhileLoop.getStatement()).getStatements().get(0);
 960                 UnaryTree unary = (UnaryTree) expr.getExpression();
 961                 workingCopy.rewrite(unary.getExpression(), make.setLabel(unary.getExpression(), "counter"));
 962             }
 963
 964             public void cancel() {
 965             }
 966         };
 967         testSource.runModificationTask(task).commit();
 968         String
  res = TestUtilities.copyFileToString(testFile); 969         System.err.println(res);
 970         assertEquals(golden, res);
 971     }
 972
 973
 976     public void testRenameInForEach() throws Exception
  { 977         testFile = new File
  (getWorkDir(), "Test.java"); 978         TestUtilities.copyStringToFile(testFile,
 979             "package personal;\n" +
 980             "\n" +
 981             "public class Test {\n" +
 982             "    public Object method() {\n" +
 983             "        List l = new ArrayList();\n" +
 984             "        for (Object o : l) {\n" +
 985             "            o.toString();\n" +
 986             "        }\n" +
 987             "    }\n" +
 988             "}\n");
 989
 990          String
  golden = 991             "package personal;\n" +
 992             "\n" +
 993             "public class Test {\n" +
 994             "    public Object method() {\n" +
 995             "        List list = new ArrayList();\n" +
 996             "        for (Object object : list) {\n" +
 997             "            object.toString();\n" +
 998             "        }\n" +
 999             "    }\n" +
 1000            "}\n";
 1001
 1002        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1003        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1004
 1005            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1006                workingCopy.toPhase(Phase.RESOLVED);
 1007                TreeMaker make = workingCopy.getTreeMaker();
 1008                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1009                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1010                VariableTree var = (VariableTree) method.getBody().getStatements().get(0);
 1011                workingCopy.rewrite(var, make.setLabel(var, "list"));
 1012
 1013                EnhancedForLoopTree forEach = (EnhancedForLoopTree) method.getBody().getStatements().get(1);
 1014                var = forEach.getVariable();
 1015                workingCopy.rewrite(var, make.setLabel(var, "object"));
 1016                IdentifierTree ident = (IdentifierTree) forEach.getExpression();
 1017                workingCopy.rewrite(ident, make.setLabel(ident, "list"));
 1018                BlockTree body = (BlockTree) forEach.getStatement();
 1019                ExpressionStatementTree est = (ExpressionStatementTree) body.getStatements().get(0);
 1020                MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
 1021                MemberSelectTree mst = (MemberSelectTree) mit.getMethodSelect();
 1022
 1023                workingCopy.rewrite(mst.getExpression(), make.setLabel(mst.getExpression(), "object"));
 1024            }
 1025
 1026            public void cancel() {
 1027            }
 1028        };
 1029        testSource.runModificationTask(task).commit();
 1030        String
  res = TestUtilities.copyFileToString(testFile); 1031        System.err.println(res);
 1032        assertEquals(golden, res);
 1033    }
 1034
 1035
 1038    public void testRenameInSyncro() throws Exception
  { 1039        testFile = new File
  (getWorkDir(), "Test.java"); 1040        TestUtilities.copyStringToFile(testFile,
 1041            "package personal;\n" +
 1042            "\n" +
 1043            "public class Test {\n" +
 1044            "    public Object method() {\n" +
 1045            "        Object lock = new Object();\n" +
 1046            "        \n" +
 1047            "        synchronized(lock) {\n" +
 1048            "            int a = 20;\n" +
 1049            "            lock.wait();\n" +
 1050            "        }\n" +
 1051            "    }\n" +
 1052            "}\n");
 1053
 1054         String
  golden = 1055            "package personal;\n" +
 1056            "\n" +
 1057            "public class Test {\n" +
 1058            "    public Object method() {\n" +
 1059            "        Object zamek = new Object();\n" +
 1060            "        \n" +
 1061            "        synchronized(zamek) {\n" +
 1062            "            int a = 20;\n" +
 1063            "            zamek.wait();\n" +
 1064            "        }\n" +
 1065            "    }\n" +
 1066            "}\n";
 1067
 1068        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1069        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1070
 1071            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1072                workingCopy.toPhase(Phase.RESOLVED);
 1073                TreeMaker make = workingCopy.getTreeMaker();
 1074                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1075                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1076                VariableTree var = (VariableTree) method.getBody().getStatements().get(0);
 1077                workingCopy.rewrite(var, make.setLabel(var, "zamek"));
 1078
 1079                SynchronizedTree syncro = (SynchronizedTree) method.getBody().getStatements().get(1);
 1080                ParenthesizedTree petecko = (ParenthesizedTree) syncro.getExpression();
 1081                IdentifierTree ident = (IdentifierTree) petecko.getExpression();
 1082                workingCopy.rewrite(ident, make.setLabel(ident, "zamek"));
 1083                BlockTree body = (BlockTree) syncro.getBlock();
 1084                ExpressionStatementTree est = (ExpressionStatementTree) body.getStatements().get(1);
 1085                MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
 1086                MemberSelectTree mst = (MemberSelectTree) mit.getMethodSelect();
 1087
 1088                workingCopy.rewrite(mst.getExpression(), make.setLabel(mst.getExpression(), "zamek"));
 1089            }
 1090
 1091            public void cancel() {
 1092            }
 1093        };
 1094        testSource.runModificationTask(task).commit();
 1095        String
  res = TestUtilities.copyFileToString(testFile); 1096        System.err.println(res);
 1097        assertEquals(golden, res);
 1098    }
 1099
 1100
 1103    public void testRenameInCatch() throws Exception
  { 1104        testFile = new File
  (getWorkDir(), "Test.java"); 1105        TestUtilities.copyStringToFile(testFile,
 1106            "package personal;\n" +
 1107            "\n" +
 1108            "public class Test {\n" +
 1109            "    public Object method() {\n" +
 1110            "        Object zamek = new Object();\n" +
 1111            "        try {\n" +
 1112            "            zamek.wait();\n" +
 1113            "        } catch (InterruptedException ex) {\n" +
 1114            "            ex.printStackTrace();\n" +
 1115            "        }\n" +
 1116            "    }\n" +
 1117            "}\n");
 1118
 1119         String
  golden = 1120            "package personal;\n" +
 1121            "\n" +
 1122            "public class Test {\n" +
 1123            "    public Object method() {\n" +
 1124            "        Object zamek = new Object();\n" +
 1125            "        try {\n" +
 1126            "            zamek.wait();\n" +
 1127            "        } catch (InterruptedException vyjimka) {\n" +
 1128            "            vyjimka.printStackTrace();\n" +
 1129            "        }\n" +
 1130            "    }\n" +
 1131            "}\n";
 1132
 1133        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1134        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1135
 1136            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1137                workingCopy.toPhase(Phase.RESOLVED);
 1138                TreeMaker make = workingCopy.getTreeMaker();
 1139                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1140                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1141                VariableTree var = (VariableTree) method.getBody().getStatements().get(0);
 1142                workingCopy.rewrite(var, make.setLabel(var, "zamek"));
 1143                TryTree tryTree = (TryTree) method.getBody().getStatements().get(1);
 1144                CatchTree ct = tryTree.getCatches().get(0);
 1145                workingCopy.rewrite(ct.getParameter(), make.setLabel(ct.getParameter(), "vyjimka"));
 1146                BlockTree body = (BlockTree) ct.getBlock();
 1147                ExpressionStatementTree est = (ExpressionStatementTree) body.getStatements().get(0);
 1148                MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
 1149                MemberSelectTree mst = (MemberSelectTree) mit.getMethodSelect();
 1150                workingCopy.rewrite(mst.getExpression(), make.setLabel(mst.getExpression(), "vyjimka"));
 1151            }
 1152
 1153            public void cancel() {
 1154            }
 1155        };
 1156        testSource.runModificationTask(task).commit();
 1157        String
  res = TestUtilities.copyFileToString(testFile); 1158        System.err.println(res);
 1159        assertEquals(golden, res);
 1160    }
 1161
 1162
 1166    public void testRenameInAssignOp() throws Exception
  { 1167        testFile = new File
  (getWorkDir(), "Test.java"); 1168        TestUtilities.copyStringToFile(testFile,
 1169            "package personal;\n" +
 1170            "\n" +
 1171            "public class Test {\n" +
 1172            "    public Object method() {\n" +
 1173            "        int bits2 = 0;\n" +
 1174            "        bits2 |= 0x12;\n" +
 1175            "    }\n" +
 1176            "}\n");
 1177
 1178         String
  golden = 1179            "package personal;\n" +
 1180            "\n" +
 1181            "public class Test {\n" +
 1182            "    public Object method() {\n" +
 1183            "        int bits = 0;\n" +
 1184            "        bits |= 0x12;\n" +
 1185            "    }\n" +
 1186            "}\n";
 1187
 1188        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1189        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1190
 1191            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1192                workingCopy.toPhase(Phase.RESOLVED);
 1193                TreeMaker make = workingCopy.getTreeMaker();
 1194                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1195                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1196                BlockTree block = method.getBody();
 1197                VariableTree var = (VariableTree) block.getStatements().get(0);
 1198                workingCopy.rewrite(var, make.setLabel(var, "bits"));
 1199                ExpressionStatementTree est = (ExpressionStatementTree) block.getStatements().get(1);
 1200                CompoundAssignmentTree cat = (CompoundAssignmentTree) est.getExpression();
 1201                IdentifierTree ident = (IdentifierTree) cat.getVariable();
 1202                workingCopy.rewrite(ident, make.setLabel(ident, "bits"));
 1203            }
 1204
 1205            public void cancel() {
 1206            }
 1207        };
 1208        testSource.runModificationTask(task).commit();
 1209        String
  res = TestUtilities.copyFileToString(testFile); 1210        System.err.println(res);
 1211        assertEquals(golden, res);
 1212    }
 1213
 1214
 1219    public void testRenameInArrayIndex() throws Exception
  { 1220        testFile = new File
  (getWorkDir(), "Test.java"); 1221        TestUtilities.copyStringToFile(testFile,
 1222            "package personal;\n" +
 1223            "\n" +
 1224            "public class Test {\n" +
 1225            "    public Object method() {\n" +
 1226            "        int pos = 10;\n" +
 1227            "        int[] i = new int[10];\n" +
 1228            "        System.err.println(i[pos-1]);\n" +
 1229            "    }\n" +
 1230            "}\n");
 1231
 1232         String
  golden = 1233            "package personal;\n" +
 1234            "\n" +
 1235            "public class Test {\n" +
 1236            "    public Object method() {\n" +
 1237            "        int position = 10;\n" +
 1238            "        int[] icko = new int[10];\n" +
 1239            "        System.err.println(icko[position-1]);\n" +
 1240            "    }\n" +
 1241            "}\n";
 1242
 1243        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1244        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1245
 1246            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1247                workingCopy.toPhase(Phase.RESOLVED);
 1248                TreeMaker make = workingCopy.getTreeMaker();
 1249                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1250                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1251                BlockTree block = method.getBody();
 1252                VariableTree var1 = (VariableTree) block.getStatements().get(0);
 1253                VariableTree var2 = (VariableTree) block.getStatements().get(1);
 1254                workingCopy.rewrite(var1, make.setLabel(var1, "position"));
 1255                workingCopy.rewrite(var2, make.setLabel(var2, "icko"));
 1256                ExpressionStatementTree est = (ExpressionStatementTree) block.getStatements().get(2);
 1257                MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
 1258                ArrayAccessTree aat = (ArrayAccessTree) mit.getArguments().get(0);
 1259                IdentifierTree ident = (IdentifierTree) aat.getExpression();
 1260                workingCopy.rewrite(ident, make.setLabel(ident, "icko"));
 1261                BinaryTree binary = (BinaryTree) aat.getIndex();
 1262                ident = (IdentifierTree) binary.getLeftOperand();
 1263                workingCopy.rewrite(ident, make.setLabel(ident, "position"));
 1264            }
 1265
 1266            public void cancel() {
 1267            }
 1268        };
 1269        testSource.runModificationTask(task).commit();
 1270        String
  res = TestUtilities.copyFileToString(testFile); 1271        System.err.println(res);
 1272        assertEquals(golden, res);
 1273    }
 1274
 1275
 1278    public void testRenameInTypeCast() throws Exception
  { 1279        testFile = new File
  (getWorkDir(), "Test.java"); 1280        TestUtilities.copyStringToFile(testFile,
 1281            "package personal;\n" +
 1282            "\n" +
 1283            "public class Test {\n" +
 1284            "    static class Item {}\n" +
 1285            "    public Object method() {\n" +
 1286            "        Object o = null;\n" +
 1287            "        Item item = (Item) o;\n" +
 1288            "    }\n" +
 1289            "}\n");
 1290
 1291         String
  golden = 1292            "package personal;\n" +
 1293            "\n" +
 1294            "public class Test {\n" +
 1295            "    static class It {}\n" +
 1296            "    public Object method() {\n" +
 1297            "        Object object = null;\n" +
 1298            "        It it = (It) object;\n" +
 1299            "    }\n" +
 1300            "}\n";
 1301
 1302        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1303        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1304
 1305            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1306                workingCopy.toPhase(Phase.RESOLVED);
 1307                TreeMaker make = workingCopy.getTreeMaker();
 1308                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1309                ClassTree clazzIn = (ClassTree) clazz.getMembers().get(1);
 1310                workingCopy.rewrite(clazzIn, make.setLabel(clazzIn, "It"));
 1311                MethodTree method = (MethodTree) clazz.getMembers().get(2);
 1312                BlockTree block = method.getBody();
 1313                VariableTree var1 = (VariableTree) block.getStatements().get(0);
 1314                VariableTree var2 = (VariableTree) block.getStatements().get(1);
 1315                workingCopy.rewrite(var1, make.setLabel(var1, "object"));
 1316                VariableTree var2copy = make.Variable(
 1317                        var2.getModifiers(),
 1318                        "it",
 1319                        make.Identifier("It"),
 1320                        var2.getInitializer());
 1321                workingCopy.rewrite(var2, var2copy);
 1322                TypeCastTree tct = (TypeCastTree) var2.getInitializer();
 1323                IdentifierTree ident = (IdentifierTree) tct.getType();
 1324                workingCopy.rewrite(ident, make.setLabel(ident, "It"));
 1325                ident = (IdentifierTree) tct.getExpression();
 1326                workingCopy.rewrite(ident, make.setLabel(ident, "object"));
 1327            }
 1328
 1329            public void cancel() {
 1330            }
 1331        };
 1332        testSource.runModificationTask(task).commit();
 1333        String
  res = TestUtilities.copyFileToString(testFile); 1334        System.err.println(res);
 1335        assertEquals(golden, res);
 1336    }
 1337
 1338
 1341    public void testRenameInAssert() throws Exception
  { 1342        testFile = new File
  (getWorkDir(), "Test.java"); 1343        TestUtilities.copyStringToFile(testFile,
 1344            "package personal;\n" +
 1345            "\n" +
 1346            "public class Test {\n" +
 1347            "    public Object method(int a) {\n" +
 1348            "        assert a == 12 : a;\n" +
 1349            "    }\n" +
 1350            "}\n");
 1351
 1352         String
  golden = 1353            "package personal;\n" +
 1354            "\n" +
 1355            "public class Test {\n" +
 1356            "    public Object method(int ada) {\n" +
 1357            "        assert ada == 12 : ada;\n" +
 1358            "    }\n" +
 1359            "}\n";
 1360
 1361        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1362        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1363
 1364            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1365                workingCopy.toPhase(Phase.RESOLVED);
 1366                TreeMaker make = workingCopy.getTreeMaker();
 1367                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1368                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1369                VariableTree vtecko = method.getParameters().get(0);
 1370                workingCopy.rewrite(vtecko, make.setLabel(vtecko, "ada"));
 1371                BlockTree block = method.getBody();
 1372                AssertTree ass = (AssertTree) block.getStatements().get(0);
 1373                BinaryTree cond = (BinaryTree) ass.getCondition();
 1374                IdentifierTree ident = (IdentifierTree) cond.getLeftOperand();
 1375                workingCopy.rewrite(ident, make.setLabel(ident, "ada"));
 1376                ident = (IdentifierTree) ass.getDetail();
 1377                workingCopy.rewrite(ident, make.setLabel(ident, "ada"));
 1378            }
 1379
 1380            public void cancel() {
 1381            }
 1382        };
 1383        testSource.runModificationTask(task).commit();
 1384        String
  res = TestUtilities.copyFileToString(testFile); 1385        System.err.println(res);
 1386        assertEquals(golden, res);
 1387    }
 1388
 1389
 1392    public void testRenameInThrowSt() throws Exception
  { 1393        testFile = new File
  (getWorkDir(), "Test.java"); 1394        TestUtilities.copyStringToFile(testFile,
 1395            "package personal;\n" +
 1396            "\n" +
 1397            "public class Test {\n" +
 1398            "    public Object method() {\n" +
 1399            "        throw new NullPointerException();\n" +
 1400            "    }\n" +
 1401            "}\n");
 1402
 1403         String
  golden = 1404            "package personal;\n" +
 1405            "\n" +
 1406            "public class Test {\n" +
 1407            "    public Object method() {\n" +
 1408            "        throw new EnpeEcko();\n" +
 1409            "    }\n" +
 1410            "}\n";
 1411
 1412        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1413        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1414
 1415            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1416                workingCopy.toPhase(Phase.RESOLVED);
 1417                TreeMaker make = workingCopy.getTreeMaker();
 1418                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1419                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1420                BlockTree block = method.getBody();
 1421                ThrowTree ttecko = (ThrowTree) block.getStatements().get(0);
 1422                NewClassTree nct = (NewClassTree) ttecko.getExpression();
 1423                IdentifierTree ident = (IdentifierTree) nct.getIdentifier();
 1424                workingCopy.rewrite(ident, make.setLabel(ident, "EnpeEcko"));
 1425            }
 1426
 1427            public void cancel() {
 1428            }
 1429        };
 1430        testSource.runModificationTask(task).commit();
 1431        String
  res = TestUtilities.copyFileToString(testFile); 1432        System.err.println(res);
 1433        assertEquals(golden, res);
 1434    }
 1435
 1436
 1439    public void testRenameInConditional() throws Exception
  { 1440        testFile = new File
  (getWorkDir(), "Test.java"); 1441        TestUtilities.copyStringToFile(testFile,
 1442            "package personal;\n" +
 1443            "\n" +
 1444            "public class Test {\n" +
 1445            "    public Object method(int ada) {\n" +
 1446            "        int result = ada == 10 ? ada++ : --ada;\n" +
 1447            "    }\n" +
 1448            "}\n");
 1449
 1450         String
  golden = 1451            "package personal;\n" +
 1452            "\n" +
 1453            "public class Test {\n" +
 1454            "    public Object method(int alda) {\n" +
 1455            "        int result = alda == 10 ? alda++ : --alda;\n" +
 1456            "    }\n" +
 1457            "}\n";
 1458
 1459        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1460        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1461
 1462            public void run(WorkingCopy workingCopy) throws java.io.IOException
  { 1463                workingCopy.toPhase(Phase.RESOLVED);
 1464                TreeMaker make = workingCopy.getTreeMaker();
 1465                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1466                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1467                VariableTree vtecko = method.getParameters().get(0);
 1468                workingCopy.rewrite(vtecko, make.setLabel(vtecko, "alda"));
 1469                BlockTree block = method.getBody();
 1470                VariableTree var = (VariableTree) block.getStatements().get(0);
 1471                ConditionalExpressionTree cet = (ConditionalExpressionTree) var.getInitializer();
 1472                BinaryTree cond = (BinaryTree) cet.getCondition();
 1473                IdentifierTree ident = (IdentifierTree) cond.getLeftOperand();
 1474                workingCopy.rewrite(ident, make.setLabel(ident, "alda"));
 1475                UnaryTree truePart = (UnaryTree) cet.getTrueExpression();
 1476                ident = (IdentifierTree) truePart.getExpression();
 1477                workingCopy.rewrite(ident, make.setLabel(ident, "alda"));
 1478                UnaryTree falsePart = (UnaryTree) cet.getFalseExpression();
 1479                ident = (IdentifierTree) falsePart.getExpression();
 1480                workingCopy.rewrite(ident, make.setLabel(ident, "alda"));
 1481            }
 1482
 1483            public void cancel() {
 1484            }
 1485        };
 1486        testSource.runModificationTask(task).commit();
 1487        String
  res = TestUtilities.copyFileToString(testFile); 1488        System.err.println(res);
 1489        assertEquals(golden, res);
 1490    }
 1491
 1492
 1495    public void testRenameInLabelled() throws Exception
  { 1496        testFile = new File
  (getWorkDir(), "Test.java"); 1497        TestUtilities.copyStringToFile(testFile, "package personal;\n\npublic class Test {\n    public Object method() {\n        cycle_start: for (int i = 0; i < 10; i++) {\n        }\n    }\n}\n");
 1498        String
  golden = "package personal;\n\npublic class Test {\n    public Object method() {\n        zacatek_smycky: for (int i = 0; i < 10; i++) {\n        }\n    }\n}\n"; 1499        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1500        CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() {
 1501
 1502            public void run(WorkingCopy workingCopy) throws IOException
  { 1503                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1504                TreeMaker make = workingCopy.getTreeMaker();
 1505                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1506                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1507                BlockTree block = method.getBody();
 1508                LabeledStatementTree lst = (LabeledStatementTree)block.getStatements().get(0);
 1509                workingCopy.rewrite(lst, make.setLabel(lst, "zacatek_smycky"));
 1510            }
 1511
 1512            public void cancel() {
 1513            }
 1514
 1515        };
 1516        testSource.runModificationTask(task).commit();
 1517        String
  res = TestUtilities.copyFileToString(testFile); 1518        System.err.println(res);
 1519        assertEquals(golden, res);
 1520    }
 1521
 1522
 1525    public void testRenameInContinue()
 1526            throws Exception
  { 1527        testFile = new File
  (getWorkDir(), "Test.java"); 1528        TestUtilities.copyStringToFile(testFile, "package personal;\n\npublic class Test {\n    public Object method() {\n        cycle_start: for (int i = 0; i < 10; i++) {\n            continue cycle_start;\n        }\n    }\n}\n");
 1529        String
  golden = "package personal;\n\npublic class Test {\n    public Object method() {\n        zacatek_smycky: for (int i = 0; i < 10; i++) {\n            continue zacatek_smycky;\n        }\n    }\n}\n"; 1530        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1531        CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() {
 1532
 1533            public void run(WorkingCopy workingCopy) throws IOException
  { 1534                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1535                TreeMaker make = workingCopy.getTreeMaker();
 1536                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1537                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1538                BlockTree block = method.getBody();
 1539                LabeledStatementTree lst = (LabeledStatementTree)block.getStatements().get(0);
 1540                workingCopy.rewrite(lst, make.setLabel(lst, "zacatek_smycky"));
 1541                ForLoopTree flt = (ForLoopTree)lst.getStatement();
 1542                BlockTree forTree = (BlockTree)flt.getStatement();
 1543                ContinueTree ct = (ContinueTree)forTree.getStatements().get(0);
 1544                workingCopy.rewrite(ct, make.setLabel(ct, "zacatek_smycky"));
 1545            }
 1546
 1547            public void cancel() {
 1548            }
 1549        };
 1550        testSource.runModificationTask(task).commit();
 1551        String
  res = TestUtilities.copyFileToString(testFile); 1552        System.err.println(res);
 1553        assertEquals(golden, res);
 1554    }
 1555
 1556
 1559    public void testRenameInBreak() throws Exception
  { 1560        testFile = new File
  (getWorkDir(), "Test.java"); 1561        TestUtilities.copyStringToFile(testFile, "package personal;\n\npublic class Test {\n    public Object method() {\n        cycle_start: for (int i = 0; i < 10; i++) {\n            break cycle_start;\n        }\n    }\n}\n");
 1562        String
  golden = "package personal;\n\npublic class Test {\n    public Object method() {\n        zacatek_smycky: for (int i = 0; i < 10; i++) {\n            break zacatek_smycky;\n        }\n    }\n}\n"; 1563        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1564        CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() {
 1565
 1566            public void run(WorkingCopy workingCopy)
 1567                    throws IOException
  { 1568                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1569                TreeMaker make = workingCopy.getTreeMaker();
 1570                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1571                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1572                BlockTree block = method.getBody();
 1573                LabeledStatementTree lst = (LabeledStatementTree)block.getStatements().get(0);
 1574                workingCopy.rewrite(lst, make.setLabel(lst, "zacatek_smycky"));
 1575                ForLoopTree flt = (ForLoopTree)lst.getStatement();
 1576                BlockTree forTree = (BlockTree)flt.getStatement();
 1577                BreakTree bt = (BreakTree)forTree.getStatements().get(0);
 1578                workingCopy.rewrite(bt, make.setLabel(bt, "zacatek_smycky"));
 1579            }
 1580
 1581            public void cancel() {
 1582            }
 1583        };
 1584        testSource.runModificationTask(task).commit();
 1585        String
  res = TestUtilities.copyFileToString(testFile); 1586        System.err.println(res);
 1587        assertEquals(golden, res);
 1588    }
 1589
 1590
 1593    public void testRenameLocVarTypePar() throws Exception
  { 1594        testFile = new File
  (getWorkDir(), "Test.java"); 1595        testFile = new File
  (getWorkDir(), "Test.java"); 1596        TestUtilities.copyStringToFile(testFile,
 1597            "package personal;\n\n" +
 1598            "import java.util.*;\n" +
 1599            "\n" +
 1600            "public class Test {\n" +
 1601            "    public Object method(int a) {\n" +
 1602            "        Map<String,Data> map1 = new HashMap<String,Data>();\n" +
 1603            "        Map<Data,String> map2 = new TreeMap<Data, String>();\n" +
 1604            "    }\n" +
 1605            "}\n");
 1606
 1607         String
  golden = 1608            "package personal;\n\n" +
 1609            "import java.util.*;\n" +
 1610            "\n" +
 1611            "public class Test {\n" +
 1612            "    public Object method(int a) {\n" +
 1613            "        Map<String,DataRen> map1 = new HashMap<String,DataRen>();\n" +
 1614            "        Map<DataRen,String> map2 = new TreeMap<DataRen, String>();\n" +
 1615            "    }\n" +
 1616            "}\n";
 1617
 1618        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1619        CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() {
 1620
 1621            public void run(WorkingCopy workingCopy)
 1622                    throws IOException
  { 1623                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1624                TreeMaker make = workingCopy.getTreeMaker();
 1625                ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1626                MethodTree method = (MethodTree) clazz.getMembers().get(1);
 1627                BlockTree block = method.getBody();
 1628
 1629                VariableTree vt = (VariableTree) block.getStatements().get(0);
 1630                ParameterizedTypeTree ptt = (ParameterizedTypeTree) vt.getType();
 1631                IdentifierTree it = (IdentifierTree) ptt.getTypeArguments().get(1);
 1632                workingCopy.rewrite(it, make.setLabel(it, "DataRen"));
 1633
 1634                NewClassTree nct = (NewClassTree) vt.getInitializer();
 1635                ptt = (ParameterizedTypeTree) nct.getIdentifier();
 1636                it = (IdentifierTree) ptt.getTypeArguments().get(1);
 1637                workingCopy.rewrite(it, make.setLabel(it, "DataRen"));
 1638
 1639                vt = (VariableTree) block.getStatements().get(1);
 1640                ptt = (ParameterizedTypeTree) vt.getType();
 1641                it = (IdentifierTree) ptt.getTypeArguments().get(0);
 1642                workingCopy.rewrite(it, make.setLabel(it, "DataRen"));
 1643                nct = (NewClassTree) vt.getInitializer();
 1644                ptt = (ParameterizedTypeTree) nct.getIdentifier();
 1645                it = (IdentifierTree) ptt.getTypeArguments().get(0);
 1646                workingCopy.rewrite(it, make.setLabel(it, "DataRen"));
 1647            }
 1648
 1649            public void cancel() {
 1650            }
 1651        };
 1652        testSource.runModificationTask(task).commit();
 1653        String
  res = TestUtilities.copyFileToString(testFile); 1654        System.err.println(res);
 1655        assertEquals(golden, res);
 1656    }
 1657
 1658
 1661    public void testRenameInSwitch() throws Exception
  { 1662        testFile = new File
  (getWorkDir(), "Test.java"); 1663        TestUtilities.copyStringToFile(testFile,
 1664            "package personal;\n" +
 1665            "\n" +
 1666            "public class Test {\n" +
 1667            "    public Object method(int la) {\n" +
 1668            "        switch (la) {\n" +
 1669            "            case 0:\n" +
 1670            "                break;\n" +
 1671            "            case 1:\n" +
 1672            "                break;\n" +
 1673            "           default:\n" +
 1674            "                // do nothing\n" +
 1675            "        }\n" +
 1676            "    }\n" +
 1677            "}\n");
 1678
 1679         String
  golden = 1680            "package personal;\n" +
 1681            "\n" +
 1682            "public class Test {\n" +
 1683            "    public Object method(int renamed) {\n" +
 1684            "        switch (renamed) {\n" +
 1685            "            case 0:\n" +
 1686            "                break;\n" +
 1687            "            case 1:\n" +
 1688            "                break;\n" +
 1689            "           default:\n" +
 1690            "                // do nothing\n" +
 1691            "        }\n" +
 1692            "    }\n" +
 1693            "}\n";
 1694
 1695        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1696        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1697
 1698            public void run(WorkingCopy workingCopy) throws IOException
  { 1699                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1700                TreeMaker make = workingCopy.getTreeMaker();
 1701                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1702                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1703                                VariableTree vt = method.getParameters().get(0);
 1705                workingCopy.rewrite(vt, make.setLabel(vt, "renamed"));
 1706                                BlockTree block = method.getBody();
 1708                SwitchTree swicStrom = (SwitchTree) block.getStatements().get(0);
 1709                ParenthesizedTree pTree = (ParenthesizedTree) swicStrom.getExpression();
 1710                IdentifierTree ident = (IdentifierTree) pTree.getExpression();
 1711                workingCopy.rewrite(ident, make.setLabel(ident, "renamed"));
 1712            }
 1713
 1714            public void cancel() {
 1715            }
 1716        };
 1717        testSource.runModificationTask(task).commit();
 1718        String
  res = TestUtilities.copyFileToString(testFile); 1719        System.err.println(res);
 1720        assertEquals(golden, res);
 1721    }
 1722
 1723
 1727    public void testRenameInTypeNewArr() throws Exception
  { 1728        testFile = new File
  (getWorkDir(), "Test.java"); 1729        TestUtilities.copyStringToFile(testFile,
 1730            "package personal;\n" +
 1731            "\n" +
 1732            "public class Test {\n" +
 1733            "    public Object method() {\n" +
 1734            "        int dim1 = 10;\n" +
 1735            "        int dim2 = 15;\n" +
 1736            "        Test[][] obj = new Test[dim1][dim2];\n" +
 1737            "    }\n" +
 1738            "}\n");
 1739
 1740         String
  golden = 1741            "package personal;\n" +
 1742            "\n" +
 1743            "public class RenamedTest {\n" +
 1744            "    public Object method() {\n" +
 1745            "        int dim1 = 10;\n" +
 1746            "        int dim2 = 15;\n" +
 1747            "        RenamedTest[][] obj = new RenamedTest[dim1][dim2];\n" +
 1748            "    }\n" +
 1749            "}\n";
 1750
 1751        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1752        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1753
 1754            public void run(WorkingCopy workingCopy) throws IOException
  { 1755                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1756                TreeMaker make = workingCopy.getTreeMaker();
 1757                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1758                workingCopy.rewrite(clazz, make.setLabel(clazz, "RenamedTest"));
 1759                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1760                BlockTree block = method.getBody();
 1761                                VariableTree vt = (VariableTree) block.getStatements().get(2);
 1763                ArrayTypeTree att = (ArrayTypeTree) vt.getType();
 1764                att = (ArrayTypeTree) att.getType();                 workingCopy.rewrite(att.getType(), make.Identifier("RenamedTest"));
 1766                                NewArrayTree nat = (NewArrayTree) vt.getInitializer();
 1768                workingCopy.rewrite(nat.getType(), make.Identifier("RenamedTest"));
 1769            }
 1770
 1771            public void cancel() {
 1772            }
 1773        };
 1774        testSource.runModificationTask(task).commit();
 1775        String
  res = TestUtilities.copyFileToString(testFile); 1776        System.err.println(res);
 1777        assertEquals(golden, res);
 1778    }
 1779
 1780
 1783    public void testRenameInTypeTest() throws Exception
  { 1784        testFile = new File
  (getWorkDir(), "Test.java"); 1785        TestUtilities.copyStringToFile(testFile,
 1786            "package personal;\n" +
 1787            "\n" +
 1788            "public class Test {\n" +
 1789            "    public Object method(Object o) {\n" +
 1790            "        if (o instanceof Test) {\n" +
 1791            "        }\n" +
 1792            "    }\n" +
 1793            "}\n");
 1794
 1795         String
  golden = 1796            "package personal;\n" +
 1797            "\n" +
 1798            "public class Test {\n" +
 1799            "    public Object method(Object obj) {\n" +
 1800            "        if (obj instanceof Test) {\n" +
 1801            "        }\n" +
 1802            "    }\n" +
 1803            "}\n";
 1804
 1805        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1806        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1807
 1808            public void run(WorkingCopy workingCopy) throws IOException
  { 1809                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1810                TreeMaker make = workingCopy.getTreeMaker();
 1811                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1812                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1813                                VariableTree vt = method.getParameters().get(0);
 1815                workingCopy.rewrite(vt, make.setLabel(vt, "obj"));
 1816                                BlockTree block = method.getBody();
 1818                IfTree iv = (IfTree) block.getStatements().get(0);
 1819                ParenthesizedTree pt = (ParenthesizedTree) iv.getCondition();
 1820                InstanceOfTree iot = (InstanceOfTree) pt.getExpression();
 1821                IdentifierTree ident = (IdentifierTree) iot.getExpression();
 1822                workingCopy.rewrite(ident, make.Identifier("obj"));
 1823            }
 1824
 1825            public void cancel() {
 1826            }
 1827        };
 1828        testSource.runModificationTask(task).commit();
 1829        String
  res = TestUtilities.copyFileToString(testFile); 1830        System.err.println(res);
 1831        assertEquals(golden, res);
 1832    }
 1833
 1834
 1837    public void testRenameInTypeTestII() throws Exception
  { 1838        testFile = new File
  (getWorkDir(), "Test.java"); 1839        TestUtilities.copyStringToFile(testFile,
 1840            "package personal;\n" +
 1841            "\n" +
 1842            "public class Test {\n" +
 1843            "    public Object method(Object o) {\n" +
 1844            "        if (o instanceof Test) {\n" +
 1845            "        }\n" +
 1846            "    }\n" +
 1847            "}\n");
 1848
 1849         String
  golden = 1850            "package personal;\n" +
 1851            "\n" +
 1852            "public class RenamedTest {\n" +
 1853            "    public Object method(Object o) {\n" +
 1854            "        if (o instanceof RenamedTest) {\n" +
 1855            "        }\n" +
 1856            "    }\n" +
 1857            "}\n";
 1858
 1859        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1860        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1861
 1862            public void run(WorkingCopy workingCopy) throws IOException
  { 1863                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1864                TreeMaker make = workingCopy.getTreeMaker();
 1865                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1866                workingCopy.rewrite(clazz, make.setLabel(clazz, "RenamedTest"));
 1867                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1868                                BlockTree block = method.getBody();
 1870                IfTree iv = (IfTree) block.getStatements().get(0);
 1871                ParenthesizedTree pt = (ParenthesizedTree) iv.getCondition();
 1872                InstanceOfTree iot = (InstanceOfTree) pt.getExpression();
 1873                IdentifierTree ident = (IdentifierTree) iot.getType();
 1874                workingCopy.rewrite(ident, make.Identifier("RenamedTest"));
 1875            }
 1876
 1877            public void cancel() {
 1878            }
 1879        };
 1880        testSource.runModificationTask(task).commit();
 1881        String
  res = TestUtilities.copyFileToString(testFile); 1882        System.err.println(res);
 1883        assertEquals(golden, res);
 1884    }
 1885
 1886
 1889    public void testChangeLiteral() throws Exception
  { 1890        testFile = new File
  (getWorkDir(), "Test.java"); 1891        TestUtilities.copyStringToFile(testFile,
 1892            "package personal;\n" +
 1893            "\n" +
 1894            "public class Test {\n" +
 1895            "    public Object method(Object o) {\n" +
 1896            "        System.err.println(\"Karel\");\n" +
 1897            "    }\n" +
 1898            "}\n");
 1899
 1900         String
  golden = 1901            "package personal;\n" +
 1902            "\n" +
 1903            "public class RenamedTest {\n" +
 1904            "    public Object method(Object o) {\n" +
 1905            "        System.err.println(\"Hrebejk\");\n" +
 1906            "    }\n" +
 1907            "}\n";
 1908
 1909        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1910        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1911
 1912            public void run(WorkingCopy workingCopy) throws IOException
  { 1913                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1914                TreeMaker make = workingCopy.getTreeMaker();
 1915                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1916                workingCopy.rewrite(clazz, make.setLabel(clazz, "RenamedTest"));
 1917                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1918                                BlockTree block = method.getBody();
 1920                ExpressionStatementTree expr = (ExpressionStatementTree) block.getStatements().get(0);
 1921                MethodInvocationTree invocation = (MethodInvocationTree) expr.getExpression();
 1922                LiteralTree literal = (LiteralTree) invocation.getArguments().get(0);
 1923                workingCopy.rewrite(literal, make.Literal("Hrebejk"));
 1924            }
 1925
 1926            public void cancel() {
 1927            }
 1928        };
 1929        testSource.runModificationTask(task).commit();
 1930        String
  res = TestUtilities.copyFileToString(testFile); 1931        System.err.println(res);
 1932        assertEquals(golden, res);
 1933    }
 1934
 1935
 1938    public void testRenameInArrInit() throws Exception
  { 1939        testFile = new File
  (getWorkDir(), "Test.java"); 1940        TestUtilities.copyStringToFile(testFile,
 1941            "package personal;\n" +
 1942            "\n" +
 1943            "public class Test {\n" +
 1944            "    public Object method(Object o) {\n" +
 1945            "        Inner inInst = new Inner();\n" +
 1946            "        Inner[] inArr = new Inner[] { inInst, new Inner() };\n" +
 1947            "    }\n" +
 1948            "    private static class Inner {\n" +
 1949            "        public Inner() {\n" +
 1950            "        }\n" +
 1951            "    }\n" +
 1952            "}\n");
 1953         String
  golden = 1954            "package personal;\n" +
 1955            "\n" +
 1956            "public class RenamedTest {\n" +
 1957            "    public Object method(Object o) {\n" +
 1958            "        Inner inInst = new Inner();\n" +
 1959            "        Inner[] inArr = new Inner[] { inInst, new Inner() };\n" +
 1960            "    }\n" +
 1961            "    private static class Inner {\n" +
 1962            "        public Inner() {\n" +
 1963            "        }\n" +
 1964            "    }\n" +
 1965            "}\n";
 1966        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 1967        CancellableTask task = new CancellableTask<WorkingCopy>() {
 1968
 1969            public void run(WorkingCopy workingCopy) throws IOException
  { 1970                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 1971                TreeMaker make = workingCopy.getTreeMaker();
 1972                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 1973                workingCopy.rewrite(clazz, make.setLabel(clazz, "RenamedTest"));
 1974                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 1975                                BlockTree block = method.getBody();
 1977            }
 1978
 1979            public void cancel() {
 1980            }
 1981        };
 1982        testSource.runModificationTask(task).commit();
 1983        String
  res = TestUtilities.copyFileToString(testFile); 1984        System.err.println(res);
 1985        assertEquals(golden, res);
 1986    }
 1987
 1988
 1993    public void testRenameClazz() throws Exception
  { 1994        testFile = new File
  (getWorkDir(), "Test.java"); 1995        TestUtilities.copyStringToFile(testFile,
 1996            "package personal;\n" +
 1997            "\n" +
 1998            "public class Test {\n" +
 1999            "    public Object method(Class o) {\n" +
 2000            "        method(Test.class);\n" +
 2001            "    }\n" +
 2002            "}\n");
 2003         String
  golden = 2004            "package personal;\n" +
 2005            "\n" +
 2006            "public class RenamedTest {\n" +
 2007            "    public Object method(Class o) {\n" +
 2008            "        method(RenamedTest.class);\n" +
 2009            "    }\n" +
 2010            "}\n";
 2011        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 2012        CancellableTask task = new CancellableTask<WorkingCopy>() {
 2013
 2014            public void run(WorkingCopy workingCopy) throws IOException
  { 2015                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 2016                TreeMaker make = workingCopy.getTreeMaker();
 2017                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 2018                workingCopy.rewrite(clazz, make.setLabel(clazz, "RenamedTest"));
 2019                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 2020                                BlockTree block = method.getBody();
 2022                ExpressionStatementTree est = (ExpressionStatementTree) block.getStatements().get(0);
 2023                MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
 2024                MemberSelectTree mst = (MemberSelectTree) mit.getArguments().get(0);
 2025                workingCopy.rewrite(mst.getExpression(), make.Identifier("RenamedTest"));
 2026            }
 2027
 2028            public void cancel() {
 2029            }
 2030        };
 2031        testSource.runModificationTask(task).commit();
 2032        String
  res = TestUtilities.copyFileToString(testFile); 2033        System.err.println(res);
 2034        assertEquals(golden, res);
 2035    }
 2036
 2037
 2040    public void testRenameInCase() throws Exception
  { 2041        testFile = new File
  (getWorkDir(), "Test.java"); 2042        TestUtilities.copyStringToFile(testFile,
 2043            "package personal;\n" +
 2044            "\n" +
 2045            "public class Test {\n" +
 2046            "    void method() {\n" +
 2047            "        int i = 10;\n" +
 2048            "        switch (i) {\n" +
 2049            "            case 0: {\n" +
 2050            "                System.err.println(i);\n" +
 2051            "            }\n" +
 2052            "            case 1:\n" +
 2053            "                i = 12;\n" +
 2054            "            default:\n" +
 2055            "                i += 7;\n" +
 2056            "                break;\n" +
 2057            "        }\n" +
 2058            "    }\n" +
 2059            "}\n");
 2060
 2061         String
  golden = 2062            "package personal;\n" +
 2063            "\n" +
 2064            "public class Test {\n" +
 2065            "    void method() {\n" +
 2066            "        int ycko = 10;\n" +
 2067            "        switch (ycko) {\n" +
 2068            "            case 0: {\n" +
 2069            "                System.err.println(ycko);\n" +
 2070            "            }" +
 2071            "case 1:\n" +
 2072            "                ycko = 12;\n" +
 2073            "            default:\n" +
 2074            "                ycko += 7;\n" +
 2075            "                break;\n" +
 2076            "        }\n" +
 2077            "    }\n" +
 2078            "}\n";
 2079        JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
 2080        CancellableTask task = new CancellableTask<WorkingCopy>() {
 2081
 2082            public void run(WorkingCopy workingCopy) throws IOException
  { 2083                workingCopy.toPhase(org.netbeans.api.java.source.JavaSource.Phase.RESOLVED);
 2084                TreeMaker make = workingCopy.getTreeMaker();
 2085                ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
 2086                MethodTree method = (MethodTree)clazz.getMembers().get(1);
 2087                BlockTree bt = method.getBody();
 2088                VariableTree vt = (VariableTree) bt.getStatements().get(0);
 2089                workingCopy.rewrite(vt, make.setLabel(vt, "ycko"));
 2090                SwitchTree st = (SwitchTree) bt.getStatements().get(1);
 2091                ParenthesizedTree pt = (ParenthesizedTree) st.getExpression();
 2092                workingCopy.rewrite(pt.getExpression(), make.setLabel(pt.getExpression(), "ycko"));
 2093                CaseTree kejs = st.getCases().get(0);
 2094                bt = (BlockTree) kejs.getStatements().get(0);
 2095                ExpressionStatementTree est = (ExpressionStatementTree) bt.getStatements().get(0);
 2096                MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
 2097                workingCopy.rewrite(mit.getArguments().get(0), make.Identifier("ycko"));
 2098                kejs = st.getCases().get(1);
 2099                est = (ExpressionStatementTree) kejs.getStatements().get(0);
 2100                AssignmentTree at = (AssignmentTree) est.getExpression();
 2101                workingCopy.rewrite(at.getVariable(), make.setLabel(at.getVariable(), "ycko"));
 2102                kejs = st.getCases().get(2);
 2103                est = (ExpressionStatementTree) kejs.getStatements().get(0);
 2104                CompoundAssignmentTree cat = (CompoundAssignmentTree) est.getExpression();
 2105                workingCopy.rewrite(cat.getVariable(), make.setLabel(cat.getVariable(), "ycko"));
 2106            }
 2107
 2108            public void cancel() {
 2109            }
 2110        };
 2111        testSource.runModificationTask(task).commit();
 2112        String
  res = TestUtilities.copyFileToString(testFile); 2113        System.err.println(res);
 2114        assertEquals(golden, res);
 2115    }
 2116
 2117        String
  getGoldenPckg() { 2119        return "";
 2120    }
 2121
 2122    String
  getSourcePckg() { 2123        return "";
 2124    }
 2125
 2126}
 2127
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |