aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java')
-rw-r--r--third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java168
1 files changed, 18 insertions, 150 deletions
diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java
index 45d959fe02..c47566bfa2 100644
--- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java
+++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java
@@ -1,7 +1,5 @@
package org.checkerframework.javacutil;
-import static com.sun.tools.javac.code.Kinds.PCK;
-import static com.sun.tools.javac.code.Kinds.TYP;
import static com.sun.tools.javac.code.Kinds.VAR;
import java.lang.reflect.Constructor;
@@ -19,8 +17,6 @@ import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacScope;
import com.sun.tools.javac.code.Symbol;
-import com.sun.tools.javac.code.Symbol.ClassSymbol;
-import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.comp.AttrContext;
@@ -43,32 +39,24 @@ public class Resolver {
private final Trees trees;
private final Log log;
- private static final Method FIND_METHOD;
- private static final Method FIND_VAR;
- private static final Method FIND_IDENT;
- private static final Method FIND_IDENT_IN_TYPE;
- private static final Method FIND_IDENT_IN_PACKAGE;
- private static final Method FIND_TYPE;
+ private final Method FIND_METHOD;
+ private final Method FIND_IDENT_IN_TYPE;
+ private final Method FIND_IDENT_IN_PACKAGE;
+ private final Method FIND_TYPE;
- private static final Class<?> ACCESSERROR;
- // Note that currently access(...) is defined in InvalidSymbolError, a superclass of AccessError
- private static final Method ACCESSERROR_ACCESS;
+ public Resolver(ProcessingEnvironment env) {
+ Context context = ((JavacProcessingEnvironment) env).getContext();
+ this.resolve = Resolve.instance(context);
+ this.names = Names.instance(context);
+ this.trees = Trees.instance(env);
+ this.log = Log.instance(context);
- static {
try {
FIND_METHOD = Resolve.class.getDeclaredMethod("findMethod",
Env.class, Type.class, Name.class, List.class, List.class,
boolean.class, boolean.class, boolean.class);
FIND_METHOD.setAccessible(true);
- FIND_VAR = Resolve.class.getDeclaredMethod("findVar",
- Env.class, Name.class);
- FIND_VAR.setAccessible(true);
-
- FIND_IDENT = Resolve.class.getDeclaredMethod(
- "findIdent", Env.class, Name.class, int.class);
- FIND_IDENT.setAccessible(true);
-
FIND_IDENT_IN_TYPE = Resolve.class.getDeclaredMethod(
"findIdentInType", Env.class, Type.class, Name.class,
int.class);
@@ -88,60 +76,6 @@ public class Resolver {
err.initCause(e);
throw err;
}
-
- try {
- ACCESSERROR = Class.forName("com.sun.tools.javac.comp.Resolve$AccessError");
- ACCESSERROR_ACCESS = ACCESSERROR.getMethod("access", Name.class, TypeSymbol.class);
- ACCESSERROR_ACCESS.setAccessible(true);
- } catch (ClassNotFoundException e) {
- ErrorReporter.errorAbort("Compiler 'Resolve$AccessError' class could not be retrieved.", e);
- // Unreachable code - needed so the compiler does not warn about a possibly uninitialized final field.
- throw new AssertionError();
- } catch (NoSuchMethodException e) {
- ErrorReporter.errorAbort("Compiler 'Resolve$AccessError' class doesn't contain required 'access' method", e);
- // Unreachable code - needed so the compiler does not warn about a possibly uninitialized final field.
- throw new AssertionError();
- }
- }
-
- public Resolver(ProcessingEnvironment env) {
- Context context = ((JavacProcessingEnvironment) env).getContext();
- this.resolve = Resolve.instance(context);
- this.names = Names.instance(context);
- this.trees = Trees.instance(env);
- this.log = Log.instance(context);
- }
-
- /**
- * Finds the package with name {@code name}.
- *
- * @param name
- * The name of the package.
- * @param path
- * The tree path to the local scope.
- * @return the {@code PackageSymbol} for the package if it is found,
- * {@code null} otherwise
- */
- public PackageSymbol findPackage(String name, TreePath path) {
- Log.DiagnosticHandler discardDiagnosticHandler =
- new Log.DiscardDiagnosticHandler(log);
- try {
- JavacScope scope = (JavacScope) trees.getScope(path);
- Env<AttrContext> env = scope.getEnv();
- Element res = wrapInvocationOnResolveInstance(FIND_IDENT, env,
- names.fromString(name), PCK);
- // findIdent will return a PackageSymbol even for a symbol that is not a package,
- // such as a.b.c.MyClass.myStaticField. "exists()" must be called on it to ensure
- // that it exists.
- if (res.getKind() == ElementKind.PACKAGE) {
- PackageSymbol ps = (PackageSymbol) res;
- return ps.exists() ? ps : null;
- } else {
- return null;
- }
- } finally {
- log.popDiagnosticHandler(discardDiagnosticHandler);
- }
}
/**
@@ -158,7 +92,7 @@ public class Resolver {
* the field).
* @param path
* The tree path to the local scope.
- * @return the element for the field
+ * @return The element for the field.
*/
public VariableElement findField(String name, TypeMirror type, TreePath path) {
Log.DiagnosticHandler discardDiagnosticHandler =
@@ -166,13 +100,10 @@ public class Resolver {
try {
JavacScope scope = (JavacScope) trees.getScope(path);
Env<AttrContext> env = scope.getEnv();
- Element res = wrapInvocationOnResolveInstance(FIND_IDENT_IN_TYPE, env, type,
+ Element res = wrapInvocation(FIND_IDENT_IN_TYPE, env, type,
names.fromString(name), VAR);
if (res.getKind() == ElementKind.FIELD) {
return (VariableElement) res;
- } else if (res.getKind() == ElementKind.OTHER && ACCESSERROR.isInstance(res)) {
- // Return the inaccessible field that was found
- return (VariableElement) wrapInvocation(res, ACCESSERROR_ACCESS, null, null);
} else {
// Most likely didn't find the field and the Element is a SymbolNotFoundError
return null;
@@ -183,35 +114,6 @@ public class Resolver {
}
/**
- * Finds the local variable with name {@code name} in the given scope.
- *
- * @param name
- * The name of the local variable.
- * @param path
- * The tree path to the local scope.
- * @return the element for the local variable
- */
- public VariableElement findLocalVariableOrParameter(String name, TreePath path) {
- Log.DiagnosticHandler discardDiagnosticHandler =
- new Log.DiscardDiagnosticHandler(log);
- try {
- JavacScope scope = (JavacScope) trees.getScope(path);
- Env<AttrContext> env = scope.getEnv();
- Element res = wrapInvocationOnResolveInstance(FIND_VAR, env,
- names.fromString(name));
- if (res.getKind() == ElementKind.LOCAL_VARIABLE
- || res.getKind() == ElementKind.PARAMETER) {
- return (VariableElement) res;
- } else {
- // Most likely didn't find the variable and the Element is a SymbolNotFoundError
- return null;
- }
- } finally {
- log.popDiagnosticHandler(discardDiagnosticHandler);
- }
- }
-
- /**
* Finds the class literal with name {@code name}.
*
* <p>
@@ -222,7 +124,7 @@ public class Resolver {
* The name of the class.
* @param path
* The tree path to the local scope.
- * @return the element for the class
+ * @return The element for the class.
*/
public Element findClass(String name, TreePath path) {
Log.DiagnosticHandler discardDiagnosticHandler =
@@ -230,37 +132,7 @@ public class Resolver {
try {
JavacScope scope = (JavacScope) trees.getScope(path);
Env<AttrContext> env = scope.getEnv();
- return wrapInvocationOnResolveInstance(FIND_TYPE, env, names.fromString(name));
- } finally {
- log.popDiagnosticHandler(discardDiagnosticHandler);
- }
- }
-
- /**
- * Finds the class with name {@code name} in a given package.
- *
- * @param name
- * The name of the class.
- * @param pck
- * The PackageSymbol for the package.
- * @param path
- * The tree path to the local scope.
- * @return the {@code ClassSymbol} for the class if it is found,
- * {@code null} otherwise
- */
- public ClassSymbol findClassInPackage(String name, PackageSymbol pck, TreePath path) {
- Log.DiagnosticHandler discardDiagnosticHandler =
- new Log.DiscardDiagnosticHandler(log);
- try {
- JavacScope scope = (JavacScope) trees.getScope(path);
- Env<AttrContext> env = scope.getEnv();
- Element res = wrapInvocationOnResolveInstance(FIND_IDENT_IN_PACKAGE, env, pck,
- names.fromString(name), TYP);
- if (res.getKind() == ElementKind.CLASS) {
- return (ClassSymbol) res;
- } else {
- return null;
- }
+ return wrapInvocation(FIND_TYPE, env, names.fromString(name));
} finally {
log.popDiagnosticHandler(discardDiagnosticHandler);
}
@@ -280,7 +152,7 @@ public class Resolver {
* Type of the receiver of the method
* @param path
* Tree path.
- * @return the method element (if found)
+ * @return The method element (if found).
*/
public Element findMethod(String methodName, TypeMirror receiverType,
TreePath path, java.util.List<TypeMirror> argumentTypes) {
@@ -307,7 +179,7 @@ public class Resolver {
Object methodContext = buildMethodContext();
Object oldContext = getField(resolve, "currentResolutionContext");
setField(resolve, "currentResolutionContext", methodContext);
- Element result = wrapInvocationOnResolveInstance(FIND_METHOD, env, site, name, argtypes,
+ Element result = wrapInvocation(FIND_METHOD, env, site, name, argtypes,
typeargtypes, allowBoxing, useVarargs, operator);
setField(resolve, "currentResolutionContext", oldContext);
return result;
@@ -357,13 +229,9 @@ public class Resolver {
return f.get(receiver);
}
- private Symbol wrapInvocationOnResolveInstance(Method method, Object... args) {
- return wrapInvocation(resolve, method, args);
- }
-
- private Symbol wrapInvocation(Object receiver, Method method, Object... args) {
+ private Symbol wrapInvocation(Method method, Object... args) {
try {
- return (Symbol) method.invoke(receiver, args);
+ return (Symbol) method.invoke(resolve, args);
} catch (IllegalAccessException e) {
Error err = new AssertionError("Unexpected Reflection error");
err.initCause(e);