From f765730526e061d39c3ecd44f37de6a7f7887cf0 Mon Sep 17 00:00:00 2001 From: Kevin Chirls Date: Tue, 31 Jan 2017 20:08:41 -0800 Subject: 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. --- src/core/lombok/javac/handlers/HandleConstructor.java | 2 +- src/core/lombok/javac/handlers/JavacHandlerUtil.java | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src/core/lombok') 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()); -- cgit From 78e526690c22f1c5a1c93705076229ff32284741 Mon Sep 17 00:00:00 2001 From: Kevin Chirls Date: Wed, 15 Mar 2017 10:09:04 -0700 Subject: Updates for pull request #1284. Fix for issues #869 and #1018. Added name to AUTHORS file. Added ECJ fix and test. Made one of the variables final to also verify the final error case. --- AUTHORS | 1 + .../lombok/eclipse/handlers/HandleConstructor.java | 2 +- .../after-delombok/ConstructorsWithAccessors.java | 3 +-- .../after-ecj/ConstructorsWithAccessors.java | 30 ++++++++++++++++++++++ .../resource/before/ConstructorsWithAccessors.java | 2 +- 5 files changed, 34 insertions(+), 4 deletions(-) (limited to 'src/core/lombok') diff --git a/AUTHORS b/AUTHORS index 6a54b021..38ceadeb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,6 +7,7 @@ Dave Brosius Dawid Rusin Enrique da Costa Cambio Jappe van der Hel +Kevin Chirls Liu DongMiao Luan Nico Maarten Mulders diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java index 49b09231..072b6dd5 100644 --- a/src/core/lombok/eclipse/handlers/HandleConstructor.java +++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java @@ -347,7 +347,7 @@ public class HandleConstructor { Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); if (nonNulls.length != 0) { - Statement nullCheck = generateNullCheck(field, sourceNode); + Statement nullCheck = generateNullCheck(parameter, sourceNode); if (nullCheck != null) nullChecks.add(nullCheck); } parameter.annotations = copyAnnotations(source, nonNulls, nullables); diff --git a/test/transform/resource/after-delombok/ConstructorsWithAccessors.java b/test/transform/resource/after-delombok/ConstructorsWithAccessors.java index 25455943..6e9398ba 100644 --- a/test/transform/resource/after-delombok/ConstructorsWithAccessors.java +++ b/test/transform/resource/after-delombok/ConstructorsWithAccessors.java @@ -23,12 +23,11 @@ class NonNullConstructorsWithAccessors { @lombok.NonNull Integer _huh; @lombok.NonNull - Integer __huh2; + final 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"); diff --git a/test/transform/resource/after-ecj/ConstructorsWithAccessors.java b/test/transform/resource/after-ecj/ConstructorsWithAccessors.java index 7c691b42..b7a20e18 100644 --- a/test/transform/resource/after-ecj/ConstructorsWithAccessors.java +++ b/test/transform/resource/after-ecj/ConstructorsWithAccessors.java @@ -11,3 +11,33 @@ this.__huh2 = _huh2; } } +@lombok.AllArgsConstructor @lombok.experimental.Accessors(prefix = {"p", "_"}) class NonNullConstructorsWithAccessors { + @lombok.NonNull Integer plower; + @lombok.NonNull Integer pUpper; + @lombok.NonNull Integer _huh; + final @lombok.NonNull Integer __huh2; + public @java.beans.ConstructorProperties({"plower", "upper", "huh", "_huh2"}) @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") NonNullConstructorsWithAccessors(final @lombok.NonNull Integer plower, final @lombok.NonNull Integer upper, final @lombok.NonNull Integer huh, final @lombok.NonNull Integer _huh2) { + super(); + 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 6facb03a..12221ab7 100644 --- a/test/transform/resource/before/ConstructorsWithAccessors.java +++ b/test/transform/resource/before/ConstructorsWithAccessors.java @@ -9,6 +9,6 @@ @lombok.NonNull Integer plower; @lombok.NonNull Integer pUpper; @lombok.NonNull Integer _huh; - @lombok.NonNull Integer __huh2; + @lombok.NonNull final Integer __huh2; } -- cgit From afb1fb88883d4b3707de3b97fb44253a725437b7 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Mon, 18 Sep 2017 23:19:25 +0200 Subject: Rename class in test file --- src/core/lombok/javac/handlers/JavacHandlerUtil.java | 4 +++- .../resource/after-delombok/ConstructorsWithAccessors.java | 7 +++---- test/transform/resource/after-ecj/ConstructorsWithAccessors.java | 7 +++---- test/transform/resource/before/ConstructorsWithAccessors.java | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'src/core/lombok') diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index b4dcb097..23206b43 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1220,7 +1220,9 @@ public class JavacHandlerUtil { /** * 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 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. */ diff --git a/test/transform/resource/after-delombok/ConstructorsWithAccessors.java b/test/transform/resource/after-delombok/ConstructorsWithAccessors.java index 6e9398ba..94ccde08 100644 --- a/test/transform/resource/after-delombok/ConstructorsWithAccessors.java +++ b/test/transform/resource/after-delombok/ConstructorsWithAccessors.java @@ -15,7 +15,7 @@ class ConstructorsWithAccessors { } } -class NonNullConstructorsWithAccessors { +class ConstructorsWithAccessorsNonNull { @lombok.NonNull Integer plower; @lombok.NonNull @@ -28,7 +28,7 @@ class NonNullConstructorsWithAccessors { @java.beans.ConstructorProperties({"plower", "upper", "huh", "_huh2"}) @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") - public NonNullConstructorsWithAccessors(@lombok.NonNull final Integer plower, @lombok.NonNull final Integer upper, @lombok.NonNull final Integer huh, @lombok.NonNull final Integer _huh2) { + public ConstructorsWithAccessorsNonNull(@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"); } @@ -46,5 +46,4 @@ class NonNullConstructorsWithAccessors { this._huh = huh; this.__huh2 = _huh2; } -} - +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/ConstructorsWithAccessors.java b/test/transform/resource/after-ecj/ConstructorsWithAccessors.java index b7a20e18..0876a5e4 100644 --- a/test/transform/resource/after-ecj/ConstructorsWithAccessors.java +++ b/test/transform/resource/after-ecj/ConstructorsWithAccessors.java @@ -11,12 +11,12 @@ this.__huh2 = _huh2; } } -@lombok.AllArgsConstructor @lombok.experimental.Accessors(prefix = {"p", "_"}) class NonNullConstructorsWithAccessors { +@lombok.AllArgsConstructor @lombok.experimental.Accessors(prefix = {"p", "_"}) class ConstructorsWithAccessorsNonNull { @lombok.NonNull Integer plower; @lombok.NonNull Integer pUpper; @lombok.NonNull Integer _huh; final @lombok.NonNull Integer __huh2; - public @java.beans.ConstructorProperties({"plower", "upper", "huh", "_huh2"}) @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") NonNullConstructorsWithAccessors(final @lombok.NonNull Integer plower, final @lombok.NonNull Integer upper, final @lombok.NonNull Integer huh, final @lombok.NonNull Integer _huh2) { + public @java.beans.ConstructorProperties({"plower", "upper", "huh", "_huh2"}) @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") ConstructorsWithAccessorsNonNull(final @lombok.NonNull Integer plower, final @lombok.NonNull Integer upper, final @lombok.NonNull Integer huh, final @lombok.NonNull Integer _huh2) { super(); if ((plower == null)) { @@ -39,5 +39,4 @@ this._huh = huh; this.__huh2 = _huh2; } -} - +} \ No newline at end of file diff --git a/test/transform/resource/before/ConstructorsWithAccessors.java b/test/transform/resource/before/ConstructorsWithAccessors.java index 12221ab7..e67a47ce 100644 --- a/test/transform/resource/before/ConstructorsWithAccessors.java +++ b/test/transform/resource/before/ConstructorsWithAccessors.java @@ -5,7 +5,7 @@ int __huh2; } -@lombok.AllArgsConstructor @lombok.experimental.Accessors(prefix={"p", "_"}) class NonNullConstructorsWithAccessors { +@lombok.AllArgsConstructor @lombok.experimental.Accessors(prefix={"p", "_"}) class ConstructorsWithAccessorsNonNull { @lombok.NonNull Integer plower; @lombok.NonNull Integer pUpper; @lombok.NonNull Integer _huh; -- cgit