From 710a04607b2c8c875d1971dd517f9eace10a14b4 Mon Sep 17 00:00:00 2001 From: Bulgakov Alexander Date: Wed, 26 Oct 2016 23:18:47 +0300 Subject: The @var annotation has been moved to the experimental package. Added a test of a @var variable with null initialization. --- test/transform/resource/before/VarNullInit.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 test/transform/resource/before/VarNullInit.java (limited to 'test/transform/resource/before/VarNullInit.java') diff --git a/test/transform/resource/before/VarNullInit.java b/test/transform/resource/before/VarNullInit.java new file mode 100644 index 00000000..14d7b4a3 --- /dev/null +++ b/test/transform/resource/before/VarNullInit.java @@ -0,0 +1,7 @@ +import lombok.experimental.var; + +class VarNullInit { + void method() { + var x = null; + } +} \ No newline at end of file -- cgit From 62672c03855f6035a01c2b84fe783be0f6231899 Mon Sep 17 00:00:00 2001 From: Bulgakov Alexander Date: Thu, 27 Oct 2016 23:52:50 +0300 Subject: Use of the 'var' feature is disabled by default. Include lombok.var.flagUsage = ALLOW in lombok.config to enable var --- .../lombok/core/configuration/AllowHelper.java | 37 ++++++++++ .../lombok/core/configuration/FlagUsageType.java | 2 +- src/core/lombok/core/handlers/HandlerUtil.java | 86 +++++++++++----------- test/transform/resource/after-ecj/VarNullInit.java | 4 +- test/transform/resource/before/VarComplex.java | 1 + test/transform/resource/before/VarInFor.java | 1 + test/transform/resource/before/VarInForOld.java | 1 + test/transform/resource/before/VarModifier.java | 1 + test/transform/resource/before/VarNullInit.java | 1 + 9 files changed, 87 insertions(+), 47 deletions(-) create mode 100644 src/core/lombok/core/configuration/AllowHelper.java (limited to 'test/transform/resource/before/VarNullInit.java') diff --git a/src/core/lombok/core/configuration/AllowHelper.java b/src/core/lombok/core/configuration/AllowHelper.java new file mode 100644 index 00000000..31d09381 --- /dev/null +++ b/src/core/lombok/core/configuration/AllowHelper.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 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 + * 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.configuration; + +import java.util.HashSet; +import java.util.Set; + +import static java.util.Arrays.asList; + +public class AllowHelper { + private final static Set allowable = new HashSet(asList( + "var" + )); + + public static boolean isAllowable(String feature) { + return allowable.contains(feature); + } +} diff --git a/src/core/lombok/core/configuration/FlagUsageType.java b/src/core/lombok/core/configuration/FlagUsageType.java index b7053b7c..8717c22b 100644 --- a/src/core/lombok/core/configuration/FlagUsageType.java +++ b/src/core/lombok/core/configuration/FlagUsageType.java @@ -23,5 +23,5 @@ package lombok.core.configuration; /** Used for lombok configuration to flag usages of certain lombok feature. */ public enum FlagUsageType { - WARNING, ERROR; + WARNING, ERROR, ALLOW; } diff --git a/src/core/lombok/core/handlers/HandlerUtil.java b/src/core/lombok/core/handlers/HandlerUtil.java index a9a4be78..cdca991a 100644 --- a/src/core/lombok/core/handlers/HandlerUtil.java +++ b/src/core/lombok/core/handlers/HandlerUtil.java @@ -21,34 +21,25 @@ */ 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.*; import lombok.core.AST; import lombok.core.AnnotationValues; import lombok.core.JavaIdentifiers; import lombok.core.LombokNode; +import lombok.core.configuration.AllowHelper; import lombok.core.configuration.ConfigurationKey; import lombok.core.configuration.FlagUsageType; import lombok.experimental.Accessors; import lombok.experimental.FieldDefaults; import lombok.experimental.Wither; +import java.util.*; +import java.util.regex.Pattern; + +import static lombok.core.configuration.FlagUsageType.ALLOW; +import static lombok.core.configuration.FlagUsageType.ERROR; +import static lombok.core.configuration.FlagUsageType.WARNING; + /** * 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). @@ -73,7 +64,7 @@ public class HandlerUtil { } /** Checks if the given name is a valid identifier. - * + * * If it is, this returns {@code true} and does nothing else. * If it isn't, this returns {@code false} and adds an error message to the supplied node. */ @@ -95,12 +86,19 @@ public class HandlerUtil { return Singulars.autoSingularize(plural); } public static void handleFlagUsage(LombokNode node, ConfigurationKey key, String featureName) { + boolean allowable = AllowHelper.isAllowable(featureName); + FlagUsageType fut = node.getAst().readConfiguration(key); + boolean allowed = !allowable || ALLOW == fut; + if (!allowed) { + node.addError("Use of " + featureName + " is disabled by default. Please use flag " + ALLOW + " to enable."); + } + if (fut != null) { String msg = "Use of " + featureName + " is flagged according to lombok configuration."; - if (fut == FlagUsageType.WARNING) node.addWarning(msg); - else node.addError(msg); + if (fut == WARNING) node.addWarning(msg); + else if (fut == ERROR) node.addError(msg); } } @@ -114,13 +112,13 @@ public class HandlerUtil { FlagUsageType fut = null; String featureName = null; - if (fut1 == FlagUsageType.ERROR) { + if (fut1 == ERROR) { fut = fut1; featureName = featureName1; - } else if (fut2 == FlagUsageType.ERROR) { + } else if (fut2 == ERROR) { fut = fut2; featureName = featureName2; - } else if (fut1 == FlagUsageType.WARNING) { + } else if (fut1 == WARNING) { fut = fut1; featureName = featureName1; } else { @@ -130,7 +128,7 @@ public class HandlerUtil { if (fut != null) { String msg = "Use of " + featureName + " is flagged according to lombok configuration."; - if (fut == FlagUsageType.WARNING) node.addWarning(msg); + if (fut == WARNING) node.addWarning(msg); else node.addError(msg); } } @@ -161,8 +159,8 @@ public class HandlerUtil { @SuppressWarnings({"all", "unchecked", "deprecation"}) public static final List> INVALID_ON_BUILDERS = Collections.unmodifiableList( Arrays.>asList( - Getter.class, Setter.class, Wither.class, ToString.class, EqualsAndHashCode.class, - RequiredArgsConstructor.class, AllArgsConstructor.class, NoArgsConstructor.class, + 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)); /** @@ -170,9 +168,9 @@ public class HandlerUtil { * 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. @@ -215,19 +213,19 @@ public class HandlerUtil { /** * Generates a getter name from a given field name. - * + * * Strategy: *
    *
  • 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}.
  • *
  • If {@code Accessors} has {@code fluent=true}, then return the basename.
  • *
  • Pick a prefix. 'get' normally, but 'is' if {@code isBoolean} is true.
  • - *
  • 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.
  • + *
  • 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.
  • *
  • 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.
  • *
- * + * * @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}. @@ -239,19 +237,19 @@ public class HandlerUtil { /** * Generates a setter name from a given field name. - * + * * Strategy: *
    *
  • 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}.
  • *
  • If {@code Accessors} has {@code fluent=true}, then return the basename.
  • *
  • 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.
  • + * If so, replace {@code is} with {@code set} and return that. *
  • 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 {@code "set"} plus the possibly title/uppercased first character, and the rest of the field name.
  • *
- * + * * @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}. @@ -263,18 +261,18 @@ public class HandlerUtil { /** * Generates a wither name from a given field name. - * + * * Strategy: *
    *
  • 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}.
  • *
  • 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.
  • + * If so, replace {@code is} with {@code with} and return that. *
  • 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 {@code "with"} plus the possibly title/uppercased first character, and the rest of the field name.
  • *
- * + * * @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}. @@ -315,10 +313,10 @@ public class HandlerUtil { /** * 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:
* {@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}. @@ -329,10 +327,10 @@ public class HandlerUtil { /** * 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:
* {@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}. @@ -343,10 +341,10 @@ public class HandlerUtil { /** * 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:
* {@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}. diff --git a/test/transform/resource/after-ecj/VarNullInit.java b/test/transform/resource/after-ecj/VarNullInit.java index 966afdc9..ba8c3e46 100644 --- a/test/transform/resource/after-ecj/VarNullInit.java +++ b/test/transform/resource/after-ecj/VarNullInit.java @@ -1,10 +1,10 @@ import lombok.experimental.var; class VarNullInit { - ValNullInit() { + VarNullInit() { super(); } void method() { - final @var java.lang.Object x = null; + @var java.lang.Object x = null; } } \ No newline at end of file diff --git a/test/transform/resource/before/VarComplex.java b/test/transform/resource/before/VarComplex.java index a30492ac..bfaa8804 100644 --- a/test/transform/resource/before/VarComplex.java +++ b/test/transform/resource/before/VarComplex.java @@ -1,3 +1,4 @@ +//CONF: lombok.var.flagUsage = ALLOW import lombok.experimental.var; public class VarComplex { diff --git a/test/transform/resource/before/VarInFor.java b/test/transform/resource/before/VarInFor.java index ed22edc3..cc8c387e 100644 --- a/test/transform/resource/before/VarInFor.java +++ b/test/transform/resource/before/VarInFor.java @@ -1,3 +1,4 @@ +//CONF: lombok.var.flagUsage = ALLOW import lombok.experimental.var; public class VarInFor { diff --git a/test/transform/resource/before/VarInForOld.java b/test/transform/resource/before/VarInForOld.java index 3000f58c..f90aba7f 100644 --- a/test/transform/resource/before/VarInForOld.java +++ b/test/transform/resource/before/VarInForOld.java @@ -1,3 +1,4 @@ +//CONF: lombok.var.flagUsage = ALLOW import lombok.experimental.var; public class VarInForOld { diff --git a/test/transform/resource/before/VarModifier.java b/test/transform/resource/before/VarModifier.java index 5c68caa7..7250c1c5 100644 --- a/test/transform/resource/before/VarModifier.java +++ b/test/transform/resource/before/VarModifier.java @@ -1,3 +1,4 @@ +//CONF: lombok.var.flagUsage = ALLOW import lombok.experimental.var; public class VarModifier { diff --git a/test/transform/resource/before/VarNullInit.java b/test/transform/resource/before/VarNullInit.java index 14d7b4a3..f7f9c55b 100644 --- a/test/transform/resource/before/VarNullInit.java +++ b/test/transform/resource/before/VarNullInit.java @@ -1,3 +1,4 @@ +//CONF: lombok.var.flagUsage = ALLOW import lombok.experimental.var; class VarNullInit { -- cgit From 69eeb9edc767bb3ceb0320bb5a0ea60dfabe827c Mon Sep 17 00:00:00 2001 From: Bulgakov Alexander Date: Sat, 12 Nov 2016 23:04:48 +0300 Subject: a initialization of variable like "var o = null;" will throw the compile time error "variable initializer is 'null'" --- src/core/lombok/eclipse/handlers/HandleVal.java | 9 +++++++++ src/core/lombok/javac/handlers/HandleVal.java | 10 +++++++++- test/transform/resource/after-delombok/VarNullInit.java | 3 ++- test/transform/resource/after-ecj/VarNullInit.java | 5 ++--- test/transform/resource/before/VarNullInit.java | 2 +- .../resource/messages-delombok/VarNullInit.java.messages | 1 + test/transform/resource/messages-ecj/VarNullInit.java.messages | 1 + 7 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 test/transform/resource/messages-delombok/VarNullInit.java.messages create mode 100644 test/transform/resource/messages-ecj/VarNullInit.java.messages (limited to 'test/transform/resource/before/VarNullInit.java') diff --git a/src/core/lombok/eclipse/handlers/HandleVal.java b/src/core/lombok/eclipse/handlers/HandleVal.java index e8b1deb4..49820f60 100644 --- a/src/core/lombok/eclipse/handlers/HandleVal.java +++ b/src/core/lombok/eclipse/handlers/HandleVal.java @@ -23,6 +23,7 @@ package lombok.eclipse.handlers; import lombok.ConfigurationKeys; import lombok.core.HandlerPriority; +import lombok.core.LombokNode; import lombok.eclipse.DeferUntilPostDiet; import lombok.eclipse.EclipseASTAdapter; import lombok.eclipse.EclipseASTVisitor; @@ -33,11 +34,13 @@ import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer; import org.eclipse.jdt.internal.compiler.ast.ForStatement; import org.eclipse.jdt.internal.compiler.ast.ForeachStatement; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; +import org.eclipse.jdt.internal.compiler.ast.NullLiteral; import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.mangosdk.spi.ProviderFor; import static lombok.core.handlers.HandlerUtil.handleFlagUsage; import static lombok.eclipse.handlers.EclipseHandlerUtil.typeMatches; +import static lombok.javac.handlers.HandleVal.VARIABLE_INITIALIZER_IS_NULL; /* * This class just handles 3 basic error cases. The real meat of eclipse 'val' support is in {@code PatchVal} and {@code PatchValEclipse}. @@ -81,5 +84,11 @@ public class HandleVal extends EclipseASTAdapter { if (local.initialization != null && local.initialization.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) { localNode.addError("'" + annotation + "' is not allowed with lambda expressions."); } + + if(isVar && local.initialization instanceof NullLiteral) addVarNullInitMessage(localNode); + } + + public static void addVarNullInitMessage(LombokNode localNode) { + localNode.addError(VARIABLE_INITIALIZER_IS_NULL); } } diff --git a/src/core/lombok/javac/handlers/HandleVal.java b/src/core/lombok/javac/handlers/HandleVal.java index 47965573..2ff3bf38 100644 --- a/src/core/lombok/javac/handlers/HandleVal.java +++ b/src/core/lombok/javac/handlers/HandleVal.java @@ -22,7 +22,10 @@ package lombok.javac.handlers; import static lombok.core.handlers.HandlerUtil.*; +import static lombok.eclipse.handlers.HandleVal.addVarNullInitMessage; import static lombok.javac.handlers.JavacHandlerUtil.*; + +import com.sun.tools.javac.tree.JCTree.JCLiteral; import lombok.ConfigurationKeys; import lombok.experimental.var; import lombok.val; @@ -51,7 +54,9 @@ import com.sun.tools.javac.util.List; @HandlerPriority(65536) // 2^16; resolution needs to work, so if the RHS expression is i.e. a call to a generated getter, we have to run after that getter has been generated. @ResolutionResetNeeded public class HandleVal extends JavacASTAdapter { - + + public static final String VARIABLE_INITIALIZER_IS_NULL = "variable initializer is 'null'"; + private static boolean eq(String typeTreeToString, String key) { return (typeTreeToString.equals(key) || typeTreeToString.equals("lombok." + key)); } @@ -118,6 +123,9 @@ public class HandleVal extends JavacASTAdapter { try { if (rhsOfEnhancedForLoop == null) { if (local.init.type == null) { + if (isVar && local.init instanceof JCLiteral && ((JCLiteral) local.init).value == null) { + addVarNullInitMessage(localNode); + } JavacResolution resolver = new JavacResolution(localNode.getContext()); try { type = ((JCExpression) resolver.resolveMethodMember(localNode).get(local.init)).type; diff --git a/test/transform/resource/after-delombok/VarNullInit.java b/test/transform/resource/after-delombok/VarNullInit.java index f0087765..8ec2ea73 100644 --- a/test/transform/resource/after-delombok/VarNullInit.java +++ b/test/transform/resource/after-delombok/VarNullInit.java @@ -1,4 +1,5 @@ -class VarNullInit { + +public class VarNullInit { void method() { java.lang.Object x = null; } diff --git a/test/transform/resource/after-ecj/VarNullInit.java b/test/transform/resource/after-ecj/VarNullInit.java index ba8c3e46..3eb2d506 100644 --- a/test/transform/resource/after-ecj/VarNullInit.java +++ b/test/transform/resource/after-ecj/VarNullInit.java @@ -1,7 +1,6 @@ import lombok.experimental.var; - -class VarNullInit { - VarNullInit() { +public class VarNullInit { + public VarNullInit() { super(); } void method() { diff --git a/test/transform/resource/before/VarNullInit.java b/test/transform/resource/before/VarNullInit.java index f7f9c55b..efdc9d9e 100644 --- a/test/transform/resource/before/VarNullInit.java +++ b/test/transform/resource/before/VarNullInit.java @@ -1,7 +1,7 @@ //CONF: lombok.var.flagUsage = ALLOW import lombok.experimental.var; -class VarNullInit { +public class VarNullInit { void method() { var x = null; } diff --git a/test/transform/resource/messages-delombok/VarNullInit.java.messages b/test/transform/resource/messages-delombok/VarNullInit.java.messages new file mode 100644 index 00000000..190ab7c4 --- /dev/null +++ b/test/transform/resource/messages-delombok/VarNullInit.java.messages @@ -0,0 +1 @@ +6 variable initializer is 'null' \ No newline at end of file diff --git a/test/transform/resource/messages-ecj/VarNullInit.java.messages b/test/transform/resource/messages-ecj/VarNullInit.java.messages new file mode 100644 index 00000000..190ab7c4 --- /dev/null +++ b/test/transform/resource/messages-ecj/VarNullInit.java.messages @@ -0,0 +1 @@ +6 variable initializer is 'null' \ No newline at end of file -- cgit