Skip to content

Commit

Permalink
Symbol.isLocal was renamed in JDK 16
Browse files Browse the repository at this point in the history
Fixes #2105

PiperOrigin-RevId: 351903127
  • Loading branch information
cushon authored and Error Prone Team committed Jan 15, 2021
1 parent fb6d049 commit 7a65117
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public static String qualifyType(VisitorState state, SuggestedFix.Builder fix, S
return sym.getSimpleName().toString();
}
if (sym.getKind() == ElementKind.CLASS) {
if (sym.isLocal()) {
if (ASTHelpers.isLocal(sym)) {
if (!sym.isAnonymous()) {
return sym.getSimpleName().toString();
}
Expand Down
28 changes: 28 additions & 0 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
import com.sun.tools.javac.util.Name;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.URI;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -2133,4 +2134,31 @@ public static boolean isBugCheckerCode(VisitorState state) {
}
return false;
}

private static final Method IS_LOCAL = getIsLocal();

private static Method getIsLocal() {
try {
return Symbol.class.getMethod("isLocal");
} catch (NoSuchMethodException e) {
// continue below
}
try {
return Symbol.class.getMethod("isDirectlyOrIndirectlyLocal");
} catch (NoSuchMethodException e) {
throw new LinkageError(e.getMessage(), e);
}
}

/**
* Returns true if the symbol is directly or indirectly local to a method or variable initializer;
* see [@code Symbol#isLocal} or {@code Symbol#isDirectlyOrIndirectlyLocal}.
*/
public static boolean isLocal(Symbol symbol) {
try {
return (boolean) IS_LOCAL.invoke(symbol);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private static boolean hasMethodParameter(TreePath path, ExpressionTree tree) {
Set<Symbol> symbols = new HashSet<>();
for (IdentifierTree ident : getVariableUses(tree)) {
Symbol sym = ASTHelpers.getSymbol(ident);
if (sym.isLocal()) {
if (ASTHelpers.isLocal(sym)) {
symbols.add(sym);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public Description matchSynchronized(SynchronizedTree tree, VisitorState state)
// TODO(cushon): check that the receiver doesn't contain mutable state.
// Currently 'this.locks[i].mu' is accepted if 'mu' is final but 'locks' is non-final.
VarSymbol varSymbol = (VarSymbol) symbol;
if (varSymbol.isLocal() || varSymbol.isStatic() || (varSymbol.flags() & Flags.FINAL) != 0) {
if (ASTHelpers.isLocal(varSymbol)
|| varSymbol.isStatic()
|| (varSymbol.flags() & Flags.FINAL) != 0) {
return NO_MATCH;
}
if (ASTHelpers.hasAnnotation(varSymbol, LazyInit.class, state)) {
Expand Down

0 comments on commit 7a65117

Please sign in to comment.