|
17 | 17 |
|
18 | 18 | import static com.google.auto.common.AnnotationMirrors.getAnnotationValue;
|
19 | 19 | import static com.google.auto.common.GeneratedAnnotations.generatedAnnotation;
|
| 20 | +import static com.google.auto.common.MoreElements.asType; |
20 | 21 | import static com.google.auto.common.MoreElements.getPackage;
|
21 | 22 | import static com.google.auto.common.MoreElements.isAnnotationPresent;
|
22 | 23 | import static com.google.auto.common.MoreStreams.toImmutableList;
|
@@ -1215,19 +1216,31 @@ final ImmutableListMultimap<ExecutableElement, AnnotationMirror> propertyFieldAn
|
1215 | 1216 |
|
1216 | 1217 | private ImmutableList<AnnotationMirror> propertyFieldAnnotations(
|
1217 | 1218 | 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); |
1218 | 1223 | 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()); |
1220 | 1237 | }
|
1221 | 1238 | ImmutableSet<String> excludedAnnotations =
|
1222 | 1239 | ImmutableSet.<String>builder()
|
1223 | 1240 | .addAll(getExcludedAnnotationClassNames(method))
|
1224 | 1241 | .add(Override.class.getCanonicalName())
|
1225 | 1242 | .build();
|
1226 | 1243 |
|
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); |
1231 | 1244 | Set<String> nonFieldAnnotations =
|
1232 | 1245 | method.getAnnotationMirrors().stream()
|
1233 | 1246 | .map(a -> a.getAnnotationType().asElement())
|
|
0 commit comments