aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2012-03-21 11:41:59 +0100
committerReinier Zwitserloot <reinier@zwitserloot.com>2012-03-21 11:41:59 +0100
commitcbaa53a9b54589f05a580d63dfc065be03bb4b88 (patch)
tree7285e222b281af312907e14dae2d1cff47c3d2af /src/core/lombok
parentfb76daf5e4320e31f19dcbd6c7a4036109d71ad7 (diff)
downloadlombok-cbaa53a9b54589f05a580d63dfc065be03bb4b88.tar.gz
lombok-cbaa53a9b54589f05a580d63dfc065be03bb4b88.tar.bz2
lombok-cbaa53a9b54589f05a580d63dfc065be03bb4b88.zip
Implementation of @Accessors.
Diffstat (limited to 'src/core/lombok')
-rw-r--r--src/core/lombok/core/TransformationsUtil.java262
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java74
-rw-r--r--src/core/lombok/eclipse/handlers/HandleGetter.java10
-rw-r--r--src/core/lombok/eclipse/handlers/HandleSetter.java8
-rw-r--r--src/core/lombok/javac/handlers/HandleGetter.java11
-rw-r--r--src/core/lombok/javac/handlers/HandleSetter.java19
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java87
7 files changed, 452 insertions, 19 deletions
diff --git a/src/core/lombok/core/TransformationsUtil.java b/src/core/lombok/core/TransformationsUtil.java
new file mode 100644
index 00000000..d0eea4a2
--- /dev/null
+++ b/src/core/lombok/core/TransformationsUtil.java
@@ -0,0 +1,262 @@
+/*
+ * Copyright (C) 2009-2012 The Project Lombok Authors.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok.core;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import lombok.experimental.Accessors;
+
+/**
+ * Container for static utility methods useful for some of the standard lombok transformations, regardless of
+ * target platform (e.g. useful for both javac and Eclipse lombok implementations).
+ */
+public class TransformationsUtil {
+ private TransformationsUtil() {
+ //Prevent instantiation
+ }
+
+ /**
+ * Given the name of a field, return the 'base name' of that field. For example, {@code fFoobar} becomes {@code foobar} if {@code f} is in the prefix list.
+ * For prefixes that end in a letter character, the next character must be a non-lowercase character (i.e. {@code hashCode} is not {@code ashCode} even if
+ * {@code h} is in the prefix list, but {@code hAshcode} would become {@code ashCode}). The first prefix that matches is used. If the prefix list is empty,
+ * or the empty string is in the prefix list and no prefix before it matches, the fieldName will be returned verbatim.
+ *
+ * If no prefix matches and the empty string is not in the prefix list and the prefix list is not empty, {@code null} is returned.
+ *
+ * @param fieldName The full name of a field.
+ * @param prefixes A list of prefixes, usually provided by the {@code Accessors} settings annotation, listing field prefixes.
+ * @return The base name of the field.
+ */
+ private static CharSequence removePrefix(CharSequence fieldName, String[] prefixes) {
+ if (prefixes == null || prefixes.length == 0) return fieldName;
+
+ outer:
+ for (String prefix : prefixes) {
+ if (prefix.length() == 0) return fieldName;
+ if (fieldName.length() <= prefix.length()) continue outer;
+ for (int i = 0; i < prefix.length(); i++) {
+ if (fieldName.charAt(i) != prefix.charAt(i)) continue outer;
+ }
+ char followupChar = fieldName.charAt(prefix.length());
+ // if prefix is a letter then follow up letter needs to not be lowercase, i.e. 'foo' is not a match
+ // as field named 'oo' with prefix 'f', but 'fOo' would be.
+ if (Character.isLetter(prefix.charAt(prefix.length() - 1)) &&
+ Character.isLowerCase(followupChar)) continue outer;
+ return "" + Character.toLowerCase(followupChar) + fieldName.subSequence(prefix.length() + 1, fieldName.length());
+ }
+
+ return null;
+ }
+
+ /** Matches any of the 8 primitive names, such as {@code boolean}. */
+ public static final Pattern PRIMITIVE_TYPE_NAME_PATTERN = Pattern.compile(
+ "^(boolean|byte|short|int|long|float|double|char)$");
+
+ /* NB: 'notnull' is not part of the pattern because there are lots of @NotNull annotations out there that are crappily named and actually mean
+ something else, such as 'this field must not be null _when saved to the db_ but its perfectly okay to start out as such, and a no-args
+ constructor and the implied starts-out-as-null state that goes with it is in fact mandatory' which happens with javax.validation.constraints.NotNull.
+ Various problems with spring have also been reported. See issue #287, issue #271, and issue #43. */
+
+ /** Matches the simple part of any annotation that lombok considers as indicative of NonNull status. */
+ public static final Pattern NON_NULL_PATTERN = Pattern.compile("^(?:nonnull)$", Pattern.CASE_INSENSITIVE);
+
+ /** Matches the simple part of any annotation that lombok considers as indicative of Nullable status. */
+ public static final Pattern NULLABLE_PATTERN = Pattern.compile("^(?:nullable|checkfornull)$", Pattern.CASE_INSENSITIVE);
+
+ /**
+ * Generates a getter name from a given field name.
+ *
+ * Strategy:
+ * <ul>
+ * <li>Reduce the field's name to its base name by stripping off any prefix (from {@code Accessors}). If the field name does not fit
+ * the prefix list, this method immediately returns {@null}.</li>
+ * <li>If {@code Accessors} has {@code fluent=true}, then return the basename.</li>
+ * <li>Pick a prefix. 'get' normally, but 'is' if {@code isBoolean} is true.</li>
+ * <li>Only if {@code isBoolean} is true: Check if the field starts with {@code is} followed by a non-lowercase character. If so, return the field name verbatim.</li>
+ * <li>Check if the first character of the field is lowercase. If so, check if the second character
+ * exists and is title or upper case. If so, uppercase the first character. If not, titlecase the first character.</li>
+ * <li>Return the prefix plus the possibly title/uppercased first character, and the rest of the field name.</li>
+ * </ul>
+ *
+ * @param accessors Accessors configuration.
+ * @param fieldName the name of the field.
+ * @param isBoolean if the field is of type 'boolean'. For fields of type {@code java.lang.Boolean}, you should provide {@code false}.
+ * @return The getter name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
+ */
+ public static String toGetterName(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
+ if (fieldName.length() == 0) return null;
+
+ Accessors ac = accessors.getInstance();
+ fieldName = removePrefix(fieldName, ac.prefix());
+ if (fieldName == null) return null;
+
+ if (ac.fluent()) return fieldName.toString();
+
+ if (isBoolean && fieldName.toString().startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) {
+ // The field is for example named 'isRunning'.
+ return fieldName.toString();
+ }
+
+ return buildName(isBoolean ? "is" : "get", fieldName.toString());
+ }
+
+ /**
+ * Generates a setter name from a given field name.
+ *
+ * Strategy:
+ * <ul>
+ * <li>Reduce the field's name to its base name by stripping off any prefix (from {@code Accessors}). If the field name does not fit
+ * the prefix list, this method immediately returns {@null}.</li>
+ * <li>If {@code Accessors} has {@code fluent=true}, then return the basename.</li>
+ * <li>Only if {@code isBoolean} is true: Check if the field starts with {@code is} followed by a non-lowercase character.
+ * If so, replace {@code is} with {@code set} and return that.</li>
+ * <li>Check if the first character of the field is lowercase. If so, check if the second character
+ * exists and is title or upper case. If so, uppercase the first character. If not, titlecase the first character.</li>
+ * <li>Return {@code "set"} plus the possibly title/uppercased first character, and the rest of the field name.</li>
+ * </ul>
+ *
+ * @param accessors Accessors configuration.
+ * @param fieldName the name of the field.
+ * @param isBoolean if the field is of type 'boolean'. For fields of type {@code java.lang.Boolean}, you should provide {@code false}.
+ * @return The setter name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
+ */
+ public static String toSetterName(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
+ if (fieldName.length() == 0) return null;
+ Accessors ac = accessors.getInstance();
+ fieldName = removePrefix(fieldName, ac.prefix());
+ if (fieldName == null) return null;
+
+ if (ac.fluent()) return fieldName.toString();
+
+ if (isBoolean && fieldName.toString().startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) {
+ // The field is for example named 'isRunning'.
+ return "set" + fieldName.toString().substring(2);
+ }
+
+ return buildName("set", fieldName.toString());
+ }
+
+ /**
+ * Returns all names of methods that would represent the getter for a field with the provided name.
+ *
+ * For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br />
+ * {@code [isRunning, getRunning, isIsRunning, getIsRunning]}
+ *
+ * @param accessors Accessors configuration.
+ * @param fieldName the name of the field.
+ * @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
+ */
+ public static List<String> toAllGetterNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
+ if (!isBoolean) return Collections.singletonList(toGetterName(accessors, fieldName, false));
+
+ Accessors acc = accessors.getInstance();
+ fieldName = removePrefix(fieldName, acc.prefix());
+ if (fieldName == null) return Collections.emptyList();
+
+ List<String> baseNames = toBaseNames(fieldName, isBoolean, acc.fluent());
+
+ Set<String> names = new HashSet<String>();
+ for (String baseName : baseNames) {
+ if (acc.fluent()) {
+ names.add(baseName);
+ } else {
+ names.add(buildName("is", baseName));
+ names.add(buildName("get", baseName));
+ }
+ }
+
+ return new ArrayList<String>(names);
+ }
+
+ /**
+ * Returns all names of methods that would represent the setter for a field with the provided name.
+ *
+ * For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br />
+ * {@code [setRunning, setIsRunning]}
+ *
+ * @param fieldName the name of the field.
+ * @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
+ */
+ public static List<String> toAllSetterNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
+ if (!isBoolean) return Collections.singletonList(toSetterName(accessors, fieldName, false));
+
+ Accessors acc = accessors.getInstance();
+ fieldName = removePrefix(fieldName, acc.prefix());
+ if (fieldName == null) return Collections.emptyList();
+
+ List<String> baseNames = toBaseNames(fieldName, isBoolean, acc.fluent());
+
+ Set<String> names = new HashSet<String>();
+ for (String baseName : baseNames) {
+ if (acc.fluent()) {
+ names.add(baseName);
+ } else {
+ names.add("set" + baseName);
+ }
+ }
+
+ return new ArrayList<String>(names);
+ }
+
+ private static List<String> toBaseNames(CharSequence fieldName, boolean isBoolean, boolean fluent) {
+ List<String> baseNames = new ArrayList<String>();
+ baseNames.add(fieldName.toString());
+
+ // isPrefix = field is called something like 'isRunning', so 'running' could also be the fieldname.
+ String fName = fieldName.toString();
+ if (fName.startsWith("is") && fName.length() > 2 && !Character.isLowerCase(fName.charAt(2))) {
+ String baseName = fName.substring(2);
+ if (fluent) {
+ baseNames.add("" + Character.toLowerCase(baseName.charAt(0)) + baseName.substring(1));
+ } else {
+ baseNames.add(baseName);
+ }
+ }
+
+ return baseNames;
+ }
+
+ /**
+ * @param prefix Something like {@code get} or {@code set} or {@code is}.
+ * @param suffix Something like {@code running}.
+ * @return prefix + smartly title-cased suffix. For example, {@code setRunning}.
+ */
+ private static String buildName(String prefix, String suffix) {
+ if (suffix.length() == 0) return prefix;
+ if (prefix.length() == 0) return suffix;
+
+ char first = suffix.charAt(0);
+ if (Character.isLowerCase(first)) {
+ boolean useUpperCase = suffix.length() > 2 &&
+ (Character.isTitleCase(suffix.charAt(1)) || Character.isUpperCase(suffix.charAt(1)));
+ suffix = String.format("%s%s",
+ useUpperCase ? Character.toUpperCase(first) : Character.toTitleCase(first),
+ suffix.subSequence(1, suffix.length()));
+ }
+ return String.format("%s%s", prefix, suffix);
+ }
+}
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index f795197b..d0fc0f33 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -41,11 +41,12 @@ import lombok.Data;
import lombok.Getter;
import lombok.Lombok;
import lombok.core.AnnotationValues;
+import lombok.core.TransformationsUtil;
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues.AnnotationValue;
import lombok.eclipse.EclipseAST;
import lombok.eclipse.EclipseNode;
-import lombok.core.TransformationsUtil;
+import lombok.experimental.Accessors;
import lombok.core.TypeResolver;
import org.eclipse.core.runtime.ILog;
@@ -756,7 +757,7 @@ public class EclipseHandlerUtil {
TypeReference fieldType = ((FieldDeclaration)field.get()).type;
boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0;
EclipseNode typeNode = field.up();
- for (String potentialGetterName : TransformationsUtil.toAllGetterNames(field.getName(), isBoolean)) {
+ for (String potentialGetterName : toAllGetterNames(field, isBoolean)) {
for (EclipseNode potentialGetter : typeNode.down()) {
if (potentialGetter.getKind() != Kind.METHOD) continue;
if (!(potentialGetter.get() instanceof MethodDeclaration)) continue;
@@ -799,7 +800,8 @@ public class EclipseHandlerUtil {
}
if (hasGetterAnnotation) {
- String getterName = TransformationsUtil.toGetterName(field.getName(), isBoolean);
+ String getterName = toGetterName(field, isBoolean);
+ if (getterName == null) return null;
return new GetterMethod(getterName.toCharArray(), fieldType);
}
@@ -909,6 +911,52 @@ public class EclipseHandlerUtil {
}
/**
+ * Translates the given field into all possible getter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static List<String> toAllGetterNames(EclipseNode field, boolean isBoolean) {
+ String fieldName = field.getName();
+ AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static String toGetterName(EclipseNode field, boolean isBoolean) {
+ String fieldName = field.getName();
+ AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toGetterName(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * Translates the given field into all possible setter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static java.util.List<String> toAllSetterNames(EclipseNode field, boolean isBoolean) {
+ String fieldName = field.getName();
+ AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toAllSetterNames(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static String toSetterName(EclipseNode field, boolean isBoolean) {
+ String fieldName = field.getName();
+ AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean);
+ }
+
+ /**
* Checks if the field should be included in operations that work on 'all' fields:
* If the field is static, or starts with a '$', or is actually an enum constant, 'false' is returned, indicating you should skip it.
*/
@@ -928,6 +976,26 @@ public class EclipseHandlerUtil {
return true;
}
+ public static AnnotationValues<Accessors> getAccessorsForField(EclipseNode field) {
+ for (EclipseNode node : field.down()) {
+ if (annotationTypeMatches(Accessors.class, node)) {
+ return createAnnotation(Accessors.class, node);
+ }
+ }
+
+ EclipseNode current = field.up();
+ while (current != null) {
+ for (EclipseNode node : field.down()) {
+ if (annotationTypeMatches(Accessors.class, node)) {
+ return createAnnotation(Accessors.class, node);
+ }
+ }
+ current = field.up();
+ }
+
+ return AnnotationValues.of(Accessors.class, field);
+ }
+
/**
* Checks if there is a field with the provided name.
*
diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java
index 92f1177f..c631e583 100644
--- a/src/core/lombok/eclipse/handlers/HandleGetter.java
+++ b/src/core/lombok/eclipse/handlers/HandleGetter.java
@@ -187,13 +187,17 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> {
}
TypeReference fieldType = copyType(field.type, source);
- String fieldName = new String(field.name);
boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0;
- String getterName = TransformationsUtil.toGetterName(fieldName, isBoolean);
+ String getterName = toGetterName(fieldNode, isBoolean);
+
+ if (getterName == null) {
+ errorNode.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list.");
+ return;
+ }
int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
- for (String altName : TransformationsUtil.toAllGetterNames(fieldName, isBoolean)) {
+ for (String altName : toAllGetterNames(fieldNode, isBoolean)) {
switch (methodExists(altName, fieldNode, false)) {
case EXISTS_BY_LOMBOK:
return;
diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java
index ea81965b..810096ab 100644
--- a/src/core/lombok/eclipse/handlers/HandleSetter.java
+++ b/src/core/lombok/eclipse/handlers/HandleSetter.java
@@ -148,11 +148,15 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> {
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
TypeReference fieldType = copyType(field.type, source);
boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0;
- String setterName = TransformationsUtil.toSetterName(new String(field.name), isBoolean);
+ String setterName = toSetterName(fieldNode, isBoolean);
+ if (setterName == null) {
+ errorNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
+ return;
+ }
int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
- for (String altName : TransformationsUtil.toAllSetterNames(new String(field.name), isBoolean)) {
+ for (String altName : toAllSetterNames(fieldNode, isBoolean)) {
switch (methodExists(altName, fieldNode, false)) {
case EXISTS_BY_LOMBOK:
return;
diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java
index c286ed24..e49b0ccb 100644
--- a/src/core/lombok/javac/handlers/HandleGetter.java
+++ b/src/core/lombok/javac/handlers/HandleGetter.java
@@ -190,9 +190,14 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> {
}
}
- String methodName = toGetterName(fieldDecl);
+ String methodName = toGetterName(fieldNode);
- for (String altName : toAllGetterNames(fieldDecl)) {
+ if (methodName == null) {
+ source.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list.");
+ return;
+ }
+
+ for (String altName : toAllGetterNames(fieldNode)) {
switch (methodExists(altName, fieldNode, false)) {
case EXISTS_BY_LOMBOK:
return;
@@ -231,7 +236,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> {
}
JCBlock methodBody = treeMaker.Block(0, statements);
- Name methodName = field.toName(toGetterName(fieldNode));
+ Name methodName = field.toName(toGetterName(field));
List<JCTypeParameter> methodGenericParams = List.nil();
List<JCVariableDecl> parameters = List.nil();
diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java
index db1fa724..93ea5a5b 100644
--- a/src/core/lombok/javac/handlers/HandleSetter.java
+++ b/src/core/lombok/javac/handlers/HandleSetter.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 The Project Lombok Authors.
+ * Copyright (C) 2009-2012 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -162,9 +162,14 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
}
JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get();
- String methodName = toSetterName(fieldDecl);
+ String methodName = toSetterName(fieldNode);
- for (String altName : toAllSetterNames(fieldDecl)) {
+ if (methodName == null) {
+ source.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
+ return;
+ }
+
+ for (String altName : toAllSetterNames(fieldNode)) {
switch (methodExists(altName, fieldNode, false)) {
case EXISTS_BY_LOMBOK:
return;
@@ -184,10 +189,14 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
- injectMethod(fieldNode.up(), createSetter(access, fieldNode, fieldNode.getTreeMaker(), source.get()));
+ JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), source.get());
+ injectMethod(fieldNode.up(), createdSetter);
}
private JCMethodDecl createSetter(long access, JavacNode field, TreeMaker treeMaker, JCTree source) {
+ String setterName = toSetterName(field);
+ if (setterName == null) return null;
+
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
JCExpression fieldRef = createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD);
@@ -206,7 +215,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
}
JCBlock methodBody = treeMaker.Block(0, statements);
- Name methodName = field.toName(toSetterName(fieldDecl));
+ Name methodName = field.toName(setterName);
List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables);
JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
//WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6.
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index 244f7d38..245636b7 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 The Project Lombok Authors.
+ * Copyright (C) 2009-2012 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -37,9 +37,11 @@ import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.core.AnnotationValues;
+import lombok.core.TransformationsUtil;
import lombok.core.TypeResolver;
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues.AnnotationValue;
+import lombok.experimental.Accessors;
import lombok.javac.Javac;
import lombok.javac.JavacNode;
@@ -272,6 +274,84 @@ public class JavacHandlerUtil {
}
/**
+ * Translates the given field into all possible getter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static java.util.List<String> toAllGetterNames(JavacNode field) {
+ String fieldName = field.getName();
+
+ boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean");
+
+ AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static String toGetterName(JavacNode field) {
+ String fieldName = field.getName();
+
+ boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean");
+
+ AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toGetterName(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * Translates the given field into all possible setter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static java.util.List<String> toAllSetterNames(JavacNode field) {
+ String fieldName = field.getName();
+
+ boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean");
+
+ AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toAllSetterNames(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static String toSetterName(JavacNode field) {
+ String fieldName = field.getName();
+
+ boolean isBoolean = ((JCVariableDecl) field.get()).toString().equals("boolean");
+
+ AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean);
+ }
+
+ public static AnnotationValues<Accessors> getAccessorsForField(JavacNode field) {
+ for (JavacNode node : field.down()) {
+ if (annotationTypeMatches(Accessors.class, node)) {
+ return createAnnotation(Accessors.class, node);
+ }
+ }
+
+ JavacNode current = field.up();
+ while (current != null) {
+ for (JavacNode node : field.down()) {
+ if (annotationTypeMatches(Accessors.class, node)) {
+ return createAnnotation(Accessors.class, node);
+ }
+ }
+ current = field.up();
+ }
+
+ return AnnotationValues.of(Accessors.class, field);
+ }
+
+ /**
* Checks if there is a field with the provided name.
*
* @param fieldName the field name to check for.
@@ -382,7 +462,7 @@ public class JavacHandlerUtil {
private static GetterMethod findGetter(JavacNode field) {
JCVariableDecl decl = (JCVariableDecl)field.get();
JavacNode typeNode = field.up();
- for (String potentialGetterName : toAllGetterNames(decl)) {
+ for (String potentialGetterName : toAllGetterNames(field)) {
for (JavacNode potentialGetter : typeNode.down()) {
if (potentialGetter.getKind() != Kind.METHOD) continue;
JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
@@ -424,7 +504,8 @@ public class JavacHandlerUtil {
}
if (hasGetterAnnotation) {
- String getterName = toGetterName(decl);
+ String getterName = toGetterName(field);
+ if (getterName == null) return null;
return new GetterMethod(field.toName(getterName), decl.vartype);
}