Skip to content

Commit e5b4b54

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
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
1 parent 895ce2b commit e5b4b54

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ private void classifyMethodOneArg(ExecutableElement method) {
403403
TypeMirror returnType = methodMirror.getReturnType();
404404
if (typeUtils.isSubtype(builderType.asType(), returnType)
405405
&& !MoreTypes.isTypeOf(Object.class, returnType)) {
406+
if (nullableAnnotationFor(method, returnType).isPresent()) {
407+
errorReporter.
408+
reportWarning(
409+
method,
410+
"[%sBuilderSetterNullable] Setter methods always return the Builder so @Nullable"
411+
+ " is not appropriate", autoWhat());
412+
}
406413
// We allow the return type to be a supertype (other than Object), to support step builders.
407414
TypeMirror parameterType = Iterables.getOnlyElement(methodMirror.getParameterTypes());
408415
propertyNameToSetters.put(

value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,37 @@ public void autoValueBuilderWrongTypeSetter() {
20372037
.onLineContaining("Builder blim(String x)");
20382038
}
20392039

2040+
@Test
2041+
public void autoValueBuilderSetterReturnsNullable() {
2042+
JavaFileObject javaFileObject =
2043+
JavaFileObjects.forSourceLines(
2044+
"foo.bar.Baz",
2045+
"package foo.bar;",
2046+
"",
2047+
"import com.google.auto.value.AutoValue;",
2048+
"import javax.annotation.Nullable;",
2049+
"",
2050+
"@AutoValue",
2051+
"public abstract class Baz {",
2052+
" abstract String blam();",
2053+
"",
2054+
" @AutoValue.Builder",
2055+
" public interface Builder {",
2056+
" @Nullable Builder blam(String x);",
2057+
" Baz build();",
2058+
" }",
2059+
"}");
2060+
Compilation compilation =
2061+
javac()
2062+
.withProcessors(new AutoValueProcessor(), new AutoValueBuilderProcessor())
2063+
.compile(javaFileObject);
2064+
assertThat(compilation)
2065+
.hadWarningContaining(
2066+
"Setter methods always return the Builder so @Nullable is not appropriate")
2067+
.inFile(javaFileObject)
2068+
.onLineContaining("Builder blam(String x)");
2069+
}
2070+
20402071
@Test
20412072
public void autoValueBuilderWrongTypeSetterWithCopyOf() {
20422073
JavaFileObject javaFileObject =

0 commit comments

Comments
 (0)