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/after-delombok/VarNullInit.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 test/transform/resource/after-delombok/VarNullInit.java (limited to 'test/transform/resource/after-delombok/VarNullInit.java') diff --git a/test/transform/resource/after-delombok/VarNullInit.java b/test/transform/resource/after-delombok/VarNullInit.java new file mode 100644 index 00000000..f0087765 --- /dev/null +++ b/test/transform/resource/after-delombok/VarNullInit.java @@ -0,0 +1,5 @@ +class VarNullInit { + void method() { + java.lang.Object x = null; + } +} \ No newline at end of file -- 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/after-delombok/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