public static MethodInvocationTree createMethodInvocation(WorkingCopy wc, String methodName, String[] pNames) { TreeMaker treeMaker = wc.getTreeMaker(); List<ExpressionTree> args = new ArrayList<ExpressionTree>(); for (int i = 0 ; pNames != null && i < pNames.length; i++) { ExpressionTree tree = treeMaker.Identifier(pNames[i]); args.add(tree); } return treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), treeMaker.Identifier(methodName), args); }
private ExpressionTree qualIdentFor(Element e) { TreeMaker tm = copy.getTreeMaker(); if (e.getKind() == ElementKind.PACKAGE) { String name = ((PackageElement)e).getQualifiedName().toString(); if (e instanceof Symbol) { int lastDot = name.lastIndexOf('.'); if (lastDot < 0) return tm.Identifier(e); return tm.MemberSelect(qualIdentFor(name.substring(0, lastDot)), e); } return qualIdentFor(name); } Element ee = e.getEnclosingElement(); if (e instanceof Symbol) return ee.getSimpleName().length() > 0 ? tm.MemberSelect(qualIdentFor(ee), e) : tm.Identifier(e); return ee.getSimpleName().length() > 0 ? tm.MemberSelect(qualIdentFor(ee), e.getSimpleName()) : tm.Identifier(e.getSimpleName()); }
/** * Creates a new CompilationUnitTree. * @param sourceRoot a source root under which the new file is created * @param path a relative path to file separated by '/' * @param imports a list of import statements. * @param typeDeclarations a list of type (class, interface or enum) declarations. * @see com.sun.source.tree.CompilationUnitTree */ public CompilationUnitTree CompilationUnit(FileObject sourceRoot, String path, List<? extends ImportTree> imports, List<? extends Tree> typeDeclarations) { String[] nameComponent = FileObjects.getFolderAndBaseName(path,'/'); //NOI18N JavaFileObject sourceFile = FileObjects.templateFileObject(sourceRoot, nameComponent[0], nameComponent[1]); IdentifierTree pkg = nameComponent[0].length() == 0 ? null : Identifier(nameComponent[0].replace('/', '.')); return delegate.CompilationUnit(Collections.<AnnotationTree>emptyList(), pkg, imports, typeDeclarations, sourceFile); }
public static MethodInvocationTree createMethodInvocation(WorkingCopy wc, String beanName, String methodName, List<ExpressionTree> args) { TreeMaker treeMaker = wc.getTreeMaker(); return treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), treeMaker.MemberSelect(treeMaker.Identifier(beanName), methodName), args); }
/** * Creates a new CompilationUnitTree. * @param packageAnnotations package annotations * @param sourceRoot a source root under which the new file is created * @param path a relative path to file separated by '/' * @param imports a list of import statements. * @param typeDeclarations a list of type (class, interface or enum) declarations. * @see com.sun.source.tree.CompilationUnitTree * @since 0.66 */ public CompilationUnitTree CompilationUnit(List<? extends AnnotationTree> packageAnnotations, FileObject sourceRoot, String path, List<? extends ImportTree> imports, List<? extends Tree> typeDeclarations) { String[] nameComponent = FileObjects.getFolderAndBaseName(path,'/'); //NOI18N JavaFileObject sourceFile = FileObjects.templateFileObject(sourceRoot, nameComponent[0], nameComponent[1]); IdentifierTree pkg = nameComponent[0].length() == 0 ? null : Identifier(nameComponent[0].replace('/', '.')); return delegate.CompilationUnit(packageAnnotations, pkg, imports, typeDeclarations, sourceFile); }
private ExpressionTree qualIdentFor(String name) { Elements elements = copy.getElements(); TreeMaker tm = copy.getTreeMaker(); int lastDot = name.lastIndexOf('.'); if (lastDot < 0) return tm.Identifier(elements.getName(name)); return tm.MemberSelect(qualIdentFor(name.substring(0, lastDot)), elements.getName(name.substring(lastDot + 1))); }
public Object run(WorkingCopy wc) { MethodInvocationTree methInvk = getDelegate(wc); if(methInvk != null) { IdentifierTree oldTree = (IdentifierTree)methInvk.getMethodSelect(); IdentifierTree newTree = wc.getTreeMaker().Identifier(name); wc.rewrite(oldTree, newTree); } return null; } }, javaClass.getFileObject());
/** * Creates a getter method for a field. * * @param clazz the class to create the getter within * @param field field to create getter for * @return the getter method * @since 0.20 */ public MethodTree createGetter(TypeElement clazz, VariableElement field) { assert clazz != null && field != null; TreeMaker make = copy.getTreeMaker(); CodeStyle cs = DiffContext.getCodeStyle(copy); Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC); boolean isStatic = field.getModifiers().contains(Modifier.STATIC); if (isStatic) { mods.add(Modifier.STATIC); } TypeMirror type = copy.getTypes().asMemberOf((DeclaredType)clazz.asType(), field); boolean isBoolean = type.getKind() == TypeKind.BOOLEAN; String getterName = CodeStyleUtils.computeGetterName(field.getSimpleName(), isBoolean, isStatic, cs); BlockTree body = make.Block(Collections.singletonList(make.Return(make.Identifier(field.getSimpleName()))), false); return make.Method(make.Modifiers(mods), getterName, make.Type(type), Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body, null); }
/** * Creates a setter method for a field. * * @param clazz the class to create the setter within * @param field field to create setter for * @return the setter method * @since 0.20 */ public MethodTree createSetter(TypeElement clazz, VariableElement field) { assert clazz != null && field != null; TreeMaker make = copy.getTreeMaker(); CodeStyle cs = DiffContext.getCodeStyle(copy); Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC); boolean isStatic = field.getModifiers().contains(Modifier.STATIC); if (isStatic) { mods.add(Modifier.STATIC); } CharSequence name = field.getSimpleName(); assert name.length() > 0; TypeMirror type = copy.getTypes().asMemberOf((DeclaredType)clazz.asType(), field); String setterName = CodeStyleUtils.computeSetterName(field.getSimpleName(), isStatic, cs); String paramName = addParamPrefixSuffix(removeFieldPrefixSuffix(field, cs), cs); List<VariableTree> params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), paramName, make.Type(type), null)); BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(field.getEnclosingElement().getSimpleName()) : make.Identifier("this"), name), make.Identifier(paramName)))), false); //NOI18N return make.Method(make.Modifiers(mods), setterName, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.<TypeParameterTree>emptyList(), params, Collections.<ExpressionTree>emptyList(), body, null); }
/** * Creates a new annotation argument whose value is an array. * * @param argumentName the argument name; cannot be null. * @param argumentValues the argument values to initialize the array with; cannot be null. * @return the new annotation argument; never null. */ public ExpressionTree createAnnotationArgument(String argumentName, List<? extends ExpressionTree> argumentValues) { Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N Parameters.notNull("argumentValues", argumentValues); // NOI18N TreeMaker make = getTreeMaker(); ExpressionTree argumentValuesTree = make.NewArray(null, Collections.<ExpressionTree>emptyList(), argumentValues); if (argumentName == null) { return argumentValuesTree; } else { return make.Assignment(make.Identifier(argumentName), argumentValuesTree); } }
/** * Creates a class constructor. * * @param clazz the class to create the constructor for * @param fields fields to be initialized by the constructor * @return the constructor * @since 0.20 */ public MethodTree createConstructor(ClassTree clazz, Iterable<? extends VariableTree> fields) { assert clazz != null && fields != null; TreeMaker make = copy.getTreeMaker(); CodeStyle cs = DiffContext.getCodeStyle(copy); Set<Modifier> mods = EnumSet.of(copy.getTreeUtilities().isEnum(clazz) ? Modifier.PRIVATE : Modifier.PUBLIC); List<VariableTree> parameters = new ArrayList<VariableTree>(); List<StatementTree> statements = new ArrayList<StatementTree>(); ModifiersTree parameterModifiers = make.Modifiers(EnumSet.noneOf(Modifier.class)); for (VariableTree vt : fields) { String paramName = addParamPrefixSuffix(removeFieldPrefixSuffix(vt, cs), cs); parameters.add(make.Variable(parameterModifiers, paramName, vt.getType(), null)); statements.add(make.ExpressionStatement(make.Assignment(make.MemberSelect(make.Identifier("this"), vt.getName()), make.Identifier(paramName)))); //NOI18N } BlockTree body = make.Block(statements, false); return make.Method(make.Modifiers(mods), "<init>", null, Collections.<TypeParameterTree> emptyList(), parameters, Collections.<ExpressionTree>emptyList(), body, null); //NOI18N }
/** * Creates a setter method for a field. * * @param clazz the class to create the setter within * @param field field to create setter for * @return the setter method * @since 0.20 */ public MethodTree createSetter(ClassTree clazz, VariableTree field) { assert clazz != null && field != null; TreeMaker make = copy.getTreeMaker(); Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC); boolean isStatic = field.getModifiers().getFlags().contains(Modifier.STATIC); if (isStatic) mods.add(Modifier.STATIC); CharSequence name = field.getName(); assert name.length() > 0; CodeStyle cs = DiffContext.getCodeStyle(copy); String propName = removeFieldPrefixSuffix(field, cs); String setterName = CodeStyleUtils.computeSetterName(field.getName(), isStatic, cs); String paramName = addParamPrefixSuffix(propName, cs); List<VariableTree> params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), paramName, field.getType(), null)); BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(clazz.getSimpleName()) : make.Identifier("this"), name), make.Identifier(paramName)))), false); //NOI18N return make.Method(make.Modifiers(mods), setterName, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.<TypeParameterTree>emptyList(), params, Collections.<ExpressionTree>emptyList(), body, null); }
public Void visitIdentifier(IdentifierTree node, Void p) { TreePath path = getCurrentPath(); Element element = info.getTrees().getElement(path); if (element != null && element.asType().getKind() != TypeKind.ERROR) { // solve the imports only when declared type!!! if (element.getKind().isClass() || element.getKind().isInterface() || (element.getKind().isField() && ((Symbol) element).isStatic())) { Tree parent = path.getParentPath() != null ? path.getParentPath().getLeaf() : null; if ( (parent != null && parent.getKind() == Kind.CASE && ((CaseTree) parent).getExpression() == node && element.getKind() == ElementKind.ENUM_CONSTANT) || (path.getCompilationUnit() != null && ((Symbol) element).enclClass() != null && path.getCompilationUnit().getSourceFile() == ((Symbol) element).enclClass().sourcefile)) { translateMap.put(node, make.Identifier(element.getSimpleName())); } else { translateMap.put(node, make.QualIdent(element)); } } } return null; }
/** * Creates a getter method for a field. * * @param field field to create getter for * @return the getter method * @since 0.20 */ public MethodTree createGetter(VariableTree field) { assert field != null; TreeMaker make = copy.getTreeMaker(); CodeStyle cs = DiffContext.getCodeStyle(copy); Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC); boolean isStatic = field.getModifiers().getFlags().contains(Modifier.STATIC); if (isStatic) { mods.add(Modifier.STATIC); } Tree type = field.getType(); boolean isBoolean = type.getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree) type).getPrimitiveTypeKind() == TypeKind.BOOLEAN; String getterName = CodeStyleUtils.computeGetterName(field.getName(), isBoolean, isStatic, cs); BlockTree body = make.Block(Collections.singletonList(make.Return(make.Identifier(field.getName()))), false); return make.Method(make.Modifiers(mods), getterName, type, Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body, null); }
private static AnnotationTree createAnnotation(ProxyAction action, TreeMaker make, WorkingCopy workingCopy) { List<AssignmentTree> annAttrs = new LinkedList<AssignmentTree>(); for (String attrName : ProxyAction.getAnnotationAttributeNames()) { if (action.isAnnotationAttributeSet(attrName)) { Object value = action.getAnnotationAttributeValue(attrName); ExpressionTree expTree; if (value instanceof String) { expTree = make.Literal(value); } else if (value instanceof ProxyAction.BlockingType) { expTree = make.MemberSelect( make.MemberSelect(make.QualIdent(workingCopy.getElements().getTypeElement("org.jdesktop.application.Task")), // NOI18N "BlockingScope"), // NOI18N value.toString()); } else { continue; } ExpressionTree identTree = make.Identifier(attrName); AssignmentTree attrTree = make.Assignment(identTree, expTree); annAttrs.add(attrTree); } } return make.Annotation(make.QualIdent(workingCopy.getElements().getTypeElement("org.jdesktop.application.Action")), annAttrs); // NOI18N }
/** * Creates a new annotation argument whose value is a literal. * * @param argumentName the argument name; cannot be null. * @param argumentValue the argument value; cannot be null. The semantics * of this parameter is the same as of the parameters of * {@link TreeMaker#Literal(Object)}. * @return the new annotation argument; never null. */ public ExpressionTree createAnnotationArgument(String argumentName, Object argumentValue) { Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N Parameters.notNull("argumentValue", argumentValue); // NOI18N TreeMaker make = getTreeMaker(); ExpressionTree argumentValueTree = make.Literal(argumentValue); if (argumentName == null) { return argumentValueTree; } else { return make.Assignment(make.Identifier(argumentName), argumentValueTree); } }
TypeElement changeSupportElement = wc.getElements().getTypeElement("java.beans.PropertyChangeSupport"); // NOI18N VariableTree changeSupport = make.Variable(parMods, "changeSupport", make.QualIdent(changeSupportElement), null); // NOI18N MemberSelectTree removeCall = make.MemberSelect(make.Identifier(changeSupport.getName()), "removePropertyChangeListener"); // NOI18N MethodInvocationTree removeInvocation = make.MethodInvocation(Collections.EMPTY_LIST, removeCall, Collections.singletonList(make.Identifier(par.getName()))); MethodTree removeMethod = make.Method( make.Modifiers(Modifier.PUBLIC, Collections.EMPTY_LIST),
String varName = ((IdentifierTree) at.getVariable()).getName().toString(); if (varName.equals("name")) { //NOI18N ExpressionTree valueTree = make.Identifier(at.getExpression().toString()); arguments.add(valueTree);
/** * Creates a new annotation argument whose value is a member of a type. For * example it can be used to generate <code>@Target(ElementType.CONSTRUCTOR)</code>. * * @param argumentName the argument name; cannot be null. * @param argumentType the fully-qualified name of the type whose member is to be invoked * (e.g. <code>java.lang.annotations.ElementType</code> in the previous * example); cannot be null. * @param argumentTypeField a field of <code>argumentType</code> * (e.g. <code>CONSTRUCTOR</code> in the previous example); * cannot be null. * @return the new annotation argument; never null. */ public ExpressionTree createAnnotationArgument(String argumentName, String argumentType, String argumentTypeField) { Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N Parameters.notNull("argumentType", argumentType); // NOI18N Parameters.javaIdentifier("argumentTypeField", argumentTypeField); // NOI18N TreeMaker make = getTreeMaker(); MemberSelectTree argumentValueTree = make.MemberSelect(createQualIdent(argumentType), argumentTypeField); if (argumentName == null) { return argumentValueTree; } else { return make.Assignment(make.Identifier(argumentName), argumentValueTree); } }
public void run (WorkingCopy workingCopy) throws Exception { workingCopy.toPhase (JavaSource.Phase.ELEMENTS_RESOLVED); CompilationUnitTree compilationUnit = workingCopy.getCompilationUnit (); TreeMaker treeMaker = workingCopy.getTreeMaker (); Trees trees = workingCopy.getTrees (); ContainsMethodTreeVisitor visitor = new ContainsMethodTreeVisitor (trees, methodName, parameters); visitor.scan (compilationUnit, null); TreePath classTreePath = visitor.getClassTreePath (); if (classTreePath != null) { ExpressionTree expressionTree = findImplementIdentifier (trees, classTreePath, className); if (visitor.isMethodExists ()) { if (expressionTree == null) { TypeElement typeElement = workingCopy.getElements ().getTypeElement (className); ExpressionTree implementsClause = typeElement != null ? treeMaker.QualIdent (typeElement) : treeMaker.Identifier (className); ClassTree oldClassTree = (ClassTree) classTreePath.getLeaf (); ClassTree newClassTree = treeMaker.addClassImplementsClause (oldClassTree, implementsClause); workingCopy.rewrite (oldClassTree, newClassTree); } } else { if (expressionTree != null) { ClassTree oldClassTree = (ClassTree) classTreePath.getLeaf (); ClassTree newClassTree = treeMaker.removeClassImplementsClause (oldClassTree, expressionTree); workingCopy.rewrite (oldClassTree, newClassTree); } } } } });