diff options
author | emcmanus <emcmanus@google.com> | 2020-01-10 09:09:05 -0800 |
---|---|---|
committer | Colin Decker <cgdecker@gmail.com> | 2020-01-13 12:41:20 -0500 |
commit | bd7bed2e4df1b4012f4c97f7d09bfc54eca1c12d (patch) | |
tree | 34ad368c635a131b57efe3d9b66fb62c7ebfb280 /value/src/test/java/com/google | |
parent | 65087f1953253e1b0d00b4e29b21722b9640a102 (diff) | |
download | auto-bd7bed2e4df1b4012f4c97f7d09bfc54eca1c12d.tar.gz |
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
Diffstat (limited to 'value/src/test/java/com/google')
-rw-r--r-- | value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java | 41 |
1 files changed, 41 insertions, 0 deletions
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 |