Skip to content

Commit 3ac0471

Browse files
cushonError Prone Team
authored andcommitted
Fix a crash in BanNashorn
PiperOrigin-RevId: 441076150
1 parent 58ffc26 commit 3ac0471

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,15 +1160,22 @@ public static ClassSymbol enclosingClass(Symbol sym) {
11601160
}
11611161

11621162
/** Return the enclosing {@code PackageSymbol} of the given symbol, or {@code null}. */
1163+
@Nullable
11631164
public static PackageSymbol enclosingPackage(Symbol sym) {
1164-
return sym.packge();
1165+
Symbol curr = sym;
1166+
for (; curr != null && curr.owner != null; curr = curr.owner) {
1167+
if (curr.getKind().equals(ElementKind.PACKAGE)) {
1168+
return (PackageSymbol) curr;
1169+
}
1170+
}
1171+
return null;
11651172
}
11661173

11671174
/** Return true if the given symbol is defined in the current package. */
11681175
public static boolean inSamePackage(Symbol targetSymbol, VisitorState state) {
11691176
JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit();
11701177
PackageSymbol usePackage = compilationUnit.packge;
1171-
PackageSymbol targetPackage = targetSymbol.packge();
1178+
PackageSymbol targetPackage = enclosingPackage(targetSymbol);
11721179

11731180
return targetPackage != null
11741181
&& usePackage != null

0 commit comments

Comments
 (0)