From 7cbdeb43b6120b5c8fb2ab9802bc0e0cb8d5efac Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 5 Jan 2024 09:05:09 -0800 Subject: [PATCH] Make AutoBuilder produce an error if it encounters a `@Nullable` primitive parameter RELNOTES=AutoBuilder now reports an error if it encounters a `@Nullable` primitive parameter. Primitive types cannot be `null`, and should not be annotated for nullness. PiperOrigin-RevId: 596009085 --- .../value/processor/AutoBuilderProcessor.java | 21 ++++++---- .../processor/AutoBuilderCompilationTest.java | 40 +++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java index 607e7367b9..35e67900fc 100644 --- a/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java +++ b/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java @@ -298,13 +298,20 @@ private ImmutableSet propertySet( .map( v -> { String name = v.getSimpleName().toString(); - return newProperty( - v, - identifiers.get(v), - propertyToGetterName.get(name), - Optional.ofNullable(builderInitializers.get(name)), - executable.isOptional(name), - nullables); + Property p = + newProperty( + v, + identifiers.get(v), + propertyToGetterName.get(name), + Optional.ofNullable(builderInitializers.get(name)), + executable.isOptional(name), + nullables); + if (p.isNullable() && v.asType().getKind().isPrimitive()) { + errorReporter() + .reportError( + v, "[AutoBuilderNullPrimitive] Primitive types cannot be @Nullable"); + } + return p; }) .collect(toImmutableSet()); } diff --git a/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java index c85deb5a46..a850d171cb 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java @@ -872,6 +872,46 @@ public void nullableSetterForNonNullableParameter() { .onLineContaining("thing(@Nullable String x)"); } + @Test + public void nullablePrimitiveParameter() { + JavaFileObject javaFileObject = + JavaFileObjects.forSourceLines( + "foo.bar.Baz", + "package foo.bar;", + "", + "import com.google.auto.value.AutoBuilder;", + "import com.example.annotations.Nullable;", + "", + "class Baz {", + " Baz(@Nullable int thing) {}", + "", + " @AutoBuilder", + " interface Builder {", + " abstract Builder thing(int x);", + " abstract Baz build();", + " }", + "}"); + JavaFileObject nullableFileObject = + JavaFileObjects.forSourceLines( + "com.example.annotations.Nullable", + "package com.example.annotations;", + "", + "import java.lang.annotation.ElementType;", + "import java.lang.annotation.Target;", + "", + "@Target(ElementType.TYPE_USE)", + "public @interface Nullable {}"); + Compilation compilation = + javac() + .withProcessors(new AutoBuilderProcessor()) + .compile(javaFileObject, nullableFileObject); + assertThat(compilation).failed(); + assertThat(compilation) + .hadErrorContaining("[AutoBuilderNullPrimitive] Primitive types cannot be @Nullable") + .inFile(javaFileObject) + .onLineContaining("Baz(@Nullable int thing)"); + } + @Test public void setterWrongType() { JavaFileObject javaFileObject =