From 9e3f988de14ecd958293972d64a2eaf5334a6fea Mon Sep 17 00:00:00 2001 From: reugn Date: Sat, 13 Nov 2021 16:31:40 +0200 Subject: [PATCH 1/3] use `Objects.requireNonNull` wherever possible --- .../auto/value/processor/autoannotation.vm | 16 +++++++--------- .../com/google/auto/value/processor/autovalue.vm | 10 ++++------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/value/src/main/java/com/google/auto/value/processor/autoannotation.vm b/value/src/main/java/com/google/auto/value/processor/autoannotation.vm index 5cdad62948..89c9a0781d 100644 --- a/value/src/main/java/com/google/auto/value/processor/autoannotation.vm +++ b/value/src/main/java/com/google/auto/value/processor/autoannotation.vm @@ -70,15 +70,11 @@ final class $className implements $annotationName, `java.io.Serializable` { $params[$p].type $members[$p] #if ($foreach.hasNext) , #end #end ) { #foreach ($p in $params.keySet()) - #if (!$members[$p].kind.primitive) + #if ($members[$p].kind == "ARRAY") if ($p == null) { - throw new NullPointerException("Null $p"); + throw new NullPointerException("Null $p"); } - - #end - - #if ($members[$p].kind == "ARRAY") #if ($params[$p].kind == "ARRAY") this.$p = #cloneArray(${p}); @@ -99,9 +95,11 @@ final class $className implements $annotationName, `java.io.Serializable` { #end #else - - this.$p = $p; - + #if (!$members[$p].kind.primitive) + this.$p = `java.util.Objects`.requireNonNull($p, "Null $p"); + #else + this.$p = $p; + #end #end #end diff --git a/value/src/main/java/com/google/auto/value/processor/autovalue.vm b/value/src/main/java/com/google/auto/value/processor/autovalue.vm index 18ca827aea..e72578cb6f 100644 --- a/value/src/main/java/com/google/auto/value/processor/autovalue.vm +++ b/value/src/main/java/com/google/auto/value/processor/autovalue.vm @@ -75,16 +75,14 @@ ${modifiers}class $subclass$formalTypes extends $origClass$actualTypes { ## the constructor is called from the extension code. #if ($identifiers) - if ($p == null) { - throw new NullPointerException("Null $p.name"); - } + this.$p = `java.util.Objects`.requireNonNull($p, "Null $p"); #else - `java.util.Objects`.requireNonNull($p); + this.$p = `java.util.Objects`.requireNonNull($p); #end - #end - + #else this.$p = $p; + #end #end } From 59b0a0648b3177cf502562a5f1a5673dcea79214 Mon Sep 17 00:00:00 2001 From: reugn Date: Sat, 13 Nov 2021 16:32:25 +0200 Subject: [PATCH 2/3] align tests with the fix --- .../AutoAnnotationCompilationTest.java | 6 ++---- .../processor/AutoValueCompilationTest.java | 17 +++++------------ .../auto/value/processor/ExtensionTest.java | 6 ++---- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java index 1f79a074b5..38d1c2fa76 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java @@ -75,6 +75,7 @@ public void testSimple() { "import com.example.annotations.MyAnnotation;", "import com.example.enums.MyEnum;", "import java.io.Serializable;", + "import java.util.Objects;", GeneratedImport.importGeneratedAnnotationType(), "", "@Generated(\"" + AutoAnnotationProcessor.class.getName() + "\")", @@ -85,10 +86,7 @@ public void testSimple() { " private static final int defaultedValue = 23;", "", " AutoAnnotation_AnnotationFactory_newMyAnnotation(MyEnum value) {", - " if (value == null) {", - " throw new NullPointerException(\"Null value\");", - " }", - " this.value = value;", + " this.value = Objects.requireNonNull(value, \"Null value\");", " }", "", " @Override public Class annotationType() {", diff --git a/value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java index 1bb84f7569..d1959723f2 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java @@ -163,6 +163,7 @@ public void importTwoWays() { "package foo.bar;", "", "import java.util.Arrays;", + "import java.util.Objects;", GeneratedImport.importGeneratedAnnotationType(), "", "@Generated(\"" + AutoValueProcessor.class.getName() + "\")", @@ -171,14 +172,8 @@ public void importTwoWays() { " private final Arrays arrays;", "", " AutoValue_Baz(int[] ints, Arrays arrays) {", - " if (ints == null) {", - " throw new NullPointerException(\"Null ints\");", - " }", - " this.ints = ints;", - " if (arrays == null) {", - " throw new NullPointerException(\"Null arrays\");", - " }", - " this.arrays = arrays;", + " this.ints = Objects.requireNonNull(ints, \"Null ints\");", + " this.arrays = Objects.requireNonNull(arrays, \"Null arrays\");", " }", "", " @SuppressWarnings(\"mutable\")", @@ -301,6 +296,7 @@ public void testNestedParameterizedTypesWithTypeAnnotations() { "", "import foo.bar.Annot;", "import foo.baz.OuterWithTypeParam;", + "import java.util.Objects;", GeneratedImport.importGeneratedAnnotationType(), "", "@Generated(\"com.google.auto.value.processor.AutoValueProcessor\")", @@ -311,10 +307,7 @@ public void testNestedParameterizedTypesWithTypeAnnotations() { " AutoValue_Nesty(", " @Annot(1) OuterWithTypeParam<@Annot(2) Double>" + ".@Annot(3) InnerWithTypeParam<@Annot(4) String> inner) {", - " if (inner == null) {", - " throw new NullPointerException(\"Null inner\");", - " }", - " this.inner = inner;", + " this.inner = Objects.requireNonNull(inner, \"Null inner\");", " }", "", " @Override", diff --git a/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java b/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java index 56eaad25f3..cdc728dc1c 100644 --- a/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java +++ b/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java @@ -122,6 +122,7 @@ public void testExtensionConsumesProperties() { "foo.bar.$AutoValue_Baz", "package foo.bar;", "", + "import java.util.Objects;", GeneratedImport.importGeneratedAnnotationType(), "", "@Generated(\"com.google.auto.value.processor.AutoValueProcessor\")", @@ -131,10 +132,7 @@ public void testExtensionConsumesProperties() { "", " $AutoValue_Baz(", " String foo) {", - " if (foo == null) {", - " throw new NullPointerException(\"Null foo\");", - " }", - " this.foo = foo;", + " this.foo = Objects.requireNonNull(foo, \"Null foo\");", " }", "", " @Override", From 266e6582308ce223071314f1f2b58e4a4f47b48a Mon Sep 17 00:00:00 2001 From: reugn Date: Mon, 15 Nov 2021 10:02:06 +0200 Subject: [PATCH 3/3] fix indentation and invert if statement --- .../auto/value/processor/autoannotation.vm | 12 ++++------ .../google/auto/value/processor/autovalue.vm | 23 ++++++++----------- .../AutoAnnotationCompilationTest.java | 14 ++++------- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/value/src/main/java/com/google/auto/value/processor/autoannotation.vm b/value/src/main/java/com/google/auto/value/processor/autoannotation.vm index 89c9a0781d..fa1dc41aa0 100644 --- a/value/src/main/java/com/google/auto/value/processor/autoannotation.vm +++ b/value/src/main/java/com/google/auto/value/processor/autoannotation.vm @@ -72,9 +72,7 @@ final class $className implements $annotationName, `java.io.Serializable` { #foreach ($p in $params.keySet()) #if ($members[$p].kind == "ARRAY") - if ($p == null) { - throw new NullPointerException("Null $p"); - } + `java.util.Objects`.requireNonNull($p, "Null $p"); #if ($params[$p].kind == "ARRAY") this.$p = #cloneArray(${p}); @@ -94,12 +92,10 @@ final class $className implements $annotationName, `java.io.Serializable` { this.$p = ${p}.toArray(new ${members[$p].componentType}[0]); #end + #elseif (!$members[$p].kind.primitive) + this.$p = `java.util.Objects`.requireNonNull($p, "Null $p"); #else - #if (!$members[$p].kind.primitive) - this.$p = `java.util.Objects`.requireNonNull($p, "Null $p"); - #else - this.$p = $p; - #end + this.$p = $p; #end #end diff --git a/value/src/main/java/com/google/auto/value/processor/autovalue.vm b/value/src/main/java/com/google/auto/value/processor/autovalue.vm index e72578cb6f..8fc2f27e02 100644 --- a/value/src/main/java/com/google/auto/value/processor/autovalue.vm +++ b/value/src/main/java/com/google/auto/value/processor/autovalue.vm @@ -68,20 +68,17 @@ ${modifiers}class $subclass$formalTypes extends $origClass$actualTypes { ${p.nullableAnnotation}$p.type $p #if ($foreach.hasNext) , #end #end ) { #foreach ($p in $props) - #if (!$p.kind.primitive && !$p.nullable && ($builderTypeName == "" || !$isFinal)) - ## We don't need a null check if the type is primitive or @Nullable. We also don't need it - ## if there is a builder, since the build() method will check for us. However, if there is a - ## builder but there are also extensions (!$isFinal) then we can't omit the null check because - ## the constructor is called from the extension code. - - #if ($identifiers) - this.$p = `java.util.Objects`.requireNonNull($p, "Null $p"); - #else - this.$p = `java.util.Objects`.requireNonNull($p); - #end - + #if ($p.kind.primitive || $p.nullable || ($builderTypeName != "" && $isFinal)) + ## We don't need a null check if the type is primitive or @Nullable. We also don't need it + ## if there is a builder, since the build() method will check for us. However, if there is a + ## builder but there are also extensions (!$isFinal) then we can't omit the null check because + ## the constructor is called from the extension code. + + this.$p = $p; + #elseif ($identifiers) + this.$p = `java.util.Objects`.requireNonNull($p, "Null $p"); #else - this.$p = $p; + this.$p = `java.util.Objects`.requireNonNull($p); #end #end } diff --git a/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java index 38d1c2fa76..aa2f78e95b 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java @@ -243,6 +243,7 @@ public void testGwtSimple() { "import com.example.annotations.MyAnnotation;", "import java.io.Serializable", "import java.util.Arrays;", + "import java.util.Objects;", GeneratedImport.importGeneratedAnnotationType(), "", "@Generated(\"" + AutoAnnotationProcessor.class.getName() + "\")", @@ -252,9 +253,7 @@ public void testGwtSimple() { " private final int[] value;", "", " AutoAnnotation_AnnotationFactory_newMyAnnotation(int[] value) {", - " if (value == null) {", - " throw new NullPointerException(\"Null value\");", - " }", + " Objects.requireNonNull(value, \"Null value\");", " this.value = Arrays.copyOf(value, value.length);", " }", "", @@ -354,6 +353,7 @@ public void testCollectionsForArrays() { "import java.util.Arrays;", "import java.util.Collection;", "import java.util.List;", + "import java.util.Objects;", "import java.util.Set;", GeneratedImport.importGeneratedAnnotationType(), "", @@ -367,13 +367,9 @@ public void testCollectionsForArrays() { " AutoAnnotation_AnnotationFactory_newMyAnnotation(", " List value,", " Set enums) {", - " if (value == null) {", - " throw new NullPointerException(\"Null value\");", - " }", + " Objects.requireNonNull(value, \"Null value\");", " this.value = intArrayFromCollection(value);", - " if (enums == null) {", - " throw new NullPointerException(\"Null enums\");", - " }", + " Objects.requireNonNull(enums, \"Null enums\");", " this.enums = enums.toArray(new MyEnum[0];", " }", "",