diff options
-rw-r--r-- | src/core/lombok/ConfigurationKeys.java | 23 | ||||
-rw-r--r-- | src/core/lombok/core/TransformationsUtil.java | 306 | ||||
-rw-r--r-- | src/core/lombok/core/handlers/HandlerUtil.java | 319 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 36 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleBuilder.java | 3 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleConstructor.java | 9 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleGetter.java | 5 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleSetter.java | 5 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleWither.java | 5 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleBuilder.java | 3 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleConstructor.java | 11 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleGetter.java | 5 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSetter.java | 5 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleWither.java | 5 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/JavacHandlerUtil.java | 38 |
15 files changed, 404 insertions, 374 deletions
diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index 021ca5e6..3e8de5a9 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -21,6 +21,8 @@ */ package lombok; +import java.util.List; + import lombok.core.FlagUsageType; import lombok.core.configuration.ConfigurationKey; @@ -262,6 +264,27 @@ public class ConfigurationKeys { */ public static final ConfigurationKey<FlagUsageType> ACCESSORS_FLAG_USAGE = new ConfigurationKey<FlagUsageType>("lombok.Accessors.flagUsage") {}; + /** + * lombok configuration: {@code lombok.Accessors.prefix} += <String: prefix>. + * + * For any class without an {@code @Accessors} that explicitly defines the {@code prefix} option, this list of prefixes is used. + */ + public static final ConfigurationKey<List<String>> ACCESSORS_PREFIX = new ConfigurationKey<List<String>>("lombok.Accessors.prefix") {}; + + /** + * lombok configuration: {@code lombok.Accessors.chain} += {@code true} | {@code false}. + * + * For any class without an {@code @Accessors} that explicitly defines the {@code chain} option, this value is used. + */ + public static final ConfigurationKey<Boolean> ACCESSORS_CHAIN = new ConfigurationKey<Boolean>("lombok.Accessors.chain") {}; + + /** + * lombok configuration: {@code lombok.Accessors.fluent} += {@code true} | {@code false}. + * + * For any class without an {@code @Accessors} that explicitly defines the {@code fluent} option, this value is used. + */ + public static final ConfigurationKey<Boolean> ACCESSORS_FLUENT = new ConfigurationKey<Boolean>("lombok.Accessors.fluent") {}; + // ----- Builder ----- /** diff --git a/src/core/lombok/core/TransformationsUtil.java b/src/core/lombok/core/TransformationsUtil.java deleted file mode 100644 index 8c7fbd3f..00000000 --- a/src/core/lombok/core/TransformationsUtil.java +++ /dev/null @@ -1,306 +0,0 @@ -/* - * 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.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.regex.Pattern; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.RequiredArgsConstructor; -import lombok.Setter; -import lombok.ToString; -import lombok.Value; -import lombok.experimental.Accessors; -import lombok.experimental.FieldDefaults; -import lombok.experimental.Wither; - -/** - * 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 - } - - @SuppressWarnings({"all", "unchecked", "deprecation"}) - public static final List<Class<? extends java.lang.annotation.Annotation>> INVALID_ON_BUILDERS = Collections.unmodifiableList( - Arrays.<Class<? extends java.lang.annotation.Annotation>>asList( - Getter.class, Setter.class, Wither.class, ToString.class, EqualsAndHashCode.class, - RequiredArgsConstructor.class, AllArgsConstructor.class, NoArgsConstructor.class, - Data.class, Value.class, lombok.experimental.Value.class, FieldDefaults.class)); - - /** - * 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. - */ - public static CharSequence removePrefix(CharSequence fieldName, String[] prefixes) { - if (prefixes == null || prefixes.length == 0) return fieldName; - - fieldName = fieldName.toString(); - - 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; - } - - /* 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 {@code 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) { - return toAccessorName(accessors, fieldName, isBoolean, "is", "get", true); - } - - /** - * 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 {@code 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) { - return toAccessorName(accessors, fieldName, isBoolean, "set", "set", true); - } - - /** - * Generates a wither 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 {@code null}.</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 with} 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 "with"} 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 wither 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 toWitherName(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { - return toAccessorName(accessors, fieldName, isBoolean, "with", "with", false); - } - - private static String toAccessorName(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean, - String booleanPrefix, String normalPrefix, boolean adhereToFluent) { - - fieldName = fieldName.toString(); - if (fieldName.length() == 0) return null; - - Accessors ac = accessors == null ? null : accessors.getInstance(); - fieldName = removePrefix(fieldName, ac == null ? new String[0] : ac.prefix()); - if (fieldName == null) return null; - - String fName = fieldName.toString(); - if (adhereToFluent && ac != null && ac.fluent()) return fName; - - if (isBoolean && fName.startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) { - // The field is for example named 'isRunning'. - return booleanPrefix + fName.substring(2); - } - - return buildName(isBoolean ? booleanPrefix : normalPrefix, fName); - } - - /** - * 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) { - return toAllAccessorNames(accessors, fieldName, isBoolean, "is", "get", true); - } - - /** - * 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 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> toAllSetterNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { - return toAllAccessorNames(accessors, fieldName, isBoolean, "set", "set", true); - } - - /** - * Returns all names of methods that would represent the wither for a field with the provided name. - * - * For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br /> - * {@code [withRunning, withIsRunning]} - * - * @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> toAllWitherNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { - return toAllAccessorNames(accessors, fieldName, isBoolean, "with", "with", false); - } - - private static List<String> toAllAccessorNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean, - String booleanPrefix, String normalPrefix, boolean adhereToFluent) { - - if (!isBoolean) { - String accessorName = toAccessorName(accessors, fieldName, false, booleanPrefix, normalPrefix, adhereToFluent); - return (accessorName == null) ? Collections.<String>emptyList() : Collections.singletonList(accessorName); - } - - 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 (adhereToFluent && acc.fluent()) { - names.add(baseName); - } else { - names.add(buildName(normalPrefix, baseName)); - if (!normalPrefix.equals(booleanPrefix)) names.add(buildName(booleanPrefix, 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/core/handlers/HandlerUtil.java b/src/core/lombok/core/handlers/HandlerUtil.java index 65e1180e..ae651de1 100644 --- a/src/core/lombok/core/handlers/HandlerUtil.java +++ b/src/core/lombok/core/handlers/HandlerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Project Lombok Authors. + * Copyright (C) 2013-2014 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 @@ -21,12 +21,38 @@ */ package lombok.core.handlers; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; + +import lombok.AllArgsConstructor; import lombok.ConfigurationKeys; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import lombok.ToString; +import lombok.Value; +import lombok.core.AST; +import lombok.core.AnnotationValues; import lombok.core.FlagUsageType; import lombok.core.JavaIdentifiers; import lombok.core.LombokNode; import lombok.core.configuration.ConfigurationKey; +import lombok.experimental.Accessors; +import lombok.experimental.FieldDefaults; +import lombok.experimental.Wither; +/** + * Container for static utility methods useful for some of the standard lombok handlers, regardless of + * target platform (e.g. useful for both javac and Eclipse lombok implementations). + */ public class HandlerUtil { private HandlerUtil() {} @@ -89,4 +115,295 @@ public class HandlerUtil { else node.addError(msg); } } + + public static boolean shouldReturnThis0(AnnotationValues<Accessors> accessors, AST<?, ?, ?> ast) { + boolean chainForced = accessors.isExplicit("chain"); + boolean fluentForced = accessors.isExplicit("fluent"); + Accessors instance = accessors.getInstance(); + + boolean chain = instance.chain(); + boolean fluent = instance.fluent(); + + if (chainForced) return chain; + + if (!chainForced) { + Boolean chainConfig = ast.readConfiguration(ConfigurationKeys.ACCESSORS_CHAIN); + if (chainConfig != null) return chainConfig; + } + + if (!fluentForced) { + Boolean fluentConfig = ast.readConfiguration(ConfigurationKeys.ACCESSORS_FLUENT); + if (fluentConfig != null) fluent = fluentConfig; + } + + return chain || fluent; + } + + @SuppressWarnings({"all", "unchecked", "deprecation"}) + public static final List<Class<? extends java.lang.annotation.Annotation>> INVALID_ON_BUILDERS = Collections.unmodifiableList( + Arrays.<Class<? extends java.lang.annotation.Annotation>>asList( + Getter.class, Setter.class, Wither.class, ToString.class, EqualsAndHashCode.class, + RequiredArgsConstructor.class, AllArgsConstructor.class, NoArgsConstructor.class, + Data.class, Value.class, lombok.experimental.Value.class, FieldDefaults.class)); + + /** + * 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. + */ + public static CharSequence removePrefix(CharSequence fieldName, List<String> prefixes) { + if (prefixes == null || prefixes.isEmpty()) return fieldName; + + fieldName = fieldName.toString(); + + 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; + } + + /* 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 {@code 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(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { + return toAccessorName(ast, accessors, fieldName, isBoolean, "is", "get", true); + } + + /** + * 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 {@code 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(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { + return toAccessorName(ast, accessors, fieldName, isBoolean, "set", "set", true); + } + + /** + * Generates a wither 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 {@code null}.</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 with} 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 "with"} 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 wither 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 toWitherName(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { + return toAccessorName(ast, accessors, fieldName, isBoolean, "with", "with", false); + } + + private static String toAccessorName(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean, + String booleanPrefix, String normalPrefix, boolean adhereToFluent) { + + fieldName = fieldName.toString(); + if (fieldName.length() == 0) return null; + + boolean explicitPrefix = accessors != null && accessors.isExplicit("prefix"); +// System.out.printf("accessors: %s actual expr: %s val: %s\n", accessors, accessors != null ? accessors.getActualExpression("prefix") : "(null)", accessors == null ? "(null)" : Arrays.toString(accessors.getInstance().prefix())); + boolean explicitFluent = accessors != null && accessors.isExplicit("fluent"); + + Accessors ac = (explicitPrefix || explicitFluent) ? accessors.getInstance() : null; + + List<String> prefix = explicitPrefix ? Arrays.asList(ac.prefix()) : ast.readConfiguration(ConfigurationKeys.ACCESSORS_PREFIX); + boolean fluent = explicitFluent ? ac.fluent() : Boolean.TRUE.equals(ast.readConfiguration(ConfigurationKeys.ACCESSORS_FLUENT)); + + fieldName = removePrefix(fieldName, prefix); + if (fieldName == null) return null; + + String fName = fieldName.toString(); + if (adhereToFluent && fluent) return fName; + + if (isBoolean && fName.startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) { + // The field is for example named 'isRunning'. + return booleanPrefix + fName.substring(2); + } + + return buildName(isBoolean ? booleanPrefix : normalPrefix, fName); + } + + /** + * 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(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { + return toAllAccessorNames(ast, accessors, fieldName, isBoolean, "is", "get", true); + } + + /** + * 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 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> toAllSetterNames(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { + return toAllAccessorNames(ast, accessors, fieldName, isBoolean, "set", "set", true); + } + + /** + * Returns all names of methods that would represent the wither for a field with the provided name. + * + * For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br /> + * {@code [withRunning, withIsRunning]} + * + * @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> toAllWitherNames(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) { + return toAllAccessorNames(ast, accessors, fieldName, isBoolean, "with", "with", false); + } + + private static List<String> toAllAccessorNames(AST<?, ?, ?> ast, AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean, + String booleanPrefix, String normalPrefix, boolean adhereToFluent) { + + if (!isBoolean) { + String accessorName = toAccessorName(ast, accessors, fieldName, false, booleanPrefix, normalPrefix, adhereToFluent); + return (accessorName == null) ? Collections.<String>emptyList() : Collections.singletonList(accessorName); + } + + boolean explicitPrefix = accessors != null && accessors.isExplicit("prefix"); + boolean explicitFluent = accessors != null && accessors.isExplicit("fluent"); + + Accessors ac = (explicitPrefix || explicitFluent) ? accessors.getInstance() : null; + + List<String> prefix = explicitPrefix ? Arrays.asList(ac.prefix()) : ast.readConfiguration(ConfigurationKeys.ACCESSORS_PREFIX); + boolean fluent = explicitFluent ? ac.fluent() : Boolean.TRUE.equals(ast.readConfiguration(ConfigurationKeys.ACCESSORS_FLUENT)); + + fieldName = removePrefix(fieldName, prefix); + if (fieldName == null) return Collections.emptyList(); + + List<String> baseNames = toBaseNames(fieldName, isBoolean, fluent); + + Set<String> names = new HashSet<String>(); + for (String baseName : baseNames) { + if (adhereToFluent && fluent) { + names.add(baseName); + } else { + names.add(buildName(normalPrefix, baseName)); + if (!normalPrefix.equals(booleanPrefix)) names.add(buildName(booleanPrefix, 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 5e322c90..80c90b65 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -22,7 +22,7 @@ package lombok.eclipse.handlers; import static lombok.eclipse.Eclipse.*; -import static lombok.core.TransformationsUtil.*; +import static lombok.core.handlers.HandlerUtil.*; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -38,14 +38,15 @@ import java.util.Map; import java.util.WeakHashMap; import lombok.AccessLevel; +import lombok.ConfigurationKeys; import lombok.Data; import lombok.Getter; import lombok.Lombok; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.AnnotationValues.AnnotationValue; -import lombok.core.TransformationsUtil; import lombok.core.TypeResolver; +import lombok.core.handlers.HandlerUtil; import lombok.eclipse.EclipseAST; import lombok.eclipse.EclipseNode; import lombok.experimental.Accessors; @@ -1043,7 +1044,7 @@ public class EclipseHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static List<String> toAllGetterNames(EclipseNode field, boolean isBoolean) { - return TransformationsUtil.toAllGetterNames(getAccessorsForField(field), field.getName(), isBoolean); + return HandlerUtil.toAllGetterNames(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean); } /** @@ -1052,7 +1053,7 @@ public class EclipseHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static String toGetterName(EclipseNode field, boolean isBoolean) { - return TransformationsUtil.toGetterName(getAccessorsForField(field), field.getName(), isBoolean); + return HandlerUtil.toGetterName(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean); } /** @@ -1060,7 +1061,7 @@ public class EclipseHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static java.util.List<String> toAllSetterNames(EclipseNode field, boolean isBoolean) { - return TransformationsUtil.toAllSetterNames(getAccessorsForField(field), field.getName(), isBoolean); + return HandlerUtil.toAllSetterNames(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean); } /** @@ -1069,7 +1070,7 @@ public class EclipseHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static String toSetterName(EclipseNode field, boolean isBoolean) { - return TransformationsUtil.toSetterName(getAccessorsForField(field), field.getName(), isBoolean); + return HandlerUtil.toSetterName(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean); } /** @@ -1077,7 +1078,7 @@ public class EclipseHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toAllWitherNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static java.util.List<String> toAllWitherNames(EclipseNode field, boolean isBoolean) { - return TransformationsUtil.toAllWitherNames(getAccessorsForField(field), field.getName(), isBoolean); + return HandlerUtil.toAllWitherNames(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean); } /** @@ -1086,19 +1087,17 @@ public class EclipseHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toWitherName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static String toWitherName(EclipseNode field, boolean isBoolean) { - return TransformationsUtil.toWitherName(getAccessorsForField(field), field.getName(), isBoolean); + return HandlerUtil.toWitherName(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean); } /** * When generating a setter, the setter either returns void (beanspec) or Self (fluent). - * This method scans for the {@code Accessors} annotation to figure that out. + * This method scans for the {@code Accessors} annotation and associated config properties to figure that out. */ public static boolean shouldReturnThis(EclipseNode field) { if ((((FieldDeclaration) field.get()).modifiers & ClassFileConstants.AccStatic) != 0) return false; AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field); - boolean forced = (accessors.getActualExpression("chain") != null); - Accessors instance = accessors.getInstance(); - return instance.chain() || (instance.fluent() && !forced); + return shouldReturnThis0(accessors, field.getAst()); } /** @@ -1126,10 +1125,11 @@ public class EclipseHandlerUtil { } public static char[] removePrefixFromField(EclipseNode field) { - String[] prefixes = null; + List<String> prefixes = null; for (EclipseNode node : field.down()) { if (annotationTypeMatches(Accessors.class, node)) { - prefixes = createAnnotation(Accessors.class, node).getInstance().prefix(); + AnnotationValues<Accessors> ann = createAnnotation(Accessors.class, node); + if (ann.isExplicit("prefix")) prefixes = Arrays.asList(ann.getInstance().prefix()); break; } } @@ -1140,7 +1140,8 @@ public class EclipseHandlerUtil { while (current != null) { for (EclipseNode node : current.down()) { if (annotationTypeMatches(Accessors.class, node)) { - prefixes = createAnnotation(Accessors.class, node).getInstance().prefix(); + AnnotationValues<Accessors> ann = createAnnotation(Accessors.class, node); + if (ann.isExplicit("prefix")) prefixes = Arrays.asList(ann.getInstance().prefix()); break outer; } } @@ -1148,8 +1149,9 @@ public class EclipseHandlerUtil { } } - if (prefixes != null && prefixes.length > 0) { - CharSequence newName = TransformationsUtil.removePrefix(field.getName(), prefixes); + if (prefixes == null) prefixes = field.getAst().readConfiguration(ConfigurationKeys.ACCESSORS_PREFIX); + if (!prefixes.isEmpty()) { + CharSequence newName = removePrefix(field.getName(), prefixes); if (newName != null) return newName.toString().toCharArray(); } diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 9cf3291f..fd7923b1 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -60,7 +60,6 @@ import lombok.ConfigurationKeys; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.HandlerPriority; -import lombok.core.TransformationsUtil; import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; @@ -350,7 +349,7 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { } boolean isBoolean = isBoolean(fd.type); - String setterName = fluent ? fieldNode.getName() : TransformationsUtil.toSetterName(null, fieldNode.getName(), isBoolean); + String setterName = fluent ? fieldNode.getName() : toSetterName(builderType.getAst(), null, fieldNode.getName(), isBoolean); return HandleSetter.createSetter(td, fieldNode, setterName, chain, ClassFileConstants.AccPublic, source, Collections.<Annotation>emptyList(), Collections.<Annotation>emptyList()); diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java index 15c4d534..ec2bf5dc 100644 --- a/src/core/lombok/eclipse/handlers/HandleConstructor.java +++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java @@ -38,7 +38,6 @@ import lombok.NoArgsConstructor; import lombok.RequiredArgsConstructor; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; -import lombok.core.TransformationsUtil; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; import lombok.experimental.Builder; @@ -114,7 +113,7 @@ public class HandleConstructor { FieldDeclaration fieldDecl = (FieldDeclaration) child.get(); if (!filterField(fieldDecl)) continue; boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0; - boolean isNonNull = findAnnotations(fieldDecl, TransformationsUtil.NON_NULL_PATTERN).length != 0; + boolean isNonNull = findAnnotations(fieldDecl, NON_NULL_PATTERN).length != 0; if ((isFinal || isNonNull) && fieldDecl.initialization == null) fields.add(child); } return fields; @@ -293,8 +292,8 @@ public class HandleConstructor { assigns.add(assignment); long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd; Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source), Modifier.FINAL); - Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); - Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); if (nonNulls.length != 0) { Statement nullCheck = generateNullCheck(field, source); if (nullCheck != null) nullChecks.add(nullCheck); @@ -363,7 +362,7 @@ public class HandleConstructor { Argument parameter = new Argument(field.name, fieldPos, copyType(field.type, source), Modifier.FINAL); - Annotation[] copiedAnnotations = copyAnnotations(source, findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN)); + Annotation[] copiedAnnotations = copyAnnotations(source, findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN)); if (copiedAnnotations.length != 0) parameter.annotations = copiedAnnotations; params.add(parameter); } diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 331cf097..0ad20717 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -38,7 +38,6 @@ import lombok.Delegate; import lombok.Getter; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; -import lombok.core.TransformationsUtil; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; import lombok.eclipse.agent.PatchDelegate; @@ -273,8 +272,8 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { Annotation[] copiedAnnotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), - findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), - findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), + findAnnotations(field, NON_NULL_PATTERN), + findAnnotations(field, NULLABLE_PATTERN), findDelegatesAndMarkAsHandled(fieldNode), deprecated); diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index 42f0cd3f..01d1a7ac 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -36,7 +36,6 @@ import lombok.ConfigurationKeys; import lombok.Setter; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; -import lombok.core.TransformationsUtil; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; import lombok.eclipse.handlers.EclipseHandlerUtil.FieldAccess; @@ -234,8 +233,8 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; - Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); - Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); List<Statement> statements = new ArrayList<Statement>(5); if (nonNulls.length == 0) { statements.add(assignment); diff --git a/src/core/lombok/eclipse/handlers/HandleWither.java b/src/core/lombok/eclipse/handlers/HandleWither.java index 6149bb08..afe573b9 100644 --- a/src/core/lombok/eclipse/handlers/HandleWither.java +++ b/src/core/lombok/eclipse/handlers/HandleWither.java @@ -35,7 +35,6 @@ import lombok.AccessLevel; import lombok.ConfigurationKeys; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; -import lombok.core.TransformationsUtil; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; import lombok.eclipse.handlers.EclipseHandlerUtil.FieldAccess; @@ -270,8 +269,8 @@ public class HandleWither extends EclipseAnnotationHandler<Wither> { method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; - Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); - Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); List<Statement> statements = new ArrayList<Statement>(5); if (nonNulls.length > 0) { Statement nullCheck = generateNullCheck(field, source); diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 59ca5d35..fc578f0f 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -50,7 +50,6 @@ import lombok.ConfigurationKeys; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.HandlerPriority; -import lombok.core.TransformationsUtil; import lombok.experimental.Builder; import lombok.experimental.NonFinal; import lombok.javac.JavacAnnotationHandler; @@ -310,7 +309,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { } boolean isBoolean = isBoolean(fieldNode); - String setterName = fluent ? fieldNode.getName() : TransformationsUtil.toSetterName(null, fieldNode.getName(), isBoolean); + String setterName = fluent ? fieldNode.getName() : toSetterName(builderType.getAst(), null, fieldNode.getName(), isBoolean); JavacTreeMaker maker = builderType.getTreeMaker(); return HandleSetter.createSetter(Flags.PUBLIC, fieldNode, maker, setterName, chain, source, List.<JCAnnotation>nil(), List.<JCAnnotation>nil()); diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java index 7b927a1d..8fc01ffe 100644 --- a/src/core/lombok/javac/handlers/HandleConstructor.java +++ b/src/core/lombok/javac/handlers/HandleConstructor.java @@ -29,7 +29,6 @@ import lombok.ConfigurationKeys; import lombok.NoArgsConstructor; import lombok.RequiredArgsConstructor; import lombok.core.AnnotationValues; -import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.delombok.LombokOptionsFactory; import lombok.experimental.Builder; @@ -108,7 +107,7 @@ public class HandleConstructor { //Skip static fields. if ((fieldFlags & Flags.STATIC) != 0) continue; boolean isFinal = (fieldFlags & Flags.FINAL) != 0; - boolean isNonNull = !findAnnotations(child, TransformationsUtil.NON_NULL_PATTERN).isEmpty(); + boolean isNonNull = !findAnnotations(child, NON_NULL_PATTERN).isEmpty(); if ((isFinal || isNonNull) && fieldDecl.init == null) fields.append(child); } return fields.toList(); @@ -242,8 +241,8 @@ public class HandleConstructor { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); Name fieldName = removePrefixFromField(fieldNode); Name rawName = field.name; - List<JCAnnotation> nonNulls = findAnnotations(fieldNode, TransformationsUtil.NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(fieldNode, TransformationsUtil.NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, field.vartype, null); params.append(param); @@ -305,8 +304,8 @@ public class HandleConstructor { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); Name fieldName = removePrefixFromField(fieldNode); JCExpression pType = cloneType(maker, field.vartype, source, typeNode.getContext()); - List<JCAnnotation> nonNulls = findAnnotations(fieldNode, TransformationsUtil.NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(fieldNode, TransformationsUtil.NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, pType, null); params.append(param); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 50d8ae97..4414ae24 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -37,7 +37,6 @@ import lombok.Delegate; import lombok.Getter; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; -import lombok.core.TransformationsUtil; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; import lombok.javac.JavacTreeMaker; @@ -243,8 +242,8 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { List<JCExpression> throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); List<JCAnnotation> delegates = findDelegatesAndRemoveFromField(field); diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 5539d932..d73dce7c 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -32,7 +32,6 @@ import lombok.ConfigurationKeys; import lombok.Setter; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; -import lombok.core.TransformationsUtil; import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; @@ -209,8 +208,8 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { JCAssign assign = treeMaker.Assign(fieldRef, treeMaker.Ident(fieldDecl.name)); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); - List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); Name methodName = field.toName(setterName); List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables); diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java index be49c824..746f0a2e 100644 --- a/src/core/lombok/javac/handlers/HandleWither.java +++ b/src/core/lombok/javac/handlers/HandleWither.java @@ -31,7 +31,6 @@ import lombok.AccessLevel; import lombok.ConfigurationKeys; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; -import lombok.core.TransformationsUtil; import lombok.experimental.Wither; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; @@ -212,8 +211,8 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); - List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); Name methodName = field.toName(witherName); List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index adfac6e2..2d859103 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2013 The Project Lombok Authors. + * Copyright (C) 2009-2014 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 @@ -21,7 +21,7 @@ */ package lombok.javac.handlers; -import static lombok.core.TransformationsUtil.INVALID_ON_BUILDERS; +import static lombok.core.handlers.HandlerUtil.*; import static lombok.javac.Javac.*; import java.lang.annotation.Annotation; @@ -29,6 +29,7 @@ import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.WeakHashMap; @@ -36,12 +37,13 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.AccessLevel; +import lombok.ConfigurationKeys; import lombok.Data; import lombok.Getter; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.AnnotationValues.AnnotationValue; -import lombok.core.TransformationsUtil; +import lombok.core.handlers.HandlerUtil; import lombok.core.TypeResolver; import lombok.delombok.LombokOptionsFactory; import lombok.experimental.Accessors; @@ -391,7 +393,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static java.util.List<String> toAllGetterNames(JavacNode field) { - return TransformationsUtil.toAllGetterNames(getAccessorsForField(field), field.getName(), isBoolean(field)); + return HandlerUtil.toAllGetterNames(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -400,7 +402,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static String toGetterName(JavacNode field) { - return TransformationsUtil.toGetterName(getAccessorsForField(field), field.getName(), isBoolean(field)); + return HandlerUtil.toGetterName(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -408,7 +410,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static java.util.List<String> toAllSetterNames(JavacNode field) { - return TransformationsUtil.toAllSetterNames(getAccessorsForField(field), field.getName(), isBoolean(field)); + return HandlerUtil.toAllSetterNames(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -417,7 +419,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static String toSetterName(JavacNode field) { - return TransformationsUtil.toSetterName(getAccessorsForField(field), field.getName(), isBoolean(field)); + return HandlerUtil.toSetterName(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -425,7 +427,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toAllWitherNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static java.util.List<String> toAllWitherNames(JavacNode field) { - return TransformationsUtil.toAllWitherNames(getAccessorsForField(field), field.getName(), isBoolean(field)); + return HandlerUtil.toAllWitherNames(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -434,7 +436,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toWitherName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static String toWitherName(JavacNode field) { - return TransformationsUtil.toWitherName(getAccessorsForField(field), field.getName(), isBoolean(field)); + return HandlerUtil.toWitherName(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -446,9 +448,7 @@ public class JavacHandlerUtil { AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); - boolean forced = (accessors.getActualExpression("chain") != null); - Accessors instance = accessors.getInstance(); - return instance.chain() || (instance.fluent() && !forced); + return HandlerUtil.shouldReturnThis0(accessors, field.getAst()); } public static JCExpression cloneSelfType(JavacNode field) { @@ -481,10 +481,11 @@ public class JavacHandlerUtil { } public static Name removePrefixFromField(JavacNode field) { - String[] prefixes = null; + java.util.List<String> prefixes = null; for (JavacNode node : field.down()) { if (annotationTypeMatches(Accessors.class, node)) { - prefixes = createAnnotation(Accessors.class, node).getInstance().prefix(); + AnnotationValues<Accessors> ann = createAnnotation(Accessors.class, node); + if (ann.isExplicit("prefix")) prefixes = Arrays.asList(ann.getInstance().prefix()); break; } } @@ -495,7 +496,8 @@ public class JavacHandlerUtil { while (current != null) { for (JavacNode node : current.down()) { if (annotationTypeMatches(Accessors.class, node)) { - prefixes = createAnnotation(Accessors.class, node).getInstance().prefix(); + AnnotationValues<Accessors> ann = createAnnotation(Accessors.class, node); + if (ann.isExplicit("prefix")) prefixes = Arrays.asList(ann.getInstance().prefix()); break outer; } } @@ -503,8 +505,10 @@ public class JavacHandlerUtil { } } - if (prefixes != null && prefixes.length > 0) { - CharSequence newName = TransformationsUtil.removePrefix(field.getName(), prefixes); + if (prefixes == null) prefixes = field.getAst().readConfiguration(ConfigurationKeys.ACCESSORS_PREFIX); + + if (!prefixes.isEmpty()) { + CharSequence newName = removePrefix(field.getName(), prefixes); if (newName != null) return field.toName(newName.toString()); } |