/** * 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 default lambda body. * * @param lambda a lambda to generate body for * @param method a method of a functional interface to be implemented by the lambda expression * @return the lambda body * @since 2.19 */ public BlockTree createDefaultLambdaBody(LambdaExpressionTree lambda, ExecutableElement method) { try { String bodyTemplate = "{" + readFromTemplate(LAMBDA_BODY, createBindings(null, method)) + "\n}"; //NOI18N return copy.getTreeMaker().createLambdaBody(lambda, bodyTemplate); } catch (Exception e) {} return copy.getTreeMaker().Block(Collections.emptyList(), false); }
/** * 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); }
/** * 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 }
BlockTree body = make.Block(statements, false); return make.Method(make.Modifiers(mods), "<init>", null, typeParams, parameters, throwsList, body, null, constructor!= null ? constructor.isVarArgs() : false); //NOI18N
Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), make.Block(Collections.<StatementTree>emptyList(), false), // "// put your action code here", 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(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); }
Collections.singletonList(par), Collections.EMPTY_LIST, make.Block(Collections.singletonList(make.ExpressionStatement(addInvocation)), false), null );
Collections.singletonList(par), Collections.EMPTY_LIST, make.Block(Collections.singletonList(make.ExpressionStatement(removeInvocation)), false), null );
if (method.getBody() != null) { if (containsErrors(method.getBody())) { copy.rewrite(method.getBody(), make.Block(Collections.<StatementTree>emptyList(), false)); } else { Trees trees = copy.getTrees();
break; case BLOCK: tree = tm.Block(((BlockTree)from).getStatements(), ((BlockTree)from).isStatic()); GeneratorUtilities gu = GeneratorUtilities.get(wc); gu.copyComments(from, tree, true);