1 19 20 package org.netbeans.modules.j2ee.common.source; 21 22 import com.sun.source.tree.*; 23 import java.io.IOException ; 24 import java.util.Collections ; 25 import java.util.EnumSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import javax.lang.model.element.*; 31 import javax.lang.model.type.*; 32 import org.netbeans.api.java.source.JavaSource.Phase; 33 import org.netbeans.api.java.source.TreeMaker; 34 import org.netbeans.api.java.source.WorkingCopy; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileSystem; 37 import org.openide.filesystems.Repository; 38 import org.openide.loaders.DataFolder; 39 import org.openide.loaders.DataObject; 40 import org.openide.util.Parameters; 41 42 46 public final class GenerationUtils extends SourceUtils { 47 48 54 56 59 static final String CLASS_TEMPLATE = "Templates/Classes/Class.java"; static final String INTERFACE_TEMPLATE = "Templates/Classes/Interface.java"; 62 64 private GenerationUtils(WorkingCopy copy, TypeElement typeElement) { 65 super(copy, typeElement); 66 } 67 68 private GenerationUtils(WorkingCopy copy, ClassTree classTree) { 69 super(copy, classTree); 70 } 71 72 public static GenerationUtils newInstance(WorkingCopy copy, TypeElement typeElement) { 73 Parameters.notNull("copy", copy); Parameters.notNull("typeElement", typeElement); 76 return new GenerationUtils(copy, typeElement); 77 } 78 79 public static GenerationUtils newInstance(WorkingCopy copy, ClassTree classTree) { 80 Parameters.notNull("copy", copy); Parameters.notNull("classTree", classTree); 83 return new GenerationUtils(copy, classTree); 84 } 85 86 public static GenerationUtils newInstance(WorkingCopy copy) throws IOException { 87 Parameters.notNull("copy", copy); 89 ClassTree classTree = findPublicTopLevelClass(copy); 90 if (classTree != null) { 91 return newInstance(copy, classTree); 92 } 93 return null; 94 } 95 96 98 100 110 public static FileObject createClass(FileObject targetFolder, String className, final String javadoc) throws IOException { 111 return createClass(CLASS_TEMPLATE, targetFolder, className, javadoc); 112 } 113 114 124 public static FileObject createInterface(FileObject targetFolder, String interfaceName, final String javadoc) throws IOException { 125 return createClass(INTERFACE_TEMPLATE, targetFolder, interfaceName, javadoc); 126 } 127 128 137 public static FileObject createClass(String template, FileObject targetFolder, String className, final String javadoc) throws IOException { 138 Parameters.notNull("template", template); Parameters.notNull("targetFolder", targetFolder); Parameters.javaIdentifier("className", className); 142 FileObject classFO = createDataObjectFromTemplate(template, targetFolder, className).getPrimaryFile(); 143 157 return classFO; 158 } 159 160 170 public static FileObject createClass(String template, FileObject targetFolder, String className, final String javadoc, 171 Map <String ,? extends Object > parameters) throws IOException { 172 Parameters.notNull("template", template); Parameters.notNull("targetFolder", targetFolder); Parameters.javaIdentifier("className", className); 176 FileObject classFO = createDataObjectFromTemplate(template, targetFolder, className, parameters).getPrimaryFile(); 177 return classFO; 178 } 179 180 182 184 191 private static DataObject createDataObjectFromTemplate(String template, FileObject targetFolder, String targetName) throws IOException { 192 assert template != null; 193 assert targetFolder != null; 194 assert targetName != null && targetName.trim().length() > 0; 195 196 FileSystem defaultFS = Repository.getDefault().getDefaultFileSystem(); 197 FileObject templateFO = defaultFS.findResource(template); 198 DataObject templateDO = DataObject.find(templateFO); 199 DataFolder dataFolder = DataFolder.findFolder(targetFolder); 200 return templateDO.createFromTemplate(dataFolder, targetName); 201 } 202 203 210 private static DataObject createDataObjectFromTemplate(String template, FileObject targetFolder, String targetName, 211 Map <String ,? extends Object > parameters) throws IOException { 212 assert template != null; 213 assert targetFolder != null; 214 assert targetName != null && targetName.trim().length() > 0; 215 216 FileSystem defaultFS = Repository.getDefault().getDefaultFileSystem(); 217 FileObject templateFO = defaultFS.findResource(template); 218 DataObject templateDO = DataObject.find(templateFO); 219 DataFolder dataFolder = DataFolder.findFolder(targetFolder); 220 return templateDO.createFromTemplate(dataFolder, targetName, parameters); 221 } 222 223 225 227 public Tree createType(String typeName) { 228 TreeMaker make = getTreeMaker(); 229 TypeKind primitiveTypeKind = null; 230 if ("boolean".equals(typeName)) { primitiveTypeKind = TypeKind.BOOLEAN; 232 } else if ("byte".equals(typeName)) { primitiveTypeKind = TypeKind.BYTE; 234 } else if ("short".equals(typeName)) { primitiveTypeKind = TypeKind.SHORT; 236 } else if ("int".equals(typeName)) { primitiveTypeKind = TypeKind.INT; 238 } else if ("long".equals(typeName)) { primitiveTypeKind = TypeKind.LONG; 240 } else if ("char".equals(typeName)) { primitiveTypeKind = TypeKind.CHAR; 242 } else if ("float".equals(typeName)) { primitiveTypeKind = TypeKind.FLOAT; 244 } else if ("double".equals(typeName)) { primitiveTypeKind = TypeKind.DOUBLE; 246 } 247 if (primitiveTypeKind != null) { 248 return getTreeMaker().PrimitiveType(primitiveTypeKind); 249 } 250 return createQualIdent(typeName); 251 } 252 253 public ModifiersTree createModifiers(Modifier modifier) { 254 return getTreeMaker().Modifiers(EnumSet.of(modifier), Collections.<AnnotationTree>emptyList()); 255 } 256 257 264 public AnnotationTree createAnnotation(String annotationType) { 265 Parameters.notNull("annotationType", annotationType); 267 return createAnnotation(annotationType, Collections.<ExpressionTree>emptyList()); 268 } 269 270 279 public AnnotationTree createAnnotation(String annotationType, List <? extends ExpressionTree> arguments) { 280 Parameters.notNull("annotationType", annotationType); Parameters.notNull("arguments", arguments); 283 ExpressionTree annotationTypeTree = createQualIdent(annotationType); 284 return getTreeMaker().Annotation(annotationTypeTree, arguments); 285 } 286 287 296 public ExpressionTree createAnnotationArgument(String argumentName, Object argumentValue) { 297 Parameters.javaIdentifierOrNull("argumentName", argumentName); Parameters.notNull("argumentValue", argumentValue); 300 TreeMaker make = getTreeMaker(); 301 ExpressionTree argumentValueTree = make.Literal(argumentValue); 302 if (argumentName == null) { 303 return argumentValueTree; 304 } else { 305 return make.Assignment(make.Identifier(argumentName), argumentValueTree); 306 } 307 } 308 309 316 public ExpressionTree createAnnotationArgument(String argumentName, List <? extends ExpressionTree> argumentValues) { 317 Parameters.javaIdentifierOrNull("argumentName", argumentName); Parameters.notNull("argumentValues", argumentValues); 320 TreeMaker make = getTreeMaker(); 321 ExpressionTree argumentValuesTree = make.NewArray(null, Collections.<ExpressionTree>emptyList(), argumentValues); 322 if (argumentName == null) { 323 return argumentValuesTree; 324 } else { 325 return make.Assignment(make.Identifier(argumentName), argumentValuesTree); 326 } 327 } 328 329 342 public ExpressionTree createAnnotationArgument(String argumentName, String argumentType, String argumentTypeField) { 343 Parameters.javaIdentifierOrNull("argumentName", argumentName); Parameters.notNull("argumentType", argumentType); Parameters.javaIdentifier("argumentTypeField", argumentTypeField); 347 TreeMaker make = getTreeMaker(); 348 MemberSelectTree argumentValueTree = make.MemberSelect(createQualIdent(argumentType), argumentTypeField); 349 if (argumentName == null) { 350 return argumentValueTree; 351 } else { 352 return make.Assignment(make.Identifier(argumentName), argumentValueTree); 353 } 354 } 355 356 363 public ClassTree ensureNoArgConstructor(ClassTree classTree) throws IOException { 364 getWorkingCopy().toPhase(Phase.RESOLVED); 365 366 ExecutableElement constructor = getNoArgConstructor(); 367 MethodTree constructorTree = constructor != null ? getWorkingCopy().getTrees().getTree(constructor) : null; 368 MethodTree newConstructorTree = null; 369 TreeMaker make = getTreeMaker(); 370 if (constructor != null) { 371 if (!constructor.getModifiers().contains(Modifier.PUBLIC)) { 372 ModifiersTree oldModifiersTree = constructorTree.getModifiers(); 373 Set <Modifier> newModifiers = EnumSet.of(Modifier.PUBLIC); 374 for (Modifier modifier : oldModifiersTree.getFlags()) { 375 if (!Modifier.PROTECTED.equals(modifier) && !Modifier.PRIVATE.equals(modifier)) { 376 newModifiers.add(modifier); 377 } 378 } 379 newConstructorTree = make.Constructor( 380 make.Modifiers(newModifiers), 381 constructorTree.getTypeParameters(), 382 constructorTree.getParameters(), 383 constructorTree.getThrows(), 384 constructorTree.getBody()); 385 } 386 } else { 387 newConstructorTree = make.Constructor( 388 createModifiers(Modifier.PUBLIC), 389 Collections.<TypeParameterTree>emptyList(), 390 Collections.<VariableTree>emptyList(), 391 Collections.<ExpressionTree>emptyList(), 392 "{ }"); } 394 ClassTree newClassTree = classTree; 395 if (newConstructorTree != null) { 396 if (constructorTree != null) { 397 newClassTree = make.removeClassMember(newClassTree, constructorTree); 398 } 399 newClassTree = make.addClassMember(newClassTree, newConstructorTree); 400 } 401 return newClassTree; 402 } 403 404 420 public MethodTree createAssignmentConstructor(ModifiersTree modifiersTree, String constructorName, List <VariableTree> parameters) { 421 Parameters.notNull("modifiersTree", modifiersTree); 422 Parameters.javaIdentifier("constructorName", constructorName); Parameters.notNull("parameters", parameters); 425 StringBuilder body = new StringBuilder (parameters.size() * 30); 426 body.append("{"); for (VariableTree parameter : parameters) { 428 String parameterName = parameter.getName().toString(); 429 body.append("this." + parameterName + " = " + parameterName + ";"); } 431 body.append("}"); 433 TreeMaker make = getTreeMaker(); 434 return make.Constructor( 435 modifiersTree, 436 Collections.<TypeParameterTree>emptyList(), 437 parameters, 438 Collections.<ExpressionTree>emptyList(), 439 body.toString()); 440 } 441 442 450 public VariableTree createField(ModifiersTree modifiersTree, String fieldName, String fieldType) { 451 Parameters.notNull("modifiersTree", modifiersTree); Parameters.javaIdentifier("fieldName", fieldName); Parameters.notNull("fieldType", fieldType); 455 return getTreeMaker().Variable( 456 modifiersTree, 457 fieldName, 458 createType(fieldType), 459 null); 460 } 461 462 464 472 public VariableTree createVariable(String variableName, String variableType) { 473 Parameters.javaIdentifier("variableName", variableName); Parameters.notNull("variableType", variableType); 476 return createField( 477 createEmptyModifiers(), 478 variableName, 479 variableType); 480 } 481 482 490 public VariableTree createVariable(String variableName, Tree variableType) { 491 Parameters.javaIdentifier("variableName", variableName); Parameters.notNull("variableType", variableType); 494 return getTreeMaker().Variable( 495 createEmptyModifiers(), 496 variableName, 497 variableType, 498 null); 499 } 500 501 508 public VariableTree removeModifiers(VariableTree variableTree) { 509 Parameters.notNull("variableTree", variableTree); 510 511 TreeMaker make = getTreeMaker(); 512 return make.Variable( 513 createEmptyModifiers(), 514 variableTree.getName(), 515 variableTree.getType(), 516 variableTree.getInitializer()); 517 } 518 519 527 public MethodTree createPropertyGetterMethod(ModifiersTree modifiersTree, String propertyName, String propertyType) throws IOException { 528 Parameters.notNull("modifiersTree", modifiersTree); Parameters.javaIdentifier("propertyName", propertyName); Parameters.notNull("propertyType", propertyType); getWorkingCopy().toPhase(Phase.RESOLVED); 532 533 return createPropertyGetterMethod(modifiersTree, propertyName, createType(propertyType)); 534 } 535 536 544 public MethodTree createPropertyGetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) throws IOException { 545 Parameters.notNull("modifiersTree", modifiersTree); Parameters.javaIdentifier("propertyName", propertyName); Parameters.notNull("propertyType", propertyType); getWorkingCopy().toPhase(Phase.RESOLVED); 549 550 return getTreeMaker().Method( 551 modifiersTree, 552 createPropertyAccessorName(propertyName, true), 553 propertyType, 554 Collections.<TypeParameterTree>emptyList(), 555 Collections.<VariableTree>emptyList(), 556 Collections.<ExpressionTree>emptyList(), 557 "{ return " + propertyName + "; }", null); 559 } 560 561 568 public MethodTree createPropertySetterMethod(ModifiersTree modifiersTree, String propertyName, String propertyType) throws IOException { 569 Parameters.notNull("modifiersTree", modifiersTree); Parameters.javaIdentifier("propertyName", propertyName); Parameters.notNull("propertyType", propertyType); getWorkingCopy().toPhase(Phase.RESOLVED); 573 574 return createPropertySetterMethod(modifiersTree, propertyName, createType(propertyType)); 575 } 576 577 584 public MethodTree createPropertySetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) throws IOException { 585 Parameters.notNull("modifiersTree", modifiersTree); Parameters.javaIdentifier("propertyName", propertyName); Parameters.notNull("propertyType", propertyType); getWorkingCopy().toPhase(Phase.RESOLVED); 589 590 TreeMaker make = getTreeMaker(); 591 return make.Method( 592 modifiersTree, 593 createPropertyAccessorName(propertyName, false), 594 make.PrimitiveType(TypeKind.VOID), 595 Collections.<TypeParameterTree>emptyList(), 596 Collections.singletonList(createVariable(propertyName, propertyType)), 597 Collections.<ExpressionTree>emptyList(), 598 "{ this." + propertyName + " = " + propertyName + "; }", null); 600 } 601 602 610 @SuppressWarnings ("unchecked") public ClassTree addAnnotation(ClassTree classTree, AnnotationTree annotationTree) { 612 Parameters.notNull("classTree", classTree); Parameters.notNull("annotationTree", annotationTree); 615 TreeMaker make = getTreeMaker(); 616 return make.Class( 617 make.addModifiersAnnotation(classTree.getModifiers(), annotationTree), 618 classTree.getSimpleName(), 619 classTree.getTypeParameters(), 620 classTree.getExtendsClause(), 621 (List <ExpressionTree>)classTree.getImplementsClause(), 622 classTree.getMembers()); 623 } 624 625 633 public MethodTree addAnnotation(MethodTree methodTree, AnnotationTree annotationTree) { 634 Parameters.notNull("methodTree", methodTree); Parameters.notNull("annotationTree", annotationTree); 637 TreeMaker make = getTreeMaker(); 638 return make.Method( 639 make.addModifiersAnnotation(methodTree.getModifiers(), annotationTree), 640 methodTree.getName(), 641 methodTree.getReturnType(), 642 methodTree.getTypeParameters(), 643 methodTree.getParameters(), 644 methodTree.getThrows(), 645 methodTree.getBody(), 646 (ExpressionTree)methodTree.getDefaultValue()); 647 } 648 649 657 public VariableTree addAnnotation(VariableTree variableTree, AnnotationTree annotationTree) { 658 Parameters.notNull("variableTree", variableTree); Parameters.notNull("annotationTree", annotationTree); 661 TreeMaker make = getTreeMaker(); 662 return make.Variable( 663 make.addModifiersAnnotation(variableTree.getModifiers(), annotationTree), 664 variableTree.getName(), 665 variableTree.getType(), 666 variableTree.getInitializer()); 667 } 668 669 678 public ClassTree addClassFields(ClassTree classTree, List <? extends VariableTree> fieldTrees) { 679 Parameters.notNull("classTree", classTree); Parameters.notNull("fieldTrees", fieldTrees); 682 int firstNonFieldIndex = 0; 683 Iterator <? extends Tree> memberTrees = classTree.getMembers().iterator(); 684 while (memberTrees.hasNext() && memberTrees.next().getKind() == Tree.Kind.VARIABLE) { 685 firstNonFieldIndex++; 686 } 687 TreeMaker make = getTreeMaker(); 688 ClassTree newClassTree = getClassTree(); 689 for (VariableTree fieldTree : fieldTrees) { 690 newClassTree = make.insertClassMember(newClassTree, firstNonFieldIndex, fieldTree); 691 firstNonFieldIndex++; 692 } 693 return newClassTree; 694 } 695 696 698 704 public ClassTree addImplementsClause(ClassTree classTree, String interfaceType) { 705 if (getTypeElement().getKind() != ElementKind.CLASS) { 706 throw new IllegalStateException ("Cannot add an implements clause to the non-class type " + getTypeElement().getQualifiedName()); } 708 709 ExpressionTree interfaceTree = createQualIdent(interfaceType); 710 return getTreeMaker().addClassImplementsClause(classTree, interfaceTree); 711 } 712 713 715 717 722 private WorkingCopy getWorkingCopy() { 723 return (WorkingCopy)getCompilationController(); 724 } 725 726 private TreeMaker getTreeMaker() { 727 return getWorkingCopy().getTreeMaker(); 728 } 729 730 private ModifiersTree createEmptyModifiers() { 731 return getTreeMaker().Modifiers(Collections.<Modifier>emptySet(), Collections.<AnnotationTree>emptyList()); 732 } 733 734 private ExpressionTree createQualIdent(String typeName) { 735 TypeElement typeElement = getWorkingCopy().getElements().getTypeElement(typeName); 736 if (typeElement == null) { 737 throw new IllegalArgumentException ("Type " + typeName + " cannot be found"); } 739 return getTreeMaker().QualIdent(typeElement); 740 } 741 742 private String createPropertyAccessorName(String propertyName, boolean getter) { 743 assert propertyName.length() > 0; 744 StringBuffer pascalCaseName = new StringBuffer (propertyName); 745 pascalCaseName.setCharAt(0, Character.toUpperCase(pascalCaseName.charAt(0))); 746 return (getter ? "get" : "set") + pascalCaseName; } 748 749 } 751 | Popular Tags |