Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept any annotation with the simple name @Generated #2125

Merged
merged 1 commit into from
Jan 19, 2021
Merged
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
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()));
Comment on lines +1376 to +1378
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late to the party, but would suggest:

Suggested change
.map(e -> MoreAnnotations.asStrings((AnnotationValue) e.getValue()))
.findAny()
.orElse(Stream.of(attribute.type.tsym.getQualifiedName().toString()));
.findFirst()
.map(e -> MoreAnnotations.asStrings((AnnotationValue) e.getValue()))
.orElseGet(() -> Stream.of(attribute.type.tsym.getQualifiedName().toString()));
  • .firstFirst just because that's what happens in practice in this non-parallel stream, and is thus arguably clearer than .findAny.
  • .map after .findFirst to clarify that only one map operation will be performed.
  • .orElseGet rather than .orElse for efficiency.

}

public static boolean isSuper(Tree tree) {
Expand Down