Skip to content

Commit

Permalink
Accept any annotation with the simple name @Generated
Browse files Browse the repository at this point in the history
to support non-standard annotations.

Fixes #1984, #1863

PiperOrigin-RevId: 352622403
  • Loading branch information
cushon authored and Error Prone Team committed Jan 19, 2021
1 parent b84b80c commit 4d6a0cc
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -1359,30 +1359,23 @@ public static ImmutableSet<String> 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<String> getGeneratedBy(Symbol symbol, VisitorState state) {
checkNotNull(symbol);
Optional<Compound> 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<Attribute> 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<String> 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) {
Expand Down

0 comments on commit 4d6a0cc

Please sign in to comment.