diff --git a/check_api/pom.xml b/check_api/pom.xml index 92f1f8f2ce4..930f792bfdd 100644 --- a/check_api/pom.xml +++ b/check_api/pom.xml @@ -15,7 +15,9 @@ limitations under the License. --> - + 4.0.0 + com.github.ben-manes.caffeine + caffeine + 2.2.6 + 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 58c11698640..be3fef958d6 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 @@ -22,6 +22,8 @@ import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE; import static java.util.Objects.requireNonNull; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import com.google.auto.value.AutoValue; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.CharMatcher; @@ -677,19 +679,27 @@ public static boolean hasAnnotation(Symbol sym, String annotationClass, VisitorS return false; } + private static final Cache inheritedAnnotationCache = + Caffeine.newBuilder().maximumSize(1000).build(); + + @SuppressWarnings("ConstantConditions") // IntelliJ worries unboxing our Boolean may throw NPE. private static boolean isInherited(VisitorState state, String annotationName) { - Symbol annotationSym = state.getSymbolFromString(annotationName); - if (annotationSym == null) { - return false; - } - try { - annotationSym.complete(); - } catch (CompletionFailure e) { - // @Inherited won't work if the annotation isn't on the classpath, but we can still check - // if it's present directly - } - Symbol inheritedSym = state.getSymtab().inheritedType.tsym; - return annotationSym.attribute(inheritedSym) != null; + return inheritedAnnotationCache.get( + annotationName, + name -> { + Symbol annotationSym = state.getSymbolFromString(name); + if (annotationSym == null) { + return false; + } + try { + annotationSym.complete(); + } catch (CompletionFailure e) { + /* @Inherited won't work if the annotation isn't on the classpath, but we can still + check if it's present directly */ + } + Symbol inheritedSym = state.getSymtab().inheritedType.tsym; + return annotationSym.attribute(inheritedSym) != null; + }); } private static boolean hasAttribute(Symbol sym, Name annotationName) {