From bd7bed2e4df1b4012f4c97f7d09bfc54eca1c12d Mon Sep 17 00:00:00 2001 From: emcmanus Date: Fri, 10 Jan 2020 09:09:05 -0800 Subject: Make it an error if a setter has a @Nullable parameter when the property being set is not @Nullable. We will generate code that rejects a null parameter whether or not @Nullable is present, so allowing it is just misleading users of the API. The mirror situation, where the property is @Nullable but the setter is not, is arguably also incorrect. In that case the generated code does *not* reject a null parameter, even though we might expect it to in the absence of @Nullable on the parameter. However, changing that would surely break a lot of existing code. Fixes https://github.com/google/auto/issues/777. RELNOTES=It is now a compilation error if a setter method in a builder has a parameter marked @Nullable when the corresponding property is not in fact @Nullable. This already generated a NullPointerException at runtime. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=289103954 --- .../value/processor/AutoValueCompilationTest.java | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'value/src/test') 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 77390765..27be5401 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 @@ -1678,6 +1678,47 @@ public class AutoValueCompilationTest { .onLine(12); } + @Test + public void autoValueBuilderNullableSetterForNonNullable() { + JavaFileObject nullableFileObject = + JavaFileObjects.forSourceLines( + "foo.bar.Nullable", + "package foo.bar;", + "", + "import java.lang.annotation.ElementType;", + "import java.lang.annotation.Target;", + "", + "@Target(ElementType.TYPE_USE)", + "public @interface Nullable {}"); + JavaFileObject javaFileObject = + JavaFileObjects.forSourceLines( + "foo.bar.Baz", + "package foo.bar;", + "", + "import com.google.auto.value.AutoValue;", + "", + "@AutoValue", + "public abstract class Baz {", + " abstract String notNull();", + "", + " @AutoValue.Builder", + " public interface Builder {", + " Builder setNotNull(@Nullable String x);", + " Baz build();", + " }", + "}"); + Compilation compilation = + javac() + .withProcessors(new AutoValueProcessor(), new AutoValueBuilderProcessor()) + .compile(javaFileObject, nullableFileObject); + assertThat(compilation) + .hadErrorContaining( + "Parameter of setter method is @Nullable but property method" + + " foo.bar.Baz.notNull() is not") + .inFile(javaFileObject) + .onLineContaining("setNotNull"); + } + // Check that we get a helpful error message if some of your properties look like getters but // others don't. @Test -- cgit v1.2.3