From 4d6a0cce2d2ccc6a945352c2ba38201f753994bd Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 19 Jan 2021 12:03:07 -0800 Subject: [PATCH] Accept any annotation with the simple name `@Generated` to support non-standard annotations. Fixes https://github.com/google/error-prone/issues/1984, https://github.com/google/error-prone/issues/1863 PiperOrigin-RevId: 352622403 --- .../google/errorprone/util/ASTHelpers.java | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java index 32451e222d3..88112c17f5d 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java +++ b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java @@ -1359,30 +1359,23 @@ public static ImmutableSet getGeneratedBy(VisitorState state) { } /** - * Returns the values of the given symbol's {@code javax.annotation.Generated} or {@code - * javax.annotation.processing.Generated} annotation, if present. + * Returns the values of the given symbol's {@code Generated} annotations, if present. If the + * annotation doesn't have {@code values} set, returns the string name of the annotation itself. */ public static ImmutableSet getGeneratedBy(Symbol symbol, VisitorState state) { checkNotNull(symbol); - Optional c = - Stream.of("javax.annotation.Generated", "javax.annotation.processing.Generated") - .map(state::getSymbolFromString) - .filter(a -> a != null) - .map(symbol::attribute) - .filter(a -> a != null) - .findFirst(); - if (!c.isPresent()) { - return ImmutableSet.of(); - } - Optional values = - c.get().getElementValues().entrySet().stream() - .filter(e -> e.getKey().getSimpleName().contentEquals("value")) - .map(e -> e.getValue()) - .findAny(); - if (!values.isPresent()) { - return ImmutableSet.of(); - } - return MoreAnnotations.asStrings((AnnotationValue) values.get()).collect(toImmutableSet()); + return symbol.getRawAttributes().stream() + .filter(attribute -> attribute.type.tsym.getSimpleName().contentEquals("Generated")) + .flatMap(ASTHelpers::generatedValues) + .collect(toImmutableSet()); + } + + private static Stream generatedValues(Attribute.Compound attribute) { + return attribute.getElementValues().entrySet().stream() + .filter(e -> e.getKey().getSimpleName().contentEquals("value")) + .map(e -> MoreAnnotations.asStrings((AnnotationValue) e.getValue())) + .findAny() + .orElse(Stream.of(attribute.type.tsym.getQualifiedName().toString())); } public static boolean isSuper(Tree tree) {