1 19 20 25 26 package org.netbeans.test.java; 27 28 import com.sun.source.tree.AnnotationTree; 29 import com.sun.source.tree.BlockTree; 30 import com.sun.source.tree.ClassTree; 31 import com.sun.source.tree.CompilationUnitTree; 32 import com.sun.source.tree.ExpressionTree; 33 import com.sun.source.tree.ImportTree; 34 import com.sun.source.tree.MethodTree; 35 import com.sun.source.tree.ModifiersTree; 36 import com.sun.source.tree.Tree; 37 import com.sun.source.tree.Tree.Kind; 38 import com.sun.source.tree.TypeParameterTree; 39 import com.sun.source.tree.VariableTree; 40 import java.io.IOException ; 41 import java.util.ArrayList ; 42 import java.util.Collections ; 43 import java.util.EnumSet ; 44 import java.util.HashMap ; 45 import java.util.LinkedList ; 46 import java.util.List ; 47 import java.util.Map ; 48 import java.util.Set ; 49 import javax.lang.model.element.Modifier; 50 import javax.lang.model.element.TypeElement; 51 import javax.lang.model.type.TypeKind; 52 import org.netbeans.api.java.classpath.ClassPath; 53 import org.netbeans.api.java.source.CancellableTask; 54 import org.netbeans.api.java.source.Comment; 55 import org.netbeans.api.java.source.JavaSource; 56 import org.netbeans.api.java.source.JavaSource.Phase; 57 import org.netbeans.api.java.source.TreeMaker; 58 import org.netbeans.api.java.source.WorkingCopy; 59 import org.openide.filesystems.*; 60 import org.openide.loaders.DataObject; 61 import org.openide.filesystems.Repository; 62 65 66 70 71 public class Common extends Object { 72 73 private static final String cr=System.getProperty("line.separator"); 74 private static final String METHODS = "newMethod"; 75 private static final String FIELDS = "newField"; 76 private static final String DEFAULTBODY = "for (int i=0;i<100;i++){\n\tSystem.out.println(new Integer(i).toString());\n}\nreturn 0;\n"; 78 public static final String DEFAULTINITIALIZERBODY = "\n/*somebody*/\n"; 79 80 82 public static Map <String ,String > PARS1; 83 85 public static Map <String ,String > PARS2; 86 88 public static Map <String ,String > PARS3; 89 90 91 93 96 99 101 static { 102 PARS1 = new HashMap <String , String >(); 103 PARS1.put("param1","int"); 104 PARS2 = new HashMap <String , String >(); 105 PARS2.put("param1","int"); 106 PARS2.put("param2","int"); 107 PARS3 = new HashMap <String , String >(); 108 PARS3.put("param1","float"); 109 PARS3.put("param2","int"); 110 PARS3.put("param3","String"); 111 112 } 113 114 private static java.io.PrintWriter pw = null; 115 116 public static void setPrintWriter(java.io.PrintWriter pr){ 117 pw=pr; 118 } 119 120 135 136 public Common() { 137 } 138 139 144 public static String concat(String s, int i){ 145 return s+new Integer (i).toString(); 146 } 147 148 152 static public String getMethodName(int i){ 153 return concat(METHODS,i); 154 } 155 156 161 public static String getFieldName(String name,int i){ 162 return concat(name,i); 163 } 164 165 169 public static String getFieldName(int i){ 170 return concat(FIELDS,i); 171 } 172 173 180 191 196 202 207 211 218 222 229 233 234 265 private static Tree getTreeForType(String paramType, TreeMaker make) { 266 Tree param = null; 267 try { 268 param = make.PrimitiveType(TypeKind.valueOf(paramType.toUpperCase())); 269 } catch (IllegalArgumentException iae) { 270 param = make.Identifier(paramType); 271 } 272 return param; 273 } 274 275 public static MethodTree createMethod(TreeMaker make,String name, Map <String ,String > params) { 276 ModifiersTree methodModifiers = make.Modifiers( 277 Collections.<Modifier>singleton(Modifier.PUBLIC), 278 Collections.<AnnotationTree>emptyList() 279 ); 280 List <VariableTree> paramList = new LinkedList <VariableTree>(); 281 for(String paramName: params.keySet()) { 282 Tree paramType = getTreeForType(params.get(paramName), make); 283 VariableTree parameter = make.Variable( 284 make.Modifiers( 285 Collections.<Modifier>emptySet(), 286 Collections.<AnnotationTree>emptyList() 287 ), 288 paramName, paramType, null ); 292 paramList.add(parameter); 293 } 294 MethodTree newMethod = make.Method( 295 methodModifiers, name, make.PrimitiveType(TypeKind.VOID), Collections.<TypeParameterTree>emptyList(), paramList, Collections.<ExpressionTree>emptyList(), "{ throw new UnsupportedOperationException(\"Not supported yet.\") }", null ); 304 return newMethod; 305 } 306 307 public static void addMethod(JavaSource js, 308 final String name, 309 final Map <String ,String > params, 310 final String returnType, 311 final Set <Modifier> modifiers) throws IOException { 312 CancellableTask task = new CancellableTask<WorkingCopy>() { 313 public void cancel() { 314 throw new UnsupportedOperationException ("Not supported yet."); 315 } 316 317 public void run(WorkingCopy workingCopy) throws Exception { 318 workingCopy.toPhase(Phase.RESOLVED); 319 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 320 TreeMaker make = workingCopy.getTreeMaker(); 321 ClassTree clazz = null; 322 for (Tree typeDecl : cut.getTypeDecls()) { 323 if (Tree.Kind.CLASS == typeDecl.getKind()) { 324 clazz = (ClassTree) typeDecl; 325 } 326 } MethodTree newMethod = createMethod(make, name, params); 328 newMethod = make.Method(make.Modifiers(modifiers), 329 newMethod.getName(), 330 getTreeForType(returnType, make), 331 newMethod.getTypeParameters(), 332 newMethod.getParameters(), 333 newMethod.getThrows(), 334 newMethod.getBody(), 335 (ExpressionTree)newMethod.getDefaultValue()); 336 ClassTree modifiedClazz = make.addClassMember(clazz, newMethod); 337 workingCopy.rewrite(clazz, modifiedClazz); 338 } 339 }; 340 js.runModificationTask(task).commit(); 341 } 342 343 public static void addConstructor(JavaSource js, final Map <String ,String > params) throws IOException { 344 CancellableTask task = new CancellableTask<WorkingCopy>() { 345 public void cancel() { 346 throw new UnsupportedOperationException ("Not supported yet."); 347 } 348 349 public void run(WorkingCopy workingCopy) throws Exception { 350 workingCopy.toPhase(Phase.RESOLVED); 351 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 352 TreeMaker make = workingCopy.getTreeMaker(); 353 ClassTree clazz = null; 354 for (Tree typeDecl : cut.getTypeDecls()) { 355 if (Tree.Kind.CLASS == typeDecl.getKind()) { 356 clazz = (ClassTree) typeDecl; 357 } 358 } MethodTree newMethod = createMethod(make, "<init>", params); 360 ClassTree modifiedClazz = make.addClassMember(clazz, newMethod); 361 workingCopy.rewrite(clazz, modifiedClazz); 362 } 363 }; 364 js.runModificationTask(task).commit(); 365 } 366 367 public static void removeConstructors(JavaSource js) throws IOException { 368 CancellableTask task = new CancellableTask<WorkingCopy>() { 369 public void cancel() { 370 throw new UnsupportedOperationException ("Not supported yet."); 371 } 372 373 ClassTree orig; 374 375 public void run(WorkingCopy workingCopy) throws Exception { 376 workingCopy.toPhase(Phase.RESOLVED); 377 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 378 TreeMaker make = workingCopy.getTreeMaker(); 379 ClassTree clazz = null; 380 for (Tree typeDecl : cut.getTypeDecls()) { 381 if (Tree.Kind.CLASS == typeDecl.getKind()) { 382 clazz = (ClassTree) typeDecl; 383 } 384 } orig = clazz; 386 for(Tree el : clazz.getMembers()) { 387 if(el.getKind().equals(Kind.METHOD)) { 388 MethodTree method = (MethodTree) el; 389 if(method.getName().toString().equals("<init>")) { 390 clazz = make.removeClassMember(clazz, method); 391 } 392 393 } 394 } 395 workingCopy.rewrite(orig, clazz); 396 } 397 }; 398 js.runModificationTask(task).commit(); 399 } 400 401 public static void addInitializer(JavaSource js, final boolean isStatic) throws IOException { 402 CancellableTask task = new CancellableTask<WorkingCopy>() { 403 public void cancel() { 404 throw new UnsupportedOperationException ("Not supported yet."); 405 } 406 407 public void run(WorkingCopy workingCopy) throws Exception { 408 workingCopy.toPhase(Phase.RESOLVED); 409 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 410 TreeMaker make = workingCopy.getTreeMaker(); 411 ClassTree clazz = null; 412 for (Tree typeDecl : cut.getTypeDecls()) { 413 if (Tree.Kind.CLASS == typeDecl.getKind()) { 414 clazz = (ClassTree) typeDecl; 415 } 416 } 418 BlockTree bt = make.Block(Collections.EMPTY_LIST, isStatic); 419 ClassTree modifiedClazz = make.addClassMember(clazz, bt); 420 workingCopy.rewrite(clazz,modifiedClazz); 421 } 422 }; 423 js.runModificationTask(task).commit(); 424 } 425 426 427 public static void addExtendImplementClause(JavaSource js,final String superClass,final List <String > ifaces) throws IOException { 428 CancellableTask task = new CancellableTask<WorkingCopy>() { 429 public void cancel() { 430 throw new UnsupportedOperationException ("Not supported yet."); 431 } 432 433 public void run(WorkingCopy workingCopy) throws Exception { 434 workingCopy.toPhase(Phase.RESOLVED); 435 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 436 TreeMaker make = workingCopy.getTreeMaker(); 437 ClassTree clazz = null; 438 for (Tree typeDecl : cut.getTypeDecls()) { 439 if (Tree.Kind.CLASS == typeDecl.getKind()) { 440 clazz = (ClassTree) typeDecl; 441 } 442 } ClassTree origClazz = clazz; 444 for(String iface :ifaces) { 445 TypeElement element = workingCopy.getElements().getTypeElement(iface); 446 ExpressionTree implementsClause = implementsClause = make.QualIdent(element); 447 clazz = make.addClassImplementsClause(clazz, implementsClause); 448 } 449 Tree extendsTree = make.QualIdent(workingCopy.getElements().getTypeElement(superClass)); 450 clazz = make.Class(clazz.getModifiers(), 451 clazz.getSimpleName(), 452 clazz.getTypeParameters(), 453 extendsTree, 454 (List <ExpressionTree>) clazz.getImplementsClause(), 455 clazz.getMembers()); 456 workingCopy.rewrite(origClazz,clazz); 457 } 458 }; 459 js.runModificationTask(task).commit(); 460 461 } 462 463 469 470 471 public static void addImport(JavaSource js,final String importText,final boolean isStatic) throws IOException { 472 CancellableTask task = new CancellableTask<WorkingCopy>() { 473 public void cancel() { 474 throw new UnsupportedOperationException ("Not supported yet."); 475 } 476 477 public void run(WorkingCopy workingCopy) throws Exception { 478 workingCopy.toPhase(Phase.RESOLVED); 479 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 480 TreeMaker make = workingCopy.getTreeMaker(); 481 CompilationUnitTree copy = make.addCompUnitImport(cut,make.Import(make.Identifier(importText), isStatic)); 482 workingCopy.rewrite(cut, copy); 483 } 484 }; 485 js.runModificationTask(task).commit(); 486 } 487 488 489 public static void setPackage(JavaSource js, final String pack) throws IOException { 490 CancellableTask task = new CancellableTask<WorkingCopy>() { 491 public void cancel() { 492 throw new UnsupportedOperationException ("Not supported yet."); 493 } 494 495 public void run(WorkingCopy workingCopy) throws Exception { 496 workingCopy.toPhase(Phase.RESOLVED); 497 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 498 TreeMaker make = workingCopy.getTreeMaker(); 499 CompilationUnitTree copy = make.CompilationUnit(make.Identifier(pack),cut.getImports(), cut.getTypeDecls(), cut.getSourceFile()); 500 workingCopy.rewrite(cut, copy); 501 } 502 }; 503 js.runModificationTask(task).commit(); 504 } 505 506 public static FileObject createClass(FileObject target, String packageName, String className) throws Exception { 507 DataObject dob = getSystemDO("Templates/Classes", "Class", "java"); 508 DataObject mdob = dob.createFromTemplate(org.openide.loaders.DataFolder.findFolder(getFO(target,packageName,null,null)), className); 509 return mdob.getPrimaryFile(); 510 } 511 512 518 525 526 527 528 public static FileObject getFO(FileObject file, String pkg, String name, String ext) throws Exception { 529 ClassPath cp=ClassPath.getClassPath(file, ClassPath.SOURCE); 530 String nam=(pkg != null && pkg.length() > 0)?pkg:""; 531 nam+=(name != null && name.length() > 0)?".":""; 532 nam=nam.replace('.', '/'); 533 nam+=(name != null && name.length() > 0)?name:""; 534 nam+=(ext != null && ext.length() > 0)?"."+ext:""; 535 FileObject[] root=cp.getRoots(); 537 FileObject ret=cp.findResource(nam); 538 return ret; 539 } 540 541 542 public static DataObject getSystemDO(String pkg, String name, String ext) throws Exception { 543 return DataObject.find(Repository.getDefault().getDefaultFileSystem().findResource(pkg+"/"+name+"."+ext)); 544 } 545 546 550 592 597 public static boolean arrayEquals(Object [] l,Object [] r) { 598 if (l.length!=r.length) return false; 599 for (int i=0; i < l.length; i++){ 600 if (!l[i].equals(r[i])) return false; 601 } 602 return true; 603 } 604 605 609 public static String unify(String result) { 610 int left=result.indexOf("* Created on"); 611 int right=result.indexOf('\n',left); 612 if (left > -1) 613 result=result.substring(0,left+"* Created on".length())+result.substring(right); 614 615 left=result.indexOf("@author"); 616 right=result.indexOf('\n',left); 617 if (left > -1) 618 result=result.substring(0,left+"@author".length())+result.substring(right); 619 return result; 620 } 621 622 626 public static String firstCharToUpper(String str) { 627 String first, rest; 628 629 if( str==null || str.equals("") ) 630 return str; 631 632 first = str.substring(0, 1).toUpperCase(); 633 rest = str.substring( 1 ); 634 return first+rest; 635 } 636 637 641 public static String firstCharToLower(String str) { 642 String first, rest; 643 644 if( str==null || str.equals("") ) 645 return( str ); 646 647 first = str.substring(0, 1).toLowerCase(); 648 rest = str.substring( 1 ); 649 650 return first+rest; 651 } 652 653 657 720 public static VariableTree createField(TreeMaker make,String name,Set <Modifier> modifiers, String type) { 721 Tree fieldType = getTreeForType(type, make); 722 VariableTree vt = make.Variable( 723 make.Modifiers(modifiers), 724 name, 725 fieldType, 726 null); 727 return vt; 728 } 729 730 731 public static void addField(JavaSource js, final String name, final Set <Modifier> modifiers, final String type) throws IOException { 732 CancellableTask task = new CancellableTask<WorkingCopy>() { 733 public void cancel() { 734 throw new UnsupportedOperationException ("Not supported yet."); 735 } 736 737 public void run(WorkingCopy workingCopy) throws Exception { 738 workingCopy.toPhase(Phase.RESOLVED); 739 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 740 TreeMaker make = workingCopy.getTreeMaker(); 741 ClassTree clazz = null; 742 for (Tree typeDecl : cut.getTypeDecls()) { 743 if (Tree.Kind.CLASS == typeDecl.getKind()) { 744 clazz = (ClassTree) typeDecl; 745 } 746 } VariableTree vt = createField(make, name, modifiers, type); 748 ClassTree modifiedClazz = make.addClassMember(clazz, vt); 749 workingCopy.rewrite(clazz, modifiedClazz); 750 } 751 }; 752 js.runModificationTask(task).commit(); 753 } 754 755 756 public static ClassTree getClassTree( 757 TreeMaker make, 758 WorkingCopy workingCopy, 759 String name, 760 String superClass, 761 List <ExpressionTree> implementsList, 762 Set <Modifier> modifiers) { 763 764 Tree extendsTree = make.QualIdent(workingCopy.getElements().getTypeElement(superClass)); 765 Map <String ,String > params = new HashMap <String , String >(); 766 params.put("param1", "String"); 767 MethodTree mt = Common.createMethod(make, "method", params); 768 VariableTree vt = Common.createField(make, "variable", EnumSet.of(Modifier.PROTECTED), "double"); 769 List <Tree> members = new ArrayList <Tree>(); 770 members.add(mt); 771 members.add(vt); 772 members.add(make.Block(Collections.EMPTY_LIST, false)); 773 ClassTree innerClass = make.Class( 774 make.Modifiers(modifiers), 775 name, 776 Collections.EMPTY_LIST, 777 extendsTree, 778 implementsList, 779 members); 780 return innerClass; 781 } 782 783 public static void addTopLevelClass(JavaSource js) throws IOException { 784 CancellableTask task = new CancellableTask<WorkingCopy>() { 785 public void cancel() { 786 throw new UnsupportedOperationException ("Not supported yet."); 787 } 788 789 public void run(WorkingCopy workingCopy) throws Exception { 790 workingCopy.toPhase(Phase.RESOLVED); 791 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 792 TreeMaker make = workingCopy.getTreeMaker(); 793 ClassTree clazz = getClassTree(make, workingCopy, "TopLevel", "java.util.List", Collections.<ExpressionTree>emptyList(), EnumSet.noneOf(Modifier.class)); 794 CompilationUnitTree copy = make.addCompUnitTypeDecl(cut, clazz); 795 796 workingCopy.rewrite(cut, copy); 797 } 798 }; 799 js.runModificationTask(task).commit(); 800 } 801 802 803 804 public static void addClassComment(JavaSource js, final String text) throws IOException { 805 CancellableTask task = new CancellableTask<WorkingCopy>() { 806 public void cancel() { 807 throw new UnsupportedOperationException ("Not supported yet."); 808 } 809 810 public void run(WorkingCopy workingCopy) throws Exception { 811 workingCopy.toPhase(Phase.RESOLVED); 812 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 813 TreeMaker make = workingCopy.getTreeMaker(); 814 ClassTree clazz = null; 815 for (Tree typeDecl : cut.getTypeDecls()) { 816 if (Tree.Kind.CLASS == typeDecl.getKind()) { 817 clazz = (ClassTree) typeDecl; 818 } 819 } 821 } 822 }; 823 js.runModificationTask(task).commit(); 824 } 825 826 827 950 959 1035 1036 1041 1053 1056 1057} 1058 | Popular Tags |