From af9e57ce9e5161d4dd2fd2eded09848469e836a6 Mon Sep 17 00:00:00 2001 From: Michael Hixson Date: Tue, 28 Apr 2020 03:42:17 -0700 Subject: [PATCH] Fix findIdent incompatibility with Java 13 Fixes #1432 Fixes #1439 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=308794942 --- .../errorprone/util/FindIdentifiers.java | 28 +++++++++++++++---- .../errorprone/util/RuntimeVersion.java | 5 ++++ 2 files changed, 27 insertions(+), 6 deletions(-) rename {core/src/test => check_api/src/main}/java/com/google/errorprone/util/RuntimeVersion.java (93%) diff --git a/check_api/src/main/java/com/google/errorprone/util/FindIdentifiers.java b/check_api/src/main/java/com/google/errorprone/util/FindIdentifiers.java index d1c3a2369c4..97ab128f71f 100644 --- a/check_api/src/main/java/com/google/errorprone/util/FindIdentifiers.java +++ b/check_api/src/main/java/com/google/errorprone/util/FindIdentifiers.java @@ -61,7 +61,9 @@ import com.sun.tools.javac.comp.Resolve; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; import com.sun.tools.javac.util.Name; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collections; import java.util.List; @@ -98,17 +100,31 @@ public static Symbol findIdent(String name, VisitorState state, KindSelector kin } } try { - Method method = - Resolve.class.getDeclaredMethod("findIdent", Env.class, Name.class, KindSelector.class); - method.setAccessible(true); - Symbol result = - (Symbol) method.invoke(Resolve.instance(state.context), env, state.getName(name), kind); + Symbol result = findIdent(name, state, kind, env); return result.exists() ? result : null; } catch (ReflectiveOperationException e) { throw new LinkageError(e.getMessage(), e); } } + // Signature was changed in Java 13: https://bugs.openjdk.java.net/browse/JDK-8223305 + private static Symbol findIdent( + String name, VisitorState state, KindSelector kind, Env env) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + if (RuntimeVersion.isAtLeast13()) { + Method method = + Resolve.class.getDeclaredMethod( + "findIdent", DiagnosticPosition.class, Env.class, Name.class, KindSelector.class); + method.setAccessible(true); + return (Symbol) + method.invoke(Resolve.instance(state.context), null, env, state.getName(name), kind); + } + Method method = + Resolve.class.getDeclaredMethod("findIdent", Env.class, Name.class, KindSelector.class); + method.setAccessible(true); + return (Symbol) method.invoke(Resolve.instance(state.context), env, state.getName(name), kind); + } + @Nullable private static ClassTree getEnclosingClass(TreePath treePath) { while (treePath != null) { @@ -246,7 +262,7 @@ public static ImmutableSet findAllIdents(VisitorState state) { } return result.build().stream() - .filter(var -> isVisible(var, state.getPath())) + .filter(variable -> isVisible(variable, state.getPath())) .collect(toImmutableSet()); } diff --git a/core/src/test/java/com/google/errorprone/util/RuntimeVersion.java b/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java similarity index 93% rename from core/src/test/java/com/google/errorprone/util/RuntimeVersion.java rename to check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java index faad4ec33f1..372deeba51a 100644 --- a/core/src/test/java/com/google/errorprone/util/RuntimeVersion.java +++ b/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java @@ -64,4 +64,9 @@ public static boolean isAtLeast11() { public static boolean isAtLeast12() { return MAJOR >= 12; } + + /** Returns true if the current runtime is JDK 13 or newer. */ + public static boolean isAtLeast13() { + return MAJOR >= 13; + } }