1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import com.sun.source.tree.TypeParameterTree; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.EnumSet ; 29 import java.util.List ; 30 import javax.lang.model.element.Modifier; 31 import javax.lang.model.type.TypeKind; 32 import org.netbeans.api.java.source.CancellableTask; 33 import org.netbeans.api.java.source.JavaSource; 34 import org.netbeans.api.java.source.JavaSource.Phase; 35 import org.netbeans.api.java.source.TestUtilities; 36 import org.netbeans.api.java.source.TreeMaker; 37 import org.netbeans.api.java.source.WorkingCopy; 38 import org.netbeans.junit.NbTestSuite; 39 40 44 public class ClassMemberTest extends GeneratorTestMDRCompat { 45 46 47 public ClassMemberTest(String testName) { 48 super(testName); 49 } 50 51 public static NbTestSuite suite() { 52 NbTestSuite suite = new NbTestSuite(); 53 suite.addTest(new ClassMemberTest("testAddAtIndex0")); 55 suite.addTest(new ClassMemberTest("testAddAtIndex2")); 56 suite.addTest(new ClassMemberTest("testAddToEmpty")); 57 suite.addTest(new ClassMemberTest("testAddConstructor")); 58 suite.addTest(new ClassMemberTest("testInsertFieldToIndex0")); 59 suite.addTest(new ClassMemberTest("testModifyFieldName")); 60 suite.addTest(new ClassMemberTest("testModifyModifiers")); 61 suite.addTest(new ClassMemberTest("testAddToEmptyInterface")); 62 suite.addTest(new ClassMemberTest("testAddNewClassWithNewMembers")); 63 suite.addTest(new ClassMemberTest("testAddInnerInterface")); 64 suite.addTest(new ClassMemberTest("testAddInnerAnnotationType")); 65 suite.addTest(new ClassMemberTest("testAddInnerEnum")); 66 suite.addTest(new ClassMemberTest("testAddAfterEmptyInit1")); 67 return suite; 69 } 70 71 public void testAddAtIndex0() throws Exception { 72 testFile = new File (getWorkDir(), "Test.java"); 73 TestUtilities.copyStringToFile(testFile, 74 "package hierbas.del.litoral;\n\n" + 75 "public class Test {\n" + 76 " \n" + 77 " public void taragui() {\n" + 78 " }\n" + 79 " \n" + 80 "}\n" 81 ); 82 String golden = 83 "package hierbas.del.litoral;\n\n" + 84 "public class Test {\n" + 85 " \n" + 86 " \n" + 87 " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" + 88 " }\n" + 89 "\n" + 90 " public void taragui() {\n" + 91 " }\n" + 92 " \n" + 93 "}\n"; 94 95 JavaSource src = getJavaSource(testFile); 96 CancellableTask task = new CancellableTask<WorkingCopy>() { 97 98 public void run(WorkingCopy workingCopy) throws IOException { 99 workingCopy.toPhase(Phase.RESOLVED); 100 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 101 TreeMaker make = workingCopy.getTreeMaker(); 102 103 for (Tree typeDecl : cut.getTypeDecls()) { 104 if (Tree.Kind.CLASS == typeDecl.getKind()) { 106 ClassTree classTree = (ClassTree) typeDecl; 107 ClassTree copy = make.insertClassMember(classTree, 0, m(make)); 108 workingCopy.rewrite(classTree, copy); 109 } 110 } 111 } 112 113 public void cancel() { 114 } 115 }; 116 src.runModificationTask(task).commit(); 117 String res = TestUtilities.copyFileToString(testFile); 118 System.err.println(res); 119 assertEquals(golden, res); 120 } 121 122 public void testAddAtIndex2() throws Exception { 123 testFile = new File (getWorkDir(), "Test.java"); 125 TestUtilities.copyStringToFile(testFile, 126 "package hierbas.del.litoral;\n\n" + 127 "public class Test {\n" + 128 " public void taragui() {\n" + 129 " }\n" + 130 " \n" + 131 "}\n" 132 ); 133 String golden = 134 "package hierbas.del.litoral;\n\n" + 135 "public class Test {\n" + 136 " public void taragui() {\n" + 137 " }\n" + 138 " \n" + 139 " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" + 140 " }\n" + 141 "\n" + 142 "}\n"; 143 144 JavaSource src = getJavaSource(testFile); 145 CancellableTask task = new CancellableTask<WorkingCopy>() { 146 147 public void run(WorkingCopy workingCopy) throws IOException { 148 workingCopy.toPhase(Phase.RESOLVED); 149 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 150 TreeMaker make = workingCopy.getTreeMaker(); 151 for (Tree typeDecl : cut.getTypeDecls()) { 152 if (Tree.Kind.CLASS == typeDecl.getKind()) { 154 ClassTree classTree = (ClassTree) typeDecl; 155 ClassTree copy = make.insertClassMember(classTree, 2, m(make)); 156 workingCopy.rewrite(classTree, copy); 157 } 158 } 159 } 160 161 public void cancel() { 162 } 163 }; 164 src.runModificationTask(task).commit(); 165 String res = TestUtilities.copyFileToString(testFile); 166 System.err.println(res); 167 assertEquals(golden, res); 168 } 169 170 public void testAddToEmpty() throws Exception { 171 testFile = new File (getWorkDir(), "Test.java"); 173 TestUtilities.copyStringToFile(testFile, 174 "package hierbas.del.litoral;\n\n" + 175 "public class Test {\n" + 176 "}\n" 177 ); 178 String golden = 179 "package hierbas.del.litoral;\n\n" + 180 "public class Test {\n\n" + 181 " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" + 182 " }\n" + 183 "}\n"; 184 185 JavaSource src = getJavaSource(testFile); 186 CancellableTask task = new CancellableTask<WorkingCopy>() { 187 188 public void run(WorkingCopy workingCopy) throws IOException { 189 workingCopy.toPhase(Phase.RESOLVED); 190 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 191 TreeMaker make = workingCopy.getTreeMaker(); 192 193 for (Tree typeDecl : cut.getTypeDecls()) { 194 if (Tree.Kind.CLASS == typeDecl.getKind()) { 196 ClassTree classTree = (ClassTree) typeDecl; 197 ClassTree copy = make.addClassMember(classTree, m(make)); 198 workingCopy.rewrite(classTree, copy); 199 } 200 } 201 } 202 203 public void cancel() { 204 } 205 }; 206 src.runModificationTask(task).commit(); 207 String res = TestUtilities.copyFileToString(testFile); 208 System.err.println(res); 209 assertEquals(golden, res); 210 } 211 212 public void testAddConstructor() throws Exception { 213 testFile = new File (getWorkDir(), "Test.java"); 214 TestUtilities.copyStringToFile(testFile, 215 "package hierbas.del.litoral;\n\n" + 216 "public class Test {\n" + 217 " \n" + 218 " String prefix;\n" + 219 " \n" + 220 " public void method() {\n" + 221 " }\n" + 222 " \n" + 223 "}\n" 224 ); 225 String golden = 226 "package hierbas.del.litoral;\n\n" + 227 "public class Test {\n" + 228 " \n" + 229 " String prefix;\n" + 230 " \n" + 231 " public Test(boolean prefix) {\n" + 232 " }\n" + 233 "\n" + 234 " public void method() {\n" + 235 " }\n" + 236 " \n" + 237 "}\n"; 238 239 JavaSource src = getJavaSource(testFile); 240 CancellableTask task = new CancellableTask<WorkingCopy>() { 241 242 public void run(WorkingCopy workingCopy) throws IOException { 243 workingCopy.toPhase(Phase.RESOLVED); 244 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 245 TreeMaker make = workingCopy.getTreeMaker(); 246 247 for (Tree typeDecl : cut.getTypeDecls()) { 248 if (Tree.Kind.CLASS == typeDecl.getKind()) { 250 ClassTree classTree = (ClassTree) typeDecl; 251 ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC)); 252 List <VariableTree> arguments = new ArrayList <VariableTree>(); 253 arguments.add(make.Variable( 254 make.Modifiers(EnumSet.noneOf(Modifier.class)), 255 "prefix", 256 make.PrimitiveType(TypeKind.BOOLEAN), null) 257 ); 258 MethodTree constructor = make.Method( 259 mods, 260 "<init>", 261 null, 262 Collections.<TypeParameterTree> emptyList(), 263 arguments, 264 Collections.<ExpressionTree>emptyList(), 265 make.Block(Collections.<StatementTree>emptyList(), false), 266 null 267 ); 268 ClassTree copy = make.insertClassMember(classTree, 2, constructor); 269 workingCopy.rewrite(classTree, copy); 270 } 271 } 272 } 273 274 public void cancel() { 275 } 276 }; 277 src.runModificationTask(task).commit(); 278 String res = TestUtilities.copyFileToString(testFile); 279 System.err.println(res); 280 assertEquals(golden, res); 281 } 282 283 public void testInsertFieldToIndex0() throws Exception { 284 testFile = new File (getWorkDir(), "Test.java"); 285 TestUtilities.copyStringToFile(testFile, 286 "package hierbas.del.litoral;\n\n" + 287 "public class Test {\n" + 288 " \n" + 289 " int i = 0;\n" + 290 " \n" + 291 " public Test() {\n" + 292 " }\n" + 293 " \n" + 294 "}\n" 295 ); 296 String golden = 297 "package hierbas.del.litoral;\n\n" + 298 "public class Test {\n" + 299 " \n" + 300 " String prefix;\n" + 301 "\n" + 302 " int i = 0;\n" + 303 " \n" + 304 " public Test() {\n" + 305 " }\n" + 306 " \n" + 307 "}\n"; 308 309 JavaSource src = getJavaSource(testFile); 310 311 CancellableTask task = new CancellableTask<WorkingCopy>() { 312 public void run(WorkingCopy workingCopy) throws java.io.IOException { 313 workingCopy.toPhase(Phase.RESOLVED); 314 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 315 TreeMaker make = workingCopy.getTreeMaker(); 316 for (Tree typeDecl : cut.getTypeDecls()) { 317 if (Tree.Kind.CLASS == typeDecl.getKind()) { 318 ClassTree clazz = (ClassTree) typeDecl; 319 VariableTree member = make.Variable( 320 make.Modifiers(Collections.<Modifier>emptySet()), 321 "prefix", 322 make.Identifier("String"), 323 null 324 ); 325 ClassTree modifiedClazz = make.insertClassMember(clazz, 0, member); 326 workingCopy.rewrite(clazz,modifiedClazz); 327 } 328 } 329 } 330 public void cancel() {} 331 }; 332 src.runModificationTask(task).commit(); 333 String res = TestUtilities.copyFileToString(testFile); 334 System.err.println(res); 335 assertEquals(golden, res); 336 } 337 338 public void testModifyFieldName() throws Exception { 339 testFile = new File (getWorkDir(), "Test.java"); 340 TestUtilities.copyStringToFile(testFile, 341 "package hierbas.del.litoral;\n\n" + 342 "public class Test {\n" + 343 " \n" + 344 " int i = 0;\n" + 345 " \n" + 346 " public Test() {\n" + 347 " }\n" + 348 " \n" + 349 "}\n" 350 ); 351 String golden = 352 "package hierbas.del.litoral;\n\n" + 353 "public class Test {\n" + 354 " \n" + 355 " int newFieldName = 0;\n" + 356 " \n" + 357 " public Test() {\n" + 358 " }\n" + 359 " \n" + 360 "}\n"; 361 362 JavaSource src = getJavaSource(testFile); 363 364 CancellableTask task = new CancellableTask<WorkingCopy>() { 365 public void run(WorkingCopy workingCopy) throws java.io.IOException { 366 workingCopy.toPhase(Phase.RESOLVED); 367 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 368 TreeMaker make = workingCopy.getTreeMaker(); 369 for (Tree typeDecl : cut.getTypeDecls()) { 370 if (Tree.Kind.CLASS == typeDecl.getKind()) { 371 VariableTree variable = (VariableTree) ((ClassTree) typeDecl).getMembers().get(0); 372 VariableTree copy = make.setLabel(variable, "newFieldName"); 373 workingCopy.rewrite(variable, copy); 374 } 375 } 376 } 377 public void cancel() {} 378 }; 379 src.runModificationTask(task).commit(); 380 String res = TestUtilities.copyFileToString(testFile); 381 System.err.println(res); 382 assertEquals(golden, res); 383 } 384 385 public void testModifyModifiers() throws Exception { 386 testFile = new File (getWorkDir(), "Test.java"); 387 TestUtilities.copyStringToFile(testFile, 388 "package hierbas.del.litoral;\n\n" + 389 "public class Test {\n" + 390 " \n" + 391 " private int i = 0;\n" + 392 " \n" + 393 " public Test() {\n" + 394 " }\n" + 395 " \n" + 396 "}\n" 397 ); 398 String golden = 399 "package hierbas.del.litoral;\n\n" + 400 "public class Test {\n" + 401 " \n" + 402 " public int i = 0;\n" + 403 " \n" + 404 " public Test() {\n" + 405 " }\n" + 406 " \n" + 407 "}\n"; 408 409 JavaSource src = getJavaSource(testFile); 410 411 CancellableTask task = new CancellableTask<WorkingCopy>() { 412 public void run(WorkingCopy workingCopy) throws java.io.IOException { 413 workingCopy.toPhase(Phase.RESOLVED); 414 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 415 TreeMaker make = workingCopy.getTreeMaker(); 416 for (Tree typeDecl : cut.getTypeDecls()) { 417 if (Tree.Kind.CLASS == typeDecl.getKind()) { 418 VariableTree variable = (VariableTree) ((ClassTree) typeDecl).getMembers().get(0); 419 ModifiersTree mods = variable.getModifiers(); 420 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC))); 421 } 422 } 423 } 424 425 public void cancel() { 426 } 427 }; 428 src.runModificationTask(task).commit(); 429 String res = TestUtilities.copyFileToString(testFile); 430 System.err.println(res); 431 assertEquals(golden, res); 432 } 433 434 public void testAddToEmptyInterface() throws Exception { 435 testFile = new File (getWorkDir(), "Test.java"); 437 TestUtilities.copyStringToFile(testFile, 438 "package hierbas.del.litoral;\n\n" + 439 "public interface Test {\n" + 440 "}\n" 441 ); 442 String golden = 443 "package hierbas.del.litoral;\n\n" + 444 "public interface Test {\n\n" + 445 " public void newlyCreatedMethod(int a, float b) throws java.io.IOException ;\n" + 446 "}\n"; 447 448 JavaSource src = getJavaSource(testFile); 449 CancellableTask task = new CancellableTask<WorkingCopy>() { 450 451 public void run(WorkingCopy workingCopy) throws IOException { 452 workingCopy.toPhase(Phase.RESOLVED); 453 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 454 TreeMaker make = workingCopy.getTreeMaker(); 455 456 for (Tree typeDecl : cut.getTypeDecls()) { 457 if (Tree.Kind.CLASS == typeDecl.getKind()) { 459 ClassTree classTree = (ClassTree) typeDecl; 460 MethodTree method = m(make); 461 MethodTree methodC = make.Method(method.getModifiers(), 462 method.getName(), 463 method.getReturnType(), 464 (List <TypeParameterTree>) method.getTypeParameters(), 465 method.getParameters(), 466 method.getThrows(), 467 (BlockTree) null, 468 null 469 ); 470 ClassTree copy = make.addClassMember(classTree, methodC); 471 workingCopy.rewrite(classTree, copy); 472 } 473 } 474 } 475 476 public void cancel() { 477 } 478 }; 479 src.runModificationTask(task).commit(); 480 String res = TestUtilities.copyFileToString(testFile); 481 System.err.println(res); 482 assertEquals(golden, res); 483 } 484 485 public void testAddNewClassWithNewMembers() throws Exception { 486 testFile = new File (getWorkDir(), "Test.java"); 487 TestUtilities.copyStringToFile(testFile, 488 "package hierbas.del.litoral;\n\n" + 489 "public class Test {\n" + 490 "}\n" 491 ); 492 String golden = 493 "package hierbas.del.litoral;\n\n" + 494 "public class Test {\n" + 495 " public class X {\n" + 496 " private int i;\n\n" + 497 " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" + 498 " }\n" + 499 " }\n" + 500 "}\n"; 501 502 JavaSource src = getJavaSource(testFile); 503 CancellableTask task = new CancellableTask<WorkingCopy>() { 504 505 public void run(WorkingCopy workingCopy) throws IOException { 506 workingCopy.toPhase(Phase.RESOLVED); 507 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 508 TreeMaker make = workingCopy.getTreeMaker(); 509 510 ClassTree ct = (ClassTree) cut.getTypeDecls().get(0); 511 MethodTree method = m(make); 512 MethodTree methodC = make.Method(method.getModifiers(), 513 method.getName(), 514 method.getReturnType(), 515 (List <TypeParameterTree>) method.getTypeParameters(), 516 method.getParameters(), 517 method.getThrows(), 518 "{}", 519 null 520 ); 521 VariableTree var = make.Variable(make.Modifiers(EnumSet.of(Modifier.PRIVATE)), "i", make.Type(workingCopy.getTypes().getPrimitiveType(TypeKind.INT)), null); 522 ClassTree nueClass = make.Class(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), "X", Collections.<TypeParameterTree>emptyList(), null, Collections.<ExpressionTree>emptyList(), Arrays.asList(var, methodC)); 523 ClassTree copy = make.addClassMember(ct, nueClass); 524 workingCopy.rewrite(ct, copy); 525 } 526 527 public void cancel() { 528 } 529 }; 530 src.runModificationTask(task).commit(); 531 String res = TestUtilities.copyFileToString(testFile); 532 System.err.println(res); 533 assertEquals(golden, res); 534 } 535 536 private MethodTree m(TreeMaker make) { 537 ModifiersTree parMods = make.Modifiers(Collections.<Modifier>emptySet(), Collections.<AnnotationTree>emptyList()); 539 VariableTree par1 = make.Variable(parMods, "a", make.PrimitiveType(TypeKind.INT), null); 541 VariableTree par2 = make.Variable(parMods, "b", make.PrimitiveType(TypeKind.FLOAT), null); 542 List <VariableTree> parList = new ArrayList <VariableTree>(2); 543 parList.add(par1); 544 parList.add(par2); 545 MethodTree newMethod = make.Method( 547 make.Modifiers( 548 Collections.singleton(Modifier.PUBLIC), Collections.EMPTY_LIST ), "newlyCreatedMethod", make.PrimitiveType(TypeKind.VOID), Collections.EMPTY_LIST, parList, Collections.singletonList(make.Identifier("java.io.IOException")), make.Block(Collections.EMPTY_LIST, false), null ); 559 return newMethod; 560 } 561 562 567 public void testAddAfterEmptyInit1() throws Exception { 568 testFile = new File (getWorkDir(), "Test.java"); 569 TestUtilities.copyStringToFile(testFile, 570 "package hierbas.del.litoral;\n\n" + 571 "public class Test {\n" + 572 " static enum Enumerace {\n" + 573 " A, B\n" + 574 " };\n" + 575 "}\n" 576 ); 577 String golden = 578 "package hierbas.del.litoral;\n\n" + 579 "public class Test {\n" + 580 " static enum Enumerace {\n" + 581 " A, B\n" + 582 " };\n\n" + 583 " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" + 584 " }\n" + 585 "}\n"; 586 587 JavaSource src = getJavaSource(testFile); 588 CancellableTask task = new CancellableTask<WorkingCopy>() { 589 590 public void run(WorkingCopy workingCopy) throws IOException { 591 workingCopy.toPhase(Phase.RESOLVED); 592 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 593 TreeMaker make = workingCopy.getTreeMaker(); 594 595 for (Tree typeDecl : cut.getTypeDecls()) { 596 if (Tree.Kind.CLASS == typeDecl.getKind()) { 598 ClassTree classTree = (ClassTree) typeDecl; 599 ClassTree copy = make.addClassMember(classTree, m(make)); 600 workingCopy.rewrite(classTree, copy); 601 } 602 } 603 } 604 605 public void cancel() { 606 } 607 }; 608 src.runModificationTask(task).commit(); 609 String res = TestUtilities.copyFileToString(testFile); 610 System.err.println(res); 611 assertEquals(golden, res); 612 } 613 614 619 public void testAddAfterEmptyInit2() throws Exception { 620 testFile = new File (getWorkDir(), "Test.java"); 621 TestUtilities.copyStringToFile(testFile, 622 "package hierbas.del.litoral;\n\n" + 623 "public class Test {\n" + 624 " ;\n" + 625 "}\n" 626 ); 627 String golden = 628 "package hierbas.del.litoral;\n\n" + 629 "public class Test {\n\n" + 630 " ;\n\n" + 631 " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" + 632 " }\n" + 633 "}\n"; 634 635 JavaSource src = getJavaSource(testFile); 636 CancellableTask task = new CancellableTask<WorkingCopy>() { 637 638 public void run(WorkingCopy workingCopy) throws IOException { 639 workingCopy.toPhase(Phase.RESOLVED); 640 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 641 TreeMaker make = workingCopy.getTreeMaker(); 642 643 for (Tree typeDecl : cut.getTypeDecls()) { 644 if (Tree.Kind.CLASS == typeDecl.getKind()) { 646 ClassTree classTree = (ClassTree) typeDecl; 647 ClassTree copy = make.addClassMember(classTree, m(make)); 648 workingCopy.rewrite(classTree, copy); 649 } 650 } 651 } 652 653 public void cancel() { 654 } 655 }; 656 src.runModificationTask(task).commit(); 657 String res = TestUtilities.copyFileToString(testFile); 658 System.err.println(res); 659 assertEquals(golden, res); 660 } 661 662 665 public void testAddInnerInterface() throws Exception { 666 testFile = new File (getWorkDir(), "Test.java"); 667 TestUtilities.copyStringToFile(testFile, 668 "package hierbas.del.litoral;\n\n" + 669 "public class Test {\n" + 670 "}\n" 671 ); 672 String golden = 673 "package hierbas.del.litoral;\n\n" + 674 "public class Test {\n" + 675 " interface Honza {\n" + 676 " }\n" + 677 "}\n"; 678 679 JavaSource src = getJavaSource(testFile); 680 CancellableTask task = new CancellableTask<WorkingCopy>() { 681 682 public void run(WorkingCopy workingCopy) throws IOException { 683 workingCopy.toPhase(Phase.RESOLVED); 684 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 685 TreeMaker make = workingCopy.getTreeMaker(); 686 ClassTree topLevel = (ClassTree) cut.getTypeDecls().get(0); 687 ClassTree innerIntfc = make.Interface(make.Modifiers( 688 Collections.<Modifier>emptySet()), 689 "Honza", 690 Collections.<TypeParameterTree>emptyList(), 691 Collections.<ExpressionTree>emptyList(), 692 Collections.<Tree>emptyList() 693 ); 694 workingCopy.rewrite(topLevel, make.addClassMember(topLevel, innerIntfc)); 695 } 696 697 public void cancel() { 698 } 699 }; 700 src.runModificationTask(task).commit(); 701 String res = TestUtilities.copyFileToString(testFile); 702 System.err.println(res); 703 assertEquals(golden, res); 704 } 705 706 709 public void testAddInnerAnnotationType() throws Exception { 710 testFile = new File (getWorkDir(), "Test.java"); 711 TestUtilities.copyStringToFile(testFile, 712 "package hierbas.del.litoral;\n\n" + 713 "public class Test {\n" + 714 "}\n" 715 ); 716 String golden = 717 "package hierbas.del.litoral;\n\n" + 718 "public class Test {\n" + 719 " public @interface Honza {\n" + 720 " }\n" + 721 "}\n"; 722 723 JavaSource src = getJavaSource(testFile); 724 CancellableTask task = new CancellableTask<WorkingCopy>() { 725 726 public void run(WorkingCopy workingCopy) throws IOException { 727 workingCopy.toPhase(Phase.RESOLVED); 728 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 729 TreeMaker make = workingCopy.getTreeMaker(); 730 ClassTree topLevel = (ClassTree) cut.getTypeDecls().get(0); 731 ClassTree innerIntfc = make.AnnotationType(make.Modifiers( 732 Collections.<Modifier>singleton(Modifier.PUBLIC)), 733 "Honza", 734 Collections.<Tree>emptyList() 735 ); 736 workingCopy.rewrite(topLevel, make.addClassMember(topLevel, innerIntfc)); 737 } 738 739 public void cancel() { 740 } 741 }; 742 src.runModificationTask(task).commit(); 743 String res = TestUtilities.copyFileToString(testFile); 744 System.err.println(res); 745 assertEquals(golden, res); 746 } 747 748 751 public void testAddInnerEnum() throws Exception { 752 testFile = new File (getWorkDir(), "Test.java"); 753 TestUtilities.copyStringToFile(testFile, 754 "package hierbas.del.litoral;\n\n" + 755 "public class Test {\n" + 756 "}\n" 757 ); 758 String golden = 759 "package hierbas.del.litoral;\n\n" + 760 "public class Test {\n" + 761 " protected enum Honza {\n" + 762 " }\n" + 763 "}\n"; 764 765 JavaSource src = getJavaSource(testFile); 766 CancellableTask task = new CancellableTask<WorkingCopy>() { 767 768 public void run(WorkingCopy workingCopy) throws IOException { 769 workingCopy.toPhase(Phase.RESOLVED); 770 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 771 TreeMaker make = workingCopy.getTreeMaker(); 772 ClassTree topLevel = (ClassTree) cut.getTypeDecls().get(0); 773 ClassTree innerIntfc = make.Enum(make.Modifiers( 774 Collections.<Modifier>singleton(Modifier.PROTECTED)), 775 "Honza", 776 Collections.<ExpressionTree>emptyList(), 777 Collections.<Tree>emptyList() 778 ); 779 workingCopy.rewrite(topLevel, make.addClassMember(topLevel, innerIntfc)); 780 } 781 782 public void cancel() { 783 } 784 }; 785 src.runModificationTask(task).commit(); 786 String res = TestUtilities.copyFileToString(testFile); 787 System.err.println(res); 788 assertEquals(golden, res); 789 } 790 791 String getGoldenPckg() { 792 return ""; 793 } 794 795 String getSourcePckg() { 796 return ""; 797 } 798 799 } 800 | Popular Tags |