aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
-rw-r--r--src/utils/lombok/core/TransformationsUtil.java186
-rw-r--r--src/utils/lombok/javac/Javac.java53
9 files changed, 453 insertions, 257 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);
}
diff --git a/src/utils/lombok/core/TransformationsUtil.java b/src/utils/lombok/core/TransformationsUtil.java
deleted file mode 100644
index 56ae0fb0..00000000
--- a/src/utils/lombok/core/TransformationsUtil.java
+++ /dev/null
@@ -1,186 +0,0 @@
-/*
- * Copyright (C) 2009 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;
-
-/**
- * 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
- }
-
- /**
- * Generates a getter name from a given field name.
- *
- * Strategy:
- *
- * First, pick a prefix. 'get' normally, but 'is' if {@code isBoolean} is true.
- *
- * Then, 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.
- *
- * return the prefix plus the possibly title/uppercased first character, and the rest of the field name.
- *
- * Note that for boolean fields, if the field starts with 'is', and the character after that is
- * <b>not</b> a lowercase character, the field name is returned without changing any character's case and without
- * any prefix.
- *
- * @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 String toGetterName(CharSequence fieldName, boolean isBoolean) {
- final String prefix = isBoolean ? "is" : "get";
-
- if (fieldName.length() == 0) return prefix;
-
- 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(prefix, fieldName.toString());
- }
-
- 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. */
- public static final Pattern NON_NULL_PATTERN = Pattern.compile("^(?:nonnull)$", Pattern.CASE_INSENSITIVE);
- public static final Pattern NULLABLE_PATTERN = Pattern.compile("^(?:nullable|checkfornull)$", Pattern.CASE_INSENSITIVE);
-
- /**
- * Generates a setter name from a given field name.
- *
- * Strategy:
- *
- * 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.
- *
- * return "set" plus the possibly title/uppercased first character, and the rest of the field name.
- *
- * Note that if the field is boolean and starts with 'is' followed by a non-lowercase letter, the 'is' is stripped and replaced with 'set'.
- *
- * @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 String toSetterName(CharSequence fieldName, boolean isBoolean) {
- if (fieldName.length() == 0) return "set";
-
- 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());
- }
-
- private static String buildName(String prefix, String suffix) {
- if (suffix.length() == 0) return prefix;
-
- 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);
- }
-
- /**
- * 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 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(CharSequence fieldName, boolean isBoolean) {
- if (!isBoolean) return Collections.singletonList(toGetterName(fieldName, false));
-
- List<String> baseNames = new ArrayList<String>();
- baseNames.add(fieldName.toString());
-
- // isPrefix = field is called something like 'isRunning', so 'running' could also be the fieldname.
- if (fieldName.toString().startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) {
- baseNames.add(fieldName.toString().substring(2));
- }
-
- Set<String> names = new HashSet<String>();
- for (String baseName : baseNames) {
- if (baseName.length() > 0 && Character.isLowerCase(baseName.charAt(0))) {
- baseName = Character.toTitleCase(baseName.charAt(0)) + baseName.substring(1);
- }
-
- names.add("is" + baseName);
- names.add("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(CharSequence fieldName, boolean isBoolean) {
- if (!isBoolean) return Collections.singletonList(toSetterName(fieldName, false));
-
- List<String> baseNames = new ArrayList<String>();
- baseNames.add(fieldName.toString());
-
- // isPrefix = field is called something like 'isRunning', so 'running' could also be the fieldname.
- if (fieldName.toString().startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) {
- baseNames.add(fieldName.toString().substring(2));
- }
-
- Set<String> names = new HashSet<String>();
- for (String baseName : baseNames) {
- if (baseName.length() > 0 && Character.isLowerCase(baseName.charAt(0))) {
- baseName = Character.toTitleCase(baseName.charAt(0)) + baseName.substring(1);
- }
-
- names.add("set" + baseName);
- }
-
- return new ArrayList<String>(names);
- }
-}
diff --git a/src/utils/lombok/javac/Javac.java b/src/utils/lombok/javac/Javac.java
index fb24df93..d97746f1 100644
--- a/src/utils/lombok/javac/Javac.java
+++ b/src/utils/lombok/javac/Javac.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
@@ -27,7 +27,6 @@ import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCLiteral;
-import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
/**
* Container for static utility methods relevant to lombok's operation on javac.
@@ -38,56 +37,6 @@ public class Javac {
}
/**
- * Translates the given field into all possible getter names.
- * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(CharSequence, boolean)}.
- */
- public static java.util.List<String> toAllGetterNames(JCVariableDecl field) {
- CharSequence fieldName = field.name;
-
- boolean isBoolean = field.vartype.toString().equals("boolean");
-
- return TransformationsUtil.toAllGetterNames(fieldName, isBoolean);
- }
-
- /**
- * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo).
- *
- * Convenient wrapper around {@link TransformationsUtil#toGetterName(CharSequence, boolean)}.
- */
- public static String toGetterName(JCVariableDecl field) {
- CharSequence fieldName = field.name;
-
- boolean isBoolean = field.vartype.toString().equals("boolean");
-
- 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<String> 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).
- *
- * Convenient wrapper around {@link TransformationsUtil#toSetterName(CharSequence, boolean)}.
- */
- public static String toSetterName(JCVariableDecl field) {
- CharSequence fieldName = field.name;
-
- boolean isBoolean = field.vartype.toString().equals("boolean");
-
- return TransformationsUtil.toSetterName(fieldName, isBoolean);
- }
-
- /**
* Checks if the given expression (that really ought to refer to a type expression) represents a primitive type.
*/
public static boolean isPrimitive(JCExpression ref) {