aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉamonn McManus <emcmanus@google.com>2023-07-11 16:25:07 -0700
committerGoogle Java Core Libraries <java-libraries-firehose+copybara@google.com>2023-07-11 16:25:50 -0700
commite5b4b548486549ba096983d6d79ed39f9a0e16b7 (patch)
tree57edf373f9513c790a9895cf2dd08003c9ac8c6b
parent895ce2b55f58873fac0140272a01f26d14871784 (diff)
downloadauto-e5b4b548486549ba096983d6d79ed39f9a0e16b7.tar.gz
Output a warning if a `setX` method in a `Builder` is marked `@Nullable`.
If the `setX` method itself, or its return type, is `@Nullable` then a warning makes sense. The method always returns a non-null value, the `Builder` itself. RELNOTES=A warning is now produced if a `setX` method in a `Builder` or its return type is marked `@Nullable`. Those methods always return the `Builder` instance, which is never null. PiperOrigin-RevId: 547329146
-rw-r--r--value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java7
-rw-r--r--value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java31
2 files changed, 38 insertions, 0 deletions
diff --git a/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java b/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
index b0872009..591b0cc8 100644
--- a/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
+++ b/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
@@ -403,6 +403,13 @@ abstract class BuilderMethodClassifier<E extends Element> {
TypeMirror returnType = methodMirror.getReturnType();
if (typeUtils.isSubtype(builderType.asType(), returnType)
&& !MoreTypes.isTypeOf(Object.class, returnType)) {
+ if (nullableAnnotationFor(method, returnType).isPresent()) {
+ errorReporter.
+ reportWarning(
+ method,
+ "[%sBuilderSetterNullable] Setter methods always return the Builder so @Nullable"
+ + " is not appropriate", autoWhat());
+ }
// We allow the return type to be a supertype (other than Object), to support step builders.
TypeMirror parameterType = Iterables.getOnlyElement(methodMirror.getParameterTypes());
propertyNameToSetters.put(
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 01ec81fe..e9fdcbfd 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
@@ -2038,6 +2038,37 @@ public class AutoValueCompilationTest {
}
@Test
+ public void autoValueBuilderSetterReturnsNullable() {
+ JavaFileObject javaFileObject =
+ JavaFileObjects.forSourceLines(
+ "foo.bar.Baz",
+ "package foo.bar;",
+ "",
+ "import com.google.auto.value.AutoValue;",
+ "import javax.annotation.Nullable;",
+ "",
+ "@AutoValue",
+ "public abstract class Baz {",
+ " abstract String blam();",
+ "",
+ " @AutoValue.Builder",
+ " public interface Builder {",
+ " @Nullable Builder blam(String x);",
+ " Baz build();",
+ " }",
+ "}");
+ Compilation compilation =
+ javac()
+ .withProcessors(new AutoValueProcessor(), new AutoValueBuilderProcessor())
+ .compile(javaFileObject);
+ assertThat(compilation)
+ .hadWarningContaining(
+ "Setter methods always return the Builder so @Nullable is not appropriate")
+ .inFile(javaFileObject)
+ .onLineContaining("Builder blam(String x)");
+ }
+
+ @Test
public void autoValueBuilderWrongTypeSetterWithCopyOf() {
JavaFileObject javaFileObject =
JavaFileObjects.forSourceLines(