Skip to content

Commit c2e14f2

Browse files
cpovirkError Prone Team
authored and
Error Prone Team
committed
Make ASTHelpers.getSymbol(MethodInvocationTree) and friends throw instead of return null.
Fixes #2882 PiperOrigin-RevId: 424097880
1 parent c18ae52 commit c2e14f2

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,13 @@ public static MethodSymbol getSymbol(MethodTree tree) {
297297
}
298298

299299
/** Gets the method symbol for a new class. */
300-
@Nullable
301300
public static MethodSymbol getSymbol(NewClassTree tree) {
302301
Symbol sym = ((JCNewClass) tree).constructor;
303-
return sym instanceof MethodSymbol ? (MethodSymbol) sym : null;
302+
if (!(sym instanceof MethodSymbol)) {
303+
// Defensive. Would only occur if there are errors in the AST.
304+
throw new IllegalArgumentException(tree.toString());
305+
}
306+
return (MethodSymbol) sym;
304307
}
305308

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

311314
/** Gets the symbol for a method invocation. */
312-
@Nullable
313315
public static MethodSymbol getSymbol(MethodInvocationTree tree) {
314316
Symbol sym = ASTHelpers.getSymbol(tree.getMethodSelect());
315317
if (!(sym instanceof MethodSymbol)) {
316318
// Defensive. Would only occur if there are errors in the AST.
317-
return null;
319+
throw new IllegalArgumentException(tree.toString());
318320
}
319321
return (MethodSymbol) sym;
320322
}
321323

322324
/** Gets the symbol for a member reference. */
323-
@Nullable
324325
public static MethodSymbol getSymbol(MemberReferenceTree tree) {
325326
Symbol sym = ((JCMemberReference) tree).sym;
326-
return sym instanceof MethodSymbol ? (MethodSymbol) sym : null;
327+
if (!(sym instanceof MethodSymbol)) {
328+
// Defensive. Would only occur if there are errors in the AST.
329+
throw new IllegalArgumentException(tree.toString());
330+
}
331+
return (MethodSymbol) sym;
327332
}
328333

329334
/* Checks whether an expression requires parentheses. */

0 commit comments

Comments
 (0)