Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -174,6 +177,25 @@ static String encodeWithAnnotations(
AnnotatedTypeMirror type,
ImmutableList<AnnotationMirror> extraAnnotations,
Set<TypeMirror> excludedAnnotationTypes) {
return encodeWithAnnotations(
type,
extraAnnotations,
a -> excludedAnnotationTypes.contains(a.getAnnotationType()),
!excludedAnnotationTypes.isEmpty());
}

static String encodeWithAnnotations(
AnnotatedTypeMirror type,
ImmutableList<AnnotationMirror> extraAnnotations,
java.util.function.Predicate<AnnotationMirror> excludeAnnotation) {
return encodeWithAnnotations(type, extraAnnotations, excludeAnnotation, true);
}

private static String encodeWithAnnotations(
AnnotatedTypeMirror type,
ImmutableList<AnnotationMirror> extraAnnotations,
java.util.function.Predicate<AnnotationMirror> 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.
Expand All @@ -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();
}
Expand Down Expand Up @@ -384,28 +406,29 @@ void appendTypeArguments(DeclaredType type, StringBuilder sb) {}
* `java.util.List`} form.
*/
private static class AnnotatedEncodingTypeVisitor extends EncodingTypeVisitor {
private final Set<TypeMirror> excludedAnnotationTypes;
private final java.util.function.Predicate<AnnotationMirror> excludeAnnotation;
private final boolean hasExclusions;
private final Function<TypeMirror, List<? extends AnnotationMirror>> getTypeAnnotations;

AnnotatedEncodingTypeVisitor(
Set<TypeMirror> excludedAnnotationTypes,
java.util.function.Predicate<AnnotationMirror> excludeAnnotation,
boolean hasExclusions,
Function<TypeMirror, List<? extends AnnotationMirror>> getTypeAnnotations) {
this.excludedAnnotationTypes = excludedAnnotationTypes;
this.excludeAnnotation = excludeAnnotation;
this.hasExclusions = hasExclusions;
this.getTypeAnnotations = getTypeAnnotations;
}

private void appendAnnotationsWithExclusions(
List<? extends AnnotationMirror> 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<AnnotationMirror> includedAnnotations =
annotations.stream()
.filter(a -> !excludedAnnotationTypes.contains(a.getAnnotationType()))
.collect(toList());
annotations.stream().filter(a -> !excludeAnnotation.test(a)).collect(toList());
appendAnnotations(includedAnnotations, sb);
}

Expand Down
Loading