Skip to content

Commit

Permalink
Fix findIdent incompatibility with Java 13
Browse files Browse the repository at this point in the history
Fixes #1432
Fixes #1439

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=308794942
  • Loading branch information
michaelhixson authored and cpovirk committed Apr 28, 2020
1 parent 3d1228d commit af9e57c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AttrContext> 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) {
Expand Down Expand Up @@ -246,7 +262,7 @@ public static ImmutableSet<VarSymbol> findAllIdents(VisitorState state) {
}

return result.build().stream()
.filter(var -> isVisible(var, state.getPath()))
.filter(variable -> isVisible(variable, state.getPath()))
.collect(toImmutableSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit af9e57c

Please sign in to comment.