1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import com.sun.source.util.SourcePositions; 23 import java.io.BufferedReader ; 24 import java.io.File ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.io.PrintStream ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.List ; 31 import java.util.List ; 32 import javax.lang.model.element.Modifier; 33 import javax.lang.model.element.TypeElement; 34 import javax.lang.model.type.TypeKind; 35 import org.netbeans.api.java.source.CancellableTask; 36 import org.netbeans.api.java.source.JavaSource; 37 import org.netbeans.api.java.source.TestUtilities; 38 import org.netbeans.api.java.source.TreeMaker; 39 import org.netbeans.api.java.source.TreeUtilities; 40 import static org.netbeans.api.java.source.JavaSource.*; 41 import org.netbeans.api.java.source.WorkingCopy; 42 import org.netbeans.junit.NbTestSuite; 43 import org.openide.filesystems.FileStateInvalidException; 44 import org.openide.filesystems.FileUtil; 45 46 54 55 public class TutorialTest extends GeneratorTest { 56 57 58 public TutorialTest(String name) { 59 super(name); 60 } 61 62 public static NbTestSuite suite() { 63 NbTestSuite suite = new NbTestSuite(); 64 suite.addTestSuite(TutorialTest.class); 65 return suite; 72 } 73 74 public void testFirstModification() throws FileStateInvalidException,IOException { 77 File tutorialFile = getFile(getSourceDir(), "/org/netbeans/test/codegen/Tutorial1.java"); 83 JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(tutorialFile)); 84 85 CancellableTask task = new CancellableTask<WorkingCopy>() { 86 87 public void run(WorkingCopy workingCopy) throws java.io.IOException { 88 workingCopy.toPhase(Phase.RESOLVED); 92 93 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 96 97 TreeMaker make = workingCopy.getTreeMaker(); 102 for (Tree typeDecl : cut.getTypeDecls()) { 105 if (Tree.Kind.CLASS == typeDecl.getKind()) { 107 ClassTree clazz = (ClassTree) typeDecl; 108 111 ExpressionTree implementsClause = make.Identifier("Externalizable"); 119 120 121 implementsClause = make.Identifier("java.io.Externalizable"); 132 133 TypeElement element = workingCopy.getElements().getTypeElement("java.io.Externalizable"); 146 implementsClause = make.QualIdent(element); 147 148 ClassTree modifiedClazz = make.addClassImplementsClause(clazz, implementsClause); 158 164 168 workingCopy.rewrite(clazz, modifiedClazz); 170 } 171 } 172 } 173 174 public void cancel() { 175 } 176 }; 177 178 tutorialSource.runModificationTask(task).commit(); 184 185 BufferedReader in = new BufferedReader (new FileReader (tutorialFile)); 187 PrintStream out = System.out; 188 String str; 189 while ((str = in.readLine()) != null) { 190 out.println(str); 191 } 192 in.close(); 193 } 194 195 public void testAddMethod() throws FileStateInvalidException, IOException { 197 File tutorialFile = getFile(getSourceDir(), "/org/netbeans/test/codegen/Tutorial1.java"); 198 JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(tutorialFile)); 199 200 CancellableTask task = new CancellableTask<WorkingCopy>() { 201 202 public void run(WorkingCopy workingCopy) throws java.io.IOException { 203 workingCopy.toPhase(Phase.RESOLVED); 204 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 205 206 TreeMaker make = workingCopy.getTreeMaker(); 207 for (Tree typeDecl : cut.getTypeDecls()) { 208 if (Tree.Kind.CLASS == typeDecl.getKind()) { 210 ClassTree clazz = (ClassTree) typeDecl; 211 212 220 ModifiersTree methodModifiers = make.Modifiers( 222 Collections.<Modifier>singleton(Modifier.PUBLIC), 223 Collections.<AnnotationTree>emptyList() 224 ); 225 226 VariableTree parameter = make.Variable( 229 make.Modifiers( 230 Collections.<Modifier>singleton(Modifier.FINAL), 231 Collections.<AnnotationTree>emptyList() 232 ), 233 "arg0", make.Identifier("Object"), null ); 237 238 TypeElement element = workingCopy.getElements().getTypeElement("java.io.IOException"); 241 ExpressionTree throwsClause = make.QualIdent(element); 242 243 MethodTree newMethod = make.Method( 252 methodModifiers, "writeExternal", make.PrimitiveType(TypeKind.VOID), Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>singletonList(parameter), Collections.<ExpressionTree>singletonList(throwsClause), "{ throw new UnsupportedOperationException(\"Not supported yet.\") }", null ); 262 263 ClassTree modifiedClazz = make.addClassMember(clazz, newMethod); 266 workingCopy.rewrite(clazz, modifiedClazz); 267 } 268 } 269 } 270 271 public void cancel() { 272 } 273 }; 274 275 tutorialSource.runModificationTask(task).commit(); 276 277 BufferedReader in = new BufferedReader (new FileReader (tutorialFile)); 279 PrintStream out = System.out; 280 String str; 281 while ((str = in.readLine()) != null) { 282 out.println(str); 283 } 284 in.close(); 285 } 286 287 public void testAddAnnotation() throws FileStateInvalidException, IOException { 289 File tutorialFile = getFile(getSourceDir(), "/org/netbeans/test/codegen/Tutorial1.java"); 290 JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(tutorialFile)); 291 292 CancellableTask task = new CancellableTask<WorkingCopy>() { 293 294 public void run(WorkingCopy workingCopy) throws java.io.IOException { 295 workingCopy.toPhase(Phase.RESOLVED); 296 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 297 298 TreeMaker make = workingCopy.getTreeMaker(); 299 for (Tree typeDecl : cut.getTypeDecls()) { 300 if (Tree.Kind.CLASS == typeDecl.getKind()) { 302 ClassTree clazz = (ClassTree) typeDecl; 303 304 List <? extends AnnotationTree> oldAnnList = clazz.getModifiers().getAnnotations(); 306 List <AnnotationTree> modifiedAnnList = new ArrayList <AnnotationTree>(oldAnnList); 307 308 AnnotationTree newAnnotation = make.Annotation( 313 make.Identifier("Override"), 314 Collections.<ExpressionTree>emptyList() 315 ); 316 modifiedAnnList.add(newAnnotation); 317 318 ModifiersTree classModifiers = make.Modifiers( 321 clazz.getModifiers().getFlags(), 322 modifiedAnnList 323 ); 324 workingCopy.rewrite(clazz.getModifiers(), classModifiers); 326 } 327 } 328 } 329 330 public void cancel() { 331 } 332 }; 333 334 tutorialSource.runModificationTask(task).commit(); 335 336 BufferedReader in = new BufferedReader (new FileReader (tutorialFile)); 338 PrintStream out = System.out; 339 String str; 340 while ((str = in.readLine()) != null) { 341 out.println(str); 342 } 343 in.close(); 344 } 345 346 public void testForJean() throws FileStateInvalidException, IOException { 348 File tutorialFile = getFile(getSourceDir(), "/org/netbeans/test/codegen/Tutorial2.java"); 349 JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(tutorialFile)); 350 351 CancellableTask task = new CancellableTask<WorkingCopy>() { 352 353 public void run(WorkingCopy workingCopy) throws java.io.IOException { 354 workingCopy.toPhase(Phase.RESOLVED); 355 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 356 357 TreeMaker make = workingCopy.getTreeMaker(); 358 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 361 MethodTree method = (MethodTree) clazz.getMembers().get(1); 364 BlockTree body = method.getBody(); 365 366 SourcePositions sp = workingCopy.getTrees().getSourcePositions(); 369 int start = (int) sp.getStartPosition(cut, body); 370 int end = (int) sp.getEndPosition(cut, body); 371 String bodyText = workingCopy.getText().substring(start, end); 373 MethodTree modified = make.Method( 374 method.getModifiers(), method.getName(), 376 method.getReturnType(), 377 method.getTypeParameters(), 378 method.getParameters(), 379 method.getThrows(), 380 bodyText.replace("{0}", "-tag-replace-"), null ); 383 workingCopy.rewrite(method, modified); 385 } 386 387 public void cancel() { 388 } 389 }; 390 391 tutorialSource.runModificationTask(task).commit(); 392 393 BufferedReader in = new BufferedReader (new FileReader (tutorialFile)); 395 PrintStream out = System.out; 396 String str; 397 while ((str = in.readLine()) != null) { 398 out.println(str); 399 } 400 in.close(); 401 } 402 403 public void testForErno() throws FileStateInvalidException, IOException { 405 File tutorialFile = getFile(getSourceDir(), "/org/netbeans/test/codegen/Tutorial2.java"); 406 JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(tutorialFile)); 407 408 CancellableTask task = new CancellableTask<WorkingCopy>() { 409 410 public void run(WorkingCopy workingCopy) throws java.io.IOException { 411 workingCopy.toPhase(Phase.RESOLVED); 412 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 413 414 TreeMaker make = workingCopy.getTreeMaker(); 415 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 418 VariableTree var = make.Variable(make.Modifiers( 421 Collections.<Modifier>emptySet(), Collections.<AnnotationTree>emptyList()), 422 "myField", 423 make.Identifier("MyClass"), 424 make.MethodInvocation( 425 Collections.<ExpressionTree>emptyList(), 426 make.MemberSelect(make.Identifier("Something"), "getMyClass"), 427 Collections.<ExpressionTree>emptyList() 428 ) 429 ); 430 ClassTree copy = make.addClassMember(clazz, var); 432 workingCopy.rewrite(clazz, copy); 433 } 434 435 public void cancel() { 436 } 437 }; 438 439 tutorialSource.runModificationTask(task).commit(); 440 441 BufferedReader in = new BufferedReader (new FileReader (tutorialFile)); 443 PrintStream out = System.out; 444 String str; 445 while ((str = in.readLine()) != null) { 446 out.println(str); 447 } 448 in.close(); 449 } 450 451 public void testMethodInvocation() throws Exception { 453 testFile = new File (getWorkDir(), "Test.java"); 454 TestUtilities.copyStringToFile(testFile, 455 "package hierbas.del.litoral;\n\n" + 456 "import java.io.*;\n\n" + 457 "public class Test {\n" + 458 " public void taragui() {\n" + 459 " }\n" + 460 "}\n" 461 ); 462 JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 463 464 CancellableTask task = new CancellableTask<WorkingCopy>() { 465 466 public void run(WorkingCopy workingCopy) throws java.io.IOException { 467 workingCopy.toPhase(Phase.RESOLVED); 468 TreeMaker make = workingCopy.getTreeMaker(); 469 470 MethodInvocationTree cloneInvocation = make.MethodInvocation( 472 Collections.<ExpressionTree>emptyList(), 473 make.Identifier("clone"), 474 Collections.<ExpressionTree>emptyList() 475 ); 476 477 MemberSelectTree toStringSelIdent = make.MemberSelect(cloneInvocation, "toString"); 479 MethodInvocationTree toStringInvocation = make.MethodInvocation( 481 Collections.<ExpressionTree>emptyList(), 482 toStringSelIdent, 483 Collections.<ExpressionTree>emptyList() 484 ); 485 ExpressionStatementTree statement = make.ExpressionStatement(toStringInvocation); 487 488 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 490 MethodTree method = (MethodTree) clazz.getMembers().get(1); 491 BlockTree copy = make.addBlockStatement(method.getBody(), statement); 492 workingCopy.rewrite(method.getBody(), copy); 493 } 494 495 public void cancel() { 496 } 497 }; 498 499 tutorialSource.runModificationTask(task).commit(); 500 501 BufferedReader in = new BufferedReader (new FileReader (testFile)); 503 PrintStream out = System.out; 504 String str; 505 while ((str = in.readLine()) != null) { 506 out.println(str); 507 } 508 in.close(); 509 } 510 511 public void testNullLiteral() throws Exception { 513 testFile = new File (getWorkDir(), "Test.java"); 514 TestUtilities.copyStringToFile(testFile, 515 "package hierbas.del.litoral;\n\n" + 516 "import java.io.*;\n\n" + 517 "public class Test {\n" + 518 " public void taragui() {\n" + 519 " }\n" + 520 "}\n" 521 ); 522 JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 523 final String statementText = "System.err.println(null);"; 526 527 CancellableTask task = new CancellableTask<WorkingCopy>() { 528 529 public void run(WorkingCopy workingCopy) throws java.io.IOException { 530 workingCopy.toPhase(Phase.RESOLVED); 531 TreeMaker make = workingCopy.getTreeMaker(); 532 533 SourcePositions[] positions = new SourcePositions[1]; 534 final TreeUtilities treeUtils = workingCopy.getTreeUtilities(); 535 StatementTree body = treeUtils.parseStatement(statementText, positions); 536 System.err.println(TreeMakerDemo.reverse(body)); 537 } 538 539 public void cancel() { 540 } 541 }; 542 543 tutorialSource.runModificationTask(task).commit(); 544 545 BufferedReader in = new BufferedReader (new FileReader (testFile)); 547 PrintStream out = System.out; 548 String str; 549 while ((str = in.readLine()) != null) { 550 out.println(str); 551 } 552 in.close(); 553 } 554 555 String getSourcePckg() { 558 return "org/netbeans/test/codegen/"; 559 } 560 561 String getGoldenPckg() { 562 return "org/netbeans/jmi/javamodel/codegen/ConstructorTest/ConstructorTest/"; 563 } 564 565 } 566 | Popular Tags |