/** * Check whether given type has a setter method for the given field. * @param type to inspect * @param field to search setter for * @param codeStyle * @return true if setter method exists * * @since 0.136 */ public boolean hasSetter(TypeElement type, VariableElement field, CodeStyle codeStyle) { boolean isStatic = field.getModifiers().contains(Modifier.STATIC); String name = CodeStyleUtils.computeSetterName(field.getSimpleName(), isStatic, codeStyle); return delegate.alreadyDefinedIn(name, info.getTypes().getNoType(TypeKind.VOID), com.sun.tools.javac.util.List.<TypeMirror>of(field.asType()), type); }
/** * 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); }
if (!name.contentEquals(ERROR)) { boolean isStatic = variableElement.getModifiers().contains(Modifier.STATIC); String setterName = CodeStyleUtils.computeSetterName(name, isStatic, codeStyle); String getterName = CodeStyleUtils.computeGetterName(name, variableElement.asType().getKind() == TypeKind.BOOLEAN, isStatic, codeStyle); if ((prefix == null || startsWith(env, getterName)) && !eu.hasGetter(te, variableElement, codeStyle)) {
/** * 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); }