From c98cec7d2ddceddcc0f127185912be4f826a6caa Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sat, 29 Jan 2011 16:54:14 +0100 Subject: Presence of isFoo(), hasFoo(), and getFoo(), as well as properties named 'isFoo', 'hasFoo', or 'getFoo' would trigger specialized handling for @Getter/@Setter. However, this special handling broke the bean spec, and has been simplified: Only fields named 'isFoo', and only if that field's type is 'boolean', results in both 'isFoo' and 'foo' being considered as possible property names for this property, with 'foo' preferred, so that @Getter boolean isFoo will generate setFoo and isFoo methods, not setIsFoo and isIsFoo. Fixes issue #148 --- src/core/lombok/javac/handlers/HandleSetter.java | 27 +++++++++++++--------- .../lombok/javac/handlers/JavacHandlerUtil.java | 16 ++++++++++++- 2 files changed, 31 insertions(+), 12 deletions(-) (limited to 'src/core/lombok/javac') diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 6dd88abd..ec5195ac 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -174,17 +174,22 @@ public class HandleSetter implements JavacAnnotationHandler { JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); String methodName = toSetterName(fieldDecl); - switch (methodExists(methodName, fieldNode, false)) { - case EXISTS_BY_LOMBOK: - return true; - case EXISTS_BY_USER: - if (whineIfExists) errorNode.addWarning( - String.format("Not generating %s(%s %s): A method with that name already exists", - methodName, fieldDecl.vartype, fieldDecl.name)); - return true; - default: - case NOT_EXISTS: - //continue with creating the setter + for (String altName : toAllSetterNames(fieldDecl)) { + switch (methodExists(altName, fieldNode, false)) { + case EXISTS_BY_LOMBOK: + return true; + case EXISTS_BY_USER: + if (whineIfExists) { + String altNameExpl = ""; + if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName); + errorNode.addWarning( + String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl)); + } + return true; + default: + case NOT_EXISTS: + //continue scanning the other alt names. + } } long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index aa4e1bb2..1fb72dfa 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -158,6 +158,18 @@ public class JavacHandlerUtil { return TransformationsUtil.toGetterName(fieldName, isBoolean); } + /** + * Translates the given field into all possible setter names. + * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(CharSequence, boolean)}. + */ + public static java.util.List toAllSetterNames(JCVariableDecl field) { + CharSequence fieldName = field.name; + + boolean isBoolean = field.vartype.toString().equals("boolean"); + + return TransformationsUtil.toAllSetterNames(fieldName, isBoolean); + } + /** * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo). * @@ -166,7 +178,9 @@ public class JavacHandlerUtil { public static String toSetterName(JCVariableDecl field) { CharSequence fieldName = field.name; - return TransformationsUtil.toSetterName(fieldName); + boolean isBoolean = field.vartype.toString().equals("boolean"); + + return TransformationsUtil.toSetterName(fieldName, isBoolean); } /** Serves as return value for the methods that check for the existence of fields and methods. */ -- cgit