diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java index b42af90115..03f2bd6b73 100644 --- a/value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java +++ b/value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java @@ -241,7 +241,15 @@ public String getBuilderFieldType() { || availableNullableTypeAnnotations.isEmpty()) { return type; } - return TypeEncoder.encodeWithAnnotations(annotatedType, availableNullableTypeAnnotations); + return TypeEncoder.encodeWithAnnotations( + annotatedType, availableNullableTypeAnnotations, a -> isNonNullAnnotation(a)); + } + + private static boolean isNonNullAnnotation(AnnotationMirror annotation) { + String simpleName = annotation.getAnnotationType().asElement().getSimpleName().toString(); + return simpleName.equals("NonNull") + || simpleName.equals("NotNull") + || simpleName.equals("Nonnull"); } /** diff --git a/value/src/main/java/com/google/auto/value/processor/TypeEncoder.java b/value/src/main/java/com/google/auto/value/processor/TypeEncoder.java index ef0061a625..ab93421f6c 100644 --- a/value/src/main/java/com/google/auto/value/processor/TypeEncoder.java +++ b/value/src/main/java/com/google/auto/value/processor/TypeEncoder.java @@ -156,7 +156,10 @@ private static String encodeWithAnnotations( .addAll(extraAnnotations) .build() : t.getAnnotationMirrors(); - return new AnnotatedEncodingTypeVisitor(excludedAnnotationTypes, getTypeAnnotations) + return new AnnotatedEncodingTypeVisitor( + a -> excludedAnnotationTypes.contains(a.getAnnotationType()), + !excludedAnnotationTypes.isEmpty(), + getTypeAnnotations) .visit2(type, sb) .toString(); } @@ -174,6 +177,25 @@ static String encodeWithAnnotations( AnnotatedTypeMirror type, ImmutableList extraAnnotations, Set excludedAnnotationTypes) { + return encodeWithAnnotations( + type, + extraAnnotations, + a -> excludedAnnotationTypes.contains(a.getAnnotationType()), + !excludedAnnotationTypes.isEmpty()); + } + + static String encodeWithAnnotations( + AnnotatedTypeMirror type, + ImmutableList extraAnnotations, + java.util.function.Predicate excludeAnnotation) { + return encodeWithAnnotations(type, extraAnnotations, excludeAnnotation, true); + } + + private static String encodeWithAnnotations( + AnnotatedTypeMirror type, + ImmutableList extraAnnotations, + java.util.function.Predicate excludeAnnotation, + boolean hasExclusions) { StringBuilder sb = new StringBuilder(); // A function that is equivalent to t.getAnnotationMirrors() except when the t in question is // our starting type. In that case we also add extraAnnotations to the result. @@ -185,7 +207,7 @@ static String encodeWithAnnotations( .addAll(extraAnnotations) .build() : t.getAnnotationMirrors(); - return new AnnotatedEncodingTypeVisitor(excludedAnnotationTypes, getTypeAnnotations) + return new AnnotatedEncodingTypeVisitor(excludeAnnotation, hasExclusions, getTypeAnnotations) .visit2(type.getType(), sb) .toString(); } @@ -384,13 +406,16 @@ void appendTypeArguments(DeclaredType type, StringBuilder sb) {} * `java.util.List`} form. */ private static class AnnotatedEncodingTypeVisitor extends EncodingTypeVisitor { - private final Set excludedAnnotationTypes; + private final java.util.function.Predicate excludeAnnotation; + private final boolean hasExclusions; private final Function> getTypeAnnotations; AnnotatedEncodingTypeVisitor( - Set excludedAnnotationTypes, + java.util.function.Predicate excludeAnnotation, + boolean hasExclusions, Function> getTypeAnnotations) { - this.excludedAnnotationTypes = excludedAnnotationTypes; + this.excludeAnnotation = excludeAnnotation; + this.hasExclusions = hasExclusions; this.getTypeAnnotations = getTypeAnnotations; } @@ -398,14 +423,12 @@ private void appendAnnotationsWithExclusions( List annotations, StringBuilder sb) { // Optimization for the very common cases where there are no annotations or there are no // exclusions. - if (annotations.isEmpty() || excludedAnnotationTypes.isEmpty()) { + if (annotations.isEmpty() || !hasExclusions) { appendAnnotations(annotations, sb); return; } List includedAnnotations = - annotations.stream() - .filter(a -> !excludedAnnotationTypes.contains(a.getAnnotationType())) - .collect(toList()); + annotations.stream().filter(a -> !excludeAnnotation.test(a)).collect(toList()); appendAnnotations(includedAnnotations, sb); }