From ceecbd47b18d668bf1cb2d4b75138a9e49e8d77c Mon Sep 17 00:00:00 2001 From: Sumit Bhagwani Date: Thu, 13 Jan 2022 19:42:19 -0800 Subject: [PATCH] memoize constant VisitorState lookups RELNOTES=N/A PiperOrigin-RevId: 421717840 --- .../CorrespondenceSubclassToFactoryCall.java | 11 +++++++--- .../refactorings/StoreActualValueInField.java | 21 ++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/refactorings/src/main/java/com/google/common/truth/refactorings/CorrespondenceSubclassToFactoryCall.java b/refactorings/src/main/java/com/google/common/truth/refactorings/CorrespondenceSubclassToFactoryCall.java index 76a855022..84e1a74b4 100644 --- a/refactorings/src/main/java/com/google/common/truth/refactorings/CorrespondenceSubclassToFactoryCall.java +++ b/refactorings/src/main/java/com/google/common/truth/refactorings/CorrespondenceSubclassToFactoryCall.java @@ -58,6 +58,7 @@ import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher; import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; +import com.google.errorprone.suppliers.Supplier; import com.sun.source.tree.ClassTree; import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.ExpressionStatementTree; @@ -94,6 +95,12 @@ severity = SUGGESTION) public final class CorrespondenceSubclassToFactoryCall extends BugChecker implements ClassTreeMatcher { + + private static final String CORRESPONDENCE_CLASS = "com.google.common.truth.Correspondence"; + + private static final Supplier COM_GOOGLE_COMMON_TRUTH_CORRESPONDENCE = + VisitorState.memoize(state -> state.getTypeFromString(CORRESPONDENCE_CLASS)); + @Override public Description matchClass(ClassTree tree, VisitorState state) { if (!isCorrespondence(tree.getExtendsClause(), state)) { @@ -646,13 +653,11 @@ private static boolean overrides( } private static boolean isCorrespondence(Tree supertypeTree, VisitorState state) { - Type correspondenceType = state.getTypeFromString(CORRESPONDENCE_CLASS); + Type correspondenceType = COM_GOOGLE_COMMON_TRUTH_CORRESPONDENCE.get(state); if (correspondenceType == null) { return false; } return supertypeTree != null && state.getTypes().isSameType(getSymbol(supertypeTree).type, correspondenceType); } - - private static final String CORRESPONDENCE_CLASS = "com.google.common.truth.Correspondence"; } diff --git a/refactorings/src/main/java/com/google/common/truth/refactorings/StoreActualValueInField.java b/refactorings/src/main/java/com/google/common/truth/refactorings/StoreActualValueInField.java index 17cf4e582..08ba08533 100644 --- a/refactorings/src/main/java/com/google/common/truth/refactorings/StoreActualValueInField.java +++ b/refactorings/src/main/java/com/google/common/truth/refactorings/StoreActualValueInField.java @@ -44,6 +44,7 @@ import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Matcher; +import com.google.errorprone.suppliers.Supplier; import com.sun.source.tree.ClassTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.IdentifierTree; @@ -75,6 +76,17 @@ severity = SUGGESTION) public final class StoreActualValueInField extends BugChecker implements MethodInvocationTreeMatcher { + + private static final Supplier COM_GOOGLE_COMMON_TRUTH_SUBJECT_SYMBOL = + VisitorState.memoize(state -> state.getSymbolFromString("com.google.common.truth.Subject")); + + private static final Supplier COM_GOOGLE_COMMON_TRUTH_FAILUREMETADATA = + VisitorState.memoize( + state -> state.getTypeFromString("com.google.common.truth.FailureMetadata")); + + private static final Supplier COM_GOOGLE_COMMON_TRUTH_SUBJECT_TYPE = + VisitorState.memoize(state -> state.getTypeFromString("com.google.common.truth.Subject")); + @Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { ClassTree enclosingClass = state.findEnclosing(ClassTree.class); @@ -144,7 +156,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState } private static String qualifierForThis(VisitorState state) { - Type subjectBaseType = state.getTypeFromString("com.google.common.truth.Subject"); + Type subjectBaseType = COM_GOOGLE_COMMON_TRUTH_SUBJECT_TYPE.get(state); boolean seenClassInBetween = false; for (Tree t : state.getPath()) { @@ -211,10 +223,10 @@ private static IdentifierTree findActualArg( Type actualType = extractTypeArgAsMemberOfSupertype( getType(state.findEnclosing(ClassTree.class)), - state.getSymbolFromString("com.google.common.truth.Subject"), + COM_GOOGLE_COMMON_TRUTH_SUBJECT_SYMBOL.get(state), 1, state.getTypes()); - Type failureMetadataType = state.getTypeFromString("com.google.common.truth.FailureMetadata"); + Type failureMetadataType = COM_GOOGLE_COMMON_TRUTH_FAILUREMETADATA.get(state); ImmutableSet candidates = args.stream() .flatMap(a -> maybeToIdentifier(a, state)) @@ -264,8 +276,7 @@ private static Tree findActualFormalType(Name name, VisitorState state) { constructor() .forClass( (type, state) -> - isSubtype( - type, state.getTypeFromString("com.google.common.truth.Subject"), state)); + isSubtype(type, COM_GOOGLE_COMMON_TRUTH_SUBJECT_TYPE.get(state), state)); private static final Matcher ACTUAL_METHOD = anyOf( instanceMethod()