Skip to content

Commit

Permalink
Make ASTHelpers.getSymbol(MethodInvocationTree) and friends throw i…
Browse files Browse the repository at this point in the history
…nstead of return `null`.

Fixes #2882

PiperOrigin-RevId: 424097880
  • Loading branch information
cpovirk authored and Error Prone Team committed Jan 25, 2022
1 parent c18ae52 commit c2e14f2
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,13 @@ public static MethodSymbol getSymbol(MethodTree tree) {
}

/** Gets the method symbol for a new class. */
@Nullable
public static MethodSymbol getSymbol(NewClassTree tree) {
Symbol sym = ((JCNewClass) tree).constructor;
return sym instanceof MethodSymbol ? (MethodSymbol) sym : null;
if (!(sym instanceof MethodSymbol)) {
// Defensive. Would only occur if there are errors in the AST.
throw new IllegalArgumentException(tree.toString());
}
return (MethodSymbol) sym;
}

/** Gets the symbol for a variable. */
Expand All @@ -309,21 +312,23 @@ public static VarSymbol getSymbol(VariableTree tree) {
}

/** Gets the symbol for a method invocation. */
@Nullable
public static MethodSymbol getSymbol(MethodInvocationTree tree) {
Symbol sym = ASTHelpers.getSymbol(tree.getMethodSelect());
if (!(sym instanceof MethodSymbol)) {
// Defensive. Would only occur if there are errors in the AST.
return null;
throw new IllegalArgumentException(tree.toString());
}
return (MethodSymbol) sym;
}

/** Gets the symbol for a member reference. */
@Nullable
public static MethodSymbol getSymbol(MemberReferenceTree tree) {
Symbol sym = ((JCMemberReference) tree).sym;
return sym instanceof MethodSymbol ? (MethodSymbol) sym : null;
if (!(sym instanceof MethodSymbol)) {
// Defensive. Would only occur if there are errors in the AST.
throw new IllegalArgumentException(tree.toString());
}
return (MethodSymbol) sym;
}

/* Checks whether an expression requires parentheses. */
Expand Down

0 comments on commit c2e14f2

Please sign in to comment.