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..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 @@ -70,15 +70,9 @@ 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 ($p == null) { - throw new NullPointerException("Null $p"); - } - - #end #if ($members[$p].kind == "ARRAY") + `java.util.Objects`.requireNonNull($p, "Null $p"); #if ($params[$p].kind == "ARRAY") this.$p = #cloneArray(${p}); @@ -98,10 +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 - 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 ec0910c2a0..2533c95d97 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,23 +68,18 @@ ${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) - if ($p == null) { - throw new NullPointerException("Null $p.name"); - } - #else - `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 = `java.util.Objects`.requireNonNull($p); #end - - this.$p = $p; #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 447b7947e2..ca7470911b 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 @@ -77,6 +77,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() + "\")", @@ -87,10 +88,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() {", @@ -247,6 +245,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() + "\")", @@ -256,9 +255,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);", " }", "", @@ -358,6 +355,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(), "", @@ -371,14 +369,10 @@ 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\");", - " }", - " this.enums = enums.toArray(new MyEnum[0]);", + " Objects.requireNonNull(enums, \"Null enums\");", + " this.enums = enums.toArray(new MyEnum[0];", " }", "", " @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 a898196cab..e42fc5b185 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 @@ -172,6 +172,7 @@ public void importTwoWays() { "package foo.bar;", "", "import java.util.Arrays;", + "import java.util.Objects;", GeneratedImport.importGeneratedAnnotationType(), "", "@Generated(\"" + AutoValueProcessor.class.getName() + "\")", @@ -180,14 +181,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\")", @@ -311,6 +306,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\")", @@ -321,10 +317,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 5b57289156..ef038718c1 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",