/** * Creates a new MethodTree representing constructor. * * @param modifiers the modifiers of this method. * @param typeParameters the list of generic type parameters, or an empty list. * @param parameters the list of parameters, or an empty list. * @param throwsList the list of throws clauses, or an empty list. * @param bodyText the method's code block provided as a plain text * @see com.sun.source.tree.MethodTree */ public MethodTree Constructor(ModifiersTree modifiers, List<? extends TypeParameterTree> typeParameters, List<? extends VariableTree> parameters, List<? extends ExpressionTree> throwsList, String bodyText) { return Method(modifiers, "<init>", null, typeParameters, parameters, throwsList, bodyText, null); }
MemberSelectTree addCall = make.MemberSelect(make.Identifier(changeSupport.getName()), "addPropertyChangeListener"); // NOI18N MethodInvocationTree addInvocation = make.MethodInvocation(Collections.EMPTY_LIST, addCall, Collections.singletonList(make.Identifier(par.getName()))); MethodTree addMethod = make.Method( make.Modifiers(Modifier.PUBLIC, Collections.EMPTY_LIST), "addPropertyChangeListener", // NOI18N
/** * Creates a new MethodTree. * * @param modifiers the modifiers of this method. * @param name the name of the method. * @param returnType the return type for this method. * @param typeParameters the list of generic type parameters, or an empty list. * @param parameters the list of parameters, or an empty list. * @param throwsList the list of throws clauses, or an empty list. * @param body the method's code block. * @param defaultValue the default value, used by annotation types. * @see com.sun.source.tree.MethodTree */ public MethodTree Method(ModifiersTree modifiers, CharSequence name, Tree returnType, List<? extends TypeParameterTree> typeParameters, List<? extends VariableTree> parameters, List<? extends ExpressionTree> throwsList, BlockTree body, ExpressionTree defaultValue) { return Method(modifiers, name, returnType, typeParameters, parameters, throwsList, body, defaultValue, false); }
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), "removePropertyChangeListener", // NOI18N
private MethodTree addBodyEvaluatorCheck(boolean evaluateBody, TreeMaker make) throws IOException { StringBuffer methodBody = new StringBuffer(); methodBody.append("{"); //NOI18N methodBody.append("\n // TODO: code that determines whether the body should be"); //NOI18N methodBody.append("\n // evaluated should be placed here."); //NOI18N methodBody.append("\n // Called from the doStartTag() method."); //NOI18N methodBody.append("\nreturn " + (evaluateBody ? "true;" : "false;")); //NOI18N methodBody.append("\n}"); //NOI18N MethodTree method = make.Method( make.Modifiers(Collections.<Modifier>singleton(Modifier.PRIVATE)), "theBodyShouldBeEvaluated", //NOI18N make.PrimitiveType(TypeKind.BOOLEAN), Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), methodBody.toString(), null); //TODO: generate Javadoc return method; }
public static MethodTree createPropertyGetterMethod(WorkingCopy wc, String name, Class type){ TreeMaker make = wc.getTreeMaker(); return make.Method( createModifiers(wc, Modifier.PUBLIC), Naming.getterName(name), createType(wc, type.getCanonicalName()), Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), "{ return " + name + "; }", // NOI18N null); }
public static MethodTree createPropertySetterMethod(WorkingCopy wc, String name, Class type){ TreeMaker make = wc.getTreeMaker(); String argName = Naming.paramNames(new Class[] { type }, null)[0]; return make.Method( createModifiers(wc, Modifier.PUBLIC), Naming.setterName(name), make.PrimitiveType(TypeKind.VOID), Collections.<TypeParameterTree>emptyList(), Collections.singletonList(createVariable(wc, argName, type)), Collections.<ExpressionTree>emptyList(), "{ this." + name + " = " + argName + "; }", // NOI18N null); }
/** * Creates a new public property getter method. * * @param modifiersTree the method modifiers; cannot be null. * @param propertyType the property type; cannot be null. * @param propertyName the property name; cannot be null. * @return the new method; never null. */ public MethodTree createPropertyGetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) { Parameters.notNull("modifiersTree", modifiersTree); // NOI18N Parameters.javaIdentifier("propertyName", propertyName); // NOI18N Parameters.notNull("propertyType", propertyType); // NOI18N return getTreeMaker().Method( modifiersTree, createPropertyAccessorName(propertyName, true), propertyType, Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), "{ return " + propertyName + "; }", // NOI18N null); }
private ClassTree createMethodsOlderVersion(Collection<String> classNames, TreeMaker maker,ClassTree modified, CompilationController controller) throws IOException { WildcardTree wildCard = maker.Wildcard(Tree.Kind.UNBOUNDED_WILDCARD, null); ParameterizedTypeTree wildClass = maker.ParameterizedType( maker.QualIdent(Class.class.getCanonicalName()), Collections.singletonList(wildCard)); ParameterizedTypeTree wildSet = maker.ParameterizedType( maker.QualIdent(Set.class.getCanonicalName()), Collections.singletonList(wildClass)); //StringBuilder builder = new StringBuilder(); String methodBody = MiscPrivateUtilities.collectRestResources(classNames, restSupport, true); ModifiersTree modifiersTree = maker.Modifiers(EnumSet .of(Modifier.PRIVATE)); MethodTree methodTree = maker.Method(modifiersTree, GET_REST_RESOURCE_CLASSES, wildSet, Collections.<TypeParameterTree> emptyList(), Collections.<VariableTree> emptyList(), Collections.<ExpressionTree> emptyList(), methodBody, null); modified = maker.addClassMember(modified, methodTree); return modified; }
/** * 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 new public property setter method. * * @param modifiersTree the method modifiers; cannot be null. * @param propertyType the property type; cannot be null. * @param propertyName the property name; cannot be null. * @return the new method; never null. */ public MethodTree createPropertySetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) { Parameters.notNull("modifiersTree", modifiersTree); // NOI18N Parameters.javaIdentifier("propertyName", propertyName); // NOI18N Parameters.notNull("propertyType", propertyType); // NOI18N TreeMaker make = getTreeMaker(); return make.Method( modifiersTree, createPropertyAccessorName(propertyName, false), make.PrimitiveType(TypeKind.VOID), Collections.<TypeParameterTree>emptyList(), Collections.singletonList(createVariable(propertyName, propertyType)), Collections.<ExpressionTree>emptyList(), "{ this." + propertyName + " = " + propertyName + "; }", // NOI18N 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); }
maker.Modifiers(Collections.<Modifier>emptySet()), "resources", wildSet, null); MethodTree methodTree = maker.Method(modifiersTree, RestConstants.GET_REST_RESOURCE_CLASSES2, maker.Type("void"), Collections.<TypeParameterTree> emptyList(),
/** * 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 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); }
MethodTree mtree = make.Method(createModifiers(wc, mInfo.getModifiers()), mInfo.getName(), createType(wc, retTypeName),
return make.Method(modsTree, cm.getName(), retTree, methTree.getTypeParameters(), params, methTree.getThrows(), code, null);
public Object run(WorkingCopy wc) { TreeMaker make = wc.getTreeMaker(); ExecutableElement execElement = execElementHandle.resolve(wc); MethodTree methodTree = wc.getTrees().getTree(execElement); MethodTree newMethodTree = wc.getTreeMaker().Method(methodTree.getModifiers(), methodTree.getName(), methodTree.getReturnType(), methodTree.getTypeParameters(), methodTree.getParameters(), methodTree.getThrows(), "{" + bodyText + "}", (ExpressionTree)methodTree.getDefaultValue()); wc.rewrite(methodTree, newMethodTree); return null; } }, javaClass.getFileObject());
/** * Adds an annotation to a method. This is equivalent to {@link TreeMaker#addModifiersAnnotation}, * but it creates and returns a new <code>MethodTree, not a new <code>ModifiersTree</code>. * * @param methodTree the method to add the annotation to; cannot be null. * @param annotationTree the annotation to add; cannot be null. * @return a new method annotated with the new annotation; never null. */ public MethodTree addAnnotation(MethodTree methodTree, AnnotationTree annotationTree) { Parameters.notNull("methodTree", methodTree); // NOI18N Parameters.notNull("annotationTree", annotationTree); // NOI18N TreeMaker make = getTreeMaker(); return make.Method( make.addModifiersAnnotation(methodTree.getModifiers(), annotationTree), methodTree.getName(), methodTree.getReturnType(), methodTree.getTypeParameters(), methodTree.getParameters(), methodTree.getThrows(), methodTree.getBody(), (ExpressionTree)methodTree.getDefaultValue()); }