Skip to content

Commit

Permalink
memoize constant VisitorState lookups
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 421717840
  • Loading branch information
sumitbhagwani authored and Google Java Core Libraries committed Jan 14, 2022
1 parent 42f8bfe commit ceecbd4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Type> 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)) {
Expand Down Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,6 +76,17 @@
severity = SUGGESTION)
public final class StoreActualValueInField extends BugChecker
implements MethodInvocationTreeMatcher {

private static final Supplier<Symbol> COM_GOOGLE_COMMON_TRUTH_SUBJECT_SYMBOL =
VisitorState.memoize(state -> state.getSymbolFromString("com.google.common.truth.Subject"));

private static final Supplier<Type> COM_GOOGLE_COMMON_TRUTH_FAILUREMETADATA =
VisitorState.memoize(
state -> state.getTypeFromString("com.google.common.truth.FailureMetadata"));

private static final Supplier<Type> 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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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<IdentifierTree> candidates =
args.stream()
.flatMap(a -> maybeToIdentifier(a, state))
Expand Down Expand Up @@ -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<ExpressionTree> ACTUAL_METHOD =
anyOf(
instanceMethod()
Expand Down

0 comments on commit ceecbd4

Please sign in to comment.