Skip to content

Commit 4506804

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
Apply @Nullable to fields in the generated class where appropriate.
We already did this if it was a `TYPE_USE` `@Nullable` or if the corresponding property method was annotated with `@CopyAnnotations`. Now we do it in every other case as well. RELNOTES=If an AutoValue property method is `@Nullable`, the corresponding field in the generated class will be too. This was already the case for `TYPE_USE` `@Nullable` or if the method had `@CopyAnnotations`, but now `@Nullable` will be copied in other cases too. PiperOrigin-RevId: 559765107
1 parent e67ab8b commit 4506804

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.google.auto.common.AnnotationMirrors.getAnnotationValue;
1919
import static com.google.auto.common.GeneratedAnnotations.generatedAnnotation;
20+
import static com.google.auto.common.MoreElements.asType;
2021
import static com.google.auto.common.MoreElements.getPackage;
2122
import static com.google.auto.common.MoreElements.isAnnotationPresent;
2223
import static com.google.auto.common.MoreStreams.toImmutableList;
@@ -1215,19 +1216,31 @@ final ImmutableListMultimap<ExecutableElement, AnnotationMirror> propertyFieldAn
12151216

12161217
private ImmutableList<AnnotationMirror> propertyFieldAnnotations(
12171218
TypeElement type, ExecutableElement method) {
1219+
// We need to exclude type annotations from the ones being output on the method, since
1220+
// they will be output as part of the field's type.
1221+
Set<String> returnTypeAnnotations =
1222+
getReturnTypeAnnotations(method, this::annotationAppliesToFields);
12181223
if (!hasAnnotationMirror(method, COPY_ANNOTATIONS_NAME)) {
1219-
return ImmutableList.of();
1224+
// If there's no @CopyAnnotations, we will still copy a @Nullable annotation, if (1) it is not
1225+
// a TYPE_USE annotation (those appear as part of the type in the generated code) and (2) it
1226+
// applies to fields. All known non-TYPE_USE @Nullable annotations do apply to fields, but we
1227+
// check just in case.
1228+
return method.getAnnotationMirrors().stream()
1229+
.filter(
1230+
a -> {
1231+
TypeElement annotationType = asType(a.getAnnotationType().asElement());
1232+
return isNullable(a)
1233+
&& !returnTypeAnnotations.contains(annotationType.getQualifiedName().toString())
1234+
&& annotationAppliesToFields(annotationType);
1235+
})
1236+
.collect(toImmutableList());
12201237
}
12211238
ImmutableSet<String> excludedAnnotations =
12221239
ImmutableSet.<String>builder()
12231240
.addAll(getExcludedAnnotationClassNames(method))
12241241
.add(Override.class.getCanonicalName())
12251242
.build();
12261243

1227-
// We need to exclude type annotations from the ones being output on the method, since
1228-
// they will be output as part of the field's type.
1229-
Set<String> returnTypeAnnotations =
1230-
getReturnTypeAnnotations(method, this::annotationAppliesToFields);
12311244
Set<String> nonFieldAnnotations =
12321245
method.getAnnotationMirrors().stream()
12331246
.map(a -> a.getAnnotationType().asElement())

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ public void correctBuilder() {
11631163
"final class AutoValue_Baz<T extends Number> extends Baz<T> {",
11641164
" private final int anInt;",
11651165
" private final byte[] aByteArray;",
1166+
" @Nullable",
11661167
" private final int[] aNullableIntArray;",
11671168
" private final List<T> aList;",
11681169
" private final ImmutableMap<T, String> anImmutableMap;",

0 commit comments

Comments
 (0)