Skip to content

Commit

Permalink
Strongly type a parameter as a MethodSymbol to make it a bit clearer …
Browse files Browse the repository at this point in the history
…what it refers to.

PiperOrigin-RevId: 441134556
  • Loading branch information
graememorgan authored and Error Prone Team committed Apr 14, 2022
1 parent 5d647de commit 3613731
Showing 1 changed file with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.util.ASTHelpers.getSymbol;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -49,6 +50,7 @@
import com.sun.source.tree.TypeParameterTree;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree.JCMemberReference;
Expand Down Expand Up @@ -90,29 +92,26 @@ private ImmutableChecker(ErrorProneFlags flags, ImmutableSet<String> immutableAn
// check instantiations of `@ImmutableTypeParameter`s in method references
@Override
public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) {
checkInvocation(
tree, ((JCMemberReference) tree).referentType, state, ASTHelpers.getSymbol(tree));
checkInvocation(tree, getSymbol(tree), ((JCMemberReference) tree).referentType, state);
return NO_MATCH;
}

// check instantiations of `@ImmutableTypeParameter`s in method invocations
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
checkInvocation(
tree, ASTHelpers.getType(tree.getMethodSelect()), state, ASTHelpers.getSymbol(tree));
checkInvocation(tree, getSymbol(tree), ASTHelpers.getType(tree.getMethodSelect()), state);
return NO_MATCH;
}

@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
// check instantiations of `@ImmutableTypeParameter`s in generic constructor invocations
checkInvocation(
tree, ((JCNewClass) tree).constructorType, state, ((JCNewClass) tree).constructor);
checkInvocation(tree, getSymbol(tree), ((JCNewClass) tree).constructorType, state);
// check instantiations of `@ImmutableTypeParameter`s in class constructor invocations
checkInstantiation(
tree,
state,
ASTHelpers.getSymbol(tree.getIdentifier()).getTypeParameters(),
getSymbol(tree.getIdentifier()).getTypeParameters(),
ASTHelpers.getType(tree).getTypeArguments());

ClassTree classBody = tree.getClassBody();
Expand All @@ -132,7 +131,7 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
checkInstantiation(
tree,
state,
ASTHelpers.getSymbol(tree).getTypeParameters(),
getSymbol(tree).getTypeParameters(),
ASTHelpers.getType(tree).getTypeArguments());
return NO_MATCH;
}
Expand All @@ -141,7 +140,8 @@ private ImmutableAnalysis createImmutableAnalysis(VisitorState state) {
return new ImmutableAnalysis(this, state, wellKnownMutability, immutableAnnotations);
}

private void checkInvocation(Tree tree, Type methodType, VisitorState state, Symbol symbol) {
private void checkInvocation(
Tree tree, MethodSymbol symbol, Type methodType, VisitorState state) {
ImmutableAnalysis analysis = createImmutableAnalysis(state);
Violation info = analysis.checkInvocation(methodType, symbol);
if (info.isPresent()) {
Expand Down Expand Up @@ -197,8 +197,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
// Check that the types in containerOf actually exist
Map<String, TypeVariableSymbol> typarams = new HashMap<>();
for (TypeParameterTree typaram : tree.getTypeParameters()) {
typarams.put(
typaram.getName().toString(), (TypeVariableSymbol) ASTHelpers.getSymbol(typaram));
typarams.put(typaram.getName().toString(), (TypeVariableSymbol) getSymbol(typaram));
}
SetView<String> difference = Sets.difference(annotation.containerOf(), typarams.keySet());
if (!difference.isEmpty()) {
Expand Down Expand Up @@ -231,12 +230,12 @@ public Description matchClass(ClassTree tree, VisitorState state) {
// Check that the fields (including inherited fields) are immutable, and
// validate the type hierarchy superclass.

ClassSymbol sym = ASTHelpers.getSymbol(tree);
ClassSymbol sym = getSymbol(tree);

Violation info =
analysis.checkForImmutability(
Optional.of(tree),
immutableTypeParametersInScope(ASTHelpers.getSymbol(tree), state, analysis),
immutableTypeParametersInScope(getSymbol(tree), state, analysis),
ASTHelpers.getType(tree),
(Tree matched, Violation violation) ->
describeClass(matched, sym, annotation, violation));
Expand All @@ -255,7 +254,7 @@ private void checkClassTreeInstantiation(
tree,
state,
analysis,
ASTHelpers.getSymbol(implementTree).getTypeParameters(),
getSymbol(implementTree).getTypeParameters(),
ASTHelpers.getType(implementTree).getTypeArguments());
}

Expand All @@ -265,7 +264,7 @@ private void checkClassTreeInstantiation(
tree,
state,
analysis,
ASTHelpers.getSymbol(extendsClause).getTypeParameters(),
getSymbol(extendsClause).getTypeParameters(),
ASTHelpers.getType(extendsClause).getTypeArguments());
}
}
Expand All @@ -289,7 +288,7 @@ private Description.Builder describeClass(
/** Check anonymous implementations of {@code @Immutable} types. */
private Description handleAnonymousClass(
ClassTree tree, VisitorState state, ImmutableAnalysis analysis) {
ClassSymbol sym = ASTHelpers.getSymbol(tree);
ClassSymbol sym = getSymbol(tree);
Type superType = immutableSupertype(sym, state);
if (superType == null) {
return NO_MATCH;
Expand Down Expand Up @@ -334,7 +333,7 @@ private Description.Builder describeAnonymous(Tree tree, Type superType, Violati

/** Check for classes without {@code @Immutable} that have immutable supertypes. */
private Description checkSubtype(ClassTree tree, VisitorState state) {
ClassSymbol sym = ASTHelpers.getSymbol(tree);
ClassSymbol sym = getSymbol(tree);
Type superType = immutableSupertype(sym, state);
if (superType == null) {
return NO_MATCH;
Expand Down

0 comments on commit 3613731

Please sign in to comment.