aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Chirls <kchirls@gmail.com>2017-01-31 20:08:41 -0800
committerKevin Chirls <kchirls@gmail.com>2017-01-31 20:08:41 -0800
commitf765730526e061d39c3ecd44f37de6a7f7887cf0 (patch)
tree96bb49aa9ff32fa2b27a71949ffa99e467bd1540
parent5f02accc064cc05c822c7c93c6b972b1ca387b4f (diff)
downloadlombok-f765730526e061d39c3ecd44f37de6a7f7887cf0.tar.gz
lombok-f765730526e061d39c3ecd44f37de6a7f7887cf0.tar.bz2
lombok-f765730526e061d39c3ecd44f37de6a7f7887cf0.zip
Fix for issues #869 and #1018
Generates the null check on the constructor parameter instead of the instance field. Fix for issues #869 and #1018.
-rw-r--r--src/core/lombok/javac/handlers/HandleConstructor.java2
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java15
-rw-r--r--test/transform/resource/after-delombok/ConstructorsWithAccessors.java35
-rw-r--r--test/transform/resource/before/ConstructorsWithAccessors.java8
4 files changed, 55 insertions, 5 deletions
diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java
index ef4ba088..5f523ce6 100644
--- a/src/core/lombok/javac/handlers/HandleConstructor.java
+++ b/src/core/lombok/javac/handlers/HandleConstructor.java
@@ -291,7 +291,7 @@ public class HandleConstructor {
JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, field.vartype, null);
params.append(param);
if (!nonNulls.isEmpty()) {
- JCStatement nullCheck = generateNullCheck(maker, fieldNode, source);
+ JCStatement nullCheck = generateNullCheck(maker, fieldNode, param, source);
if (nullCheck != null) nullChecks.append(nullCheck);
}
}
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index 0b4e839d..949c9f5c 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -1175,16 +1175,23 @@ public class JavacHandlerUtil {
}
/**
- * Generates a new statement that checks if the given variable is null, and if so, throws a specified exception with the
+ * Generates a new statement that checks if the given variable is null, and if so, throws a configured exception with the
* variable name as message.
- *
- * @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}.
*/
public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JavacNode source) {
+ return generateNullCheck(maker, variable, (JCVariableDecl)variable.get(), source);
+ }
+
+ /**
+ * Generates a new statement that checks if the given variable is null, and if so, throws a configured exception with the
+ * variable name as message. This is a special case method reserved for use when the provided declaration differs from the
+ * variable's declaration, i.e. in a constructor or setter where the local parameter is named the same but with the prefix
+ * stripped as a result of @Accessors.prefix.
+ */
+ public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JCVariableDecl varDecl, JavacNode source) {
NullCheckExceptionType exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
if (exceptionType == null) exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION;
- JCVariableDecl varDecl = (JCVariableDecl) variable.get();
if (isPrimitive(varDecl.vartype)) return null;
Name fieldName = varDecl.name;
JCExpression exType = genTypeRef(variable, exceptionType.getExceptionType());
diff --git a/test/transform/resource/after-delombok/ConstructorsWithAccessors.java b/test/transform/resource/after-delombok/ConstructorsWithAccessors.java
index b83c4cec..c5295ec7 100644
--- a/test/transform/resource/after-delombok/ConstructorsWithAccessors.java
+++ b/test/transform/resource/after-delombok/ConstructorsWithAccessors.java
@@ -15,3 +15,38 @@ class ConstructorsWithAccessors {
this.__huh2 = _huh2;
}
}
+
+class NonNullConstructorsWithAccessors {
+ @lombok.NonNull
+ Integer plower;
+ @lombok.NonNull
+ Integer pUpper;
+ @lombok.NonNull
+ Integer _huh;
+ @lombok.NonNull
+ Integer __huh2;
+
+ @java.beans.ConstructorProperties({"plower", "upper", "huh", "_huh2"})
+ @java.lang.SuppressWarnings("all")
+ @javax.annotation.Generated("lombok")
+ @lombok.Generated
+ public NonNullConstructorsWithAccessors(@lombok.NonNull final Integer plower, @lombok.NonNull final Integer upper, @lombok.NonNull final Integer huh, @lombok.NonNull final Integer _huh2) {
+ if (plower == null) {
+ throw new java.lang.NullPointerException("plower");
+ }
+ if (upper == null) {
+ throw new java.lang.NullPointerException("upper");
+ }
+ if (huh == null) {
+ throw new java.lang.NullPointerException("huh");
+ }
+ if (_huh2 == null) {
+ throw new java.lang.NullPointerException("_huh2");
+ }
+ this.plower = plower;
+ this.pUpper = upper;
+ this._huh = huh;
+ this.__huh2 = _huh2;
+ }
+}
+
diff --git a/test/transform/resource/before/ConstructorsWithAccessors.java b/test/transform/resource/before/ConstructorsWithAccessors.java
index e5d2939f..6facb03a 100644
--- a/test/transform/resource/before/ConstructorsWithAccessors.java
+++ b/test/transform/resource/before/ConstructorsWithAccessors.java
@@ -4,3 +4,11 @@
int _huh;
int __huh2;
}
+
+@lombok.AllArgsConstructor @lombok.experimental.Accessors(prefix={"p", "_"}) class NonNullConstructorsWithAccessors {
+ @lombok.NonNull Integer plower;
+ @lombok.NonNull Integer pUpper;
+ @lombok.NonNull Integer _huh;
+ @lombok.NonNull Integer __huh2;
+}
+