use of com.sun.tools.javac.util.Log in project bazel by bazelbuild.
the class Resolver method findField.
/**
* Finds the field with name {@code name} in a given type.
*
* <p>
* The method adheres to all the rules of Java's scoping (while also
* considering the imports) for name resolution.
*
* @param name
* The name of the field.
* @param type
* The type of the receiver (i.e., the type in which to look for
* the field).
* @param path
* The tree path to the local scope.
* @return The element for the field.
*/
public VariableElement findField(String name, TypeMirror type, TreePath path) {
Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
try {
JavacScope scope = (JavacScope) trees.getScope(path);
Env<AttrContext> env = scope.getEnv();
Element res = wrapInvocation(FIND_IDENT_IN_TYPE, env, type, names.fromString(name), VAR);
if (res.getKind() == ElementKind.FIELD) {
return (VariableElement) res;
} else {
// Most likely didn't find the field and the Element is a SymbolNotFoundError
return null;
}
} finally {
log.popDiagnosticHandler(discardDiagnosticHandler);
}
}
use of com.sun.tools.javac.util.Log in project error-prone by google.
the class ASTHelpers method resolveExistingMethod.
/**
* Given a Type ({@code base}), find the method named {@code name}, with the appropriate {@code
* argTypes} and {@code tyargTypes} and return its MethodSymbol.
*
* <p>Ex:
*
* <pre>{@code
* .....
* class A {}
* class B {
* public int hashCode() { return 42; }
* }
* .....
*
* MethodSymbol meth = ASTHelpers.resolveExistingMethod(
* state,
* symbol,
* state.getName("hashCode"),
* ImmutableList.<Type>of(),
* ImmutableList.<Type>of());
* }</pre>
*
* {@code meth} could be different MethodSymbol's depending on whether {@code symbol} represented
* {@code B} or {@code A}. (B's hashCode method or Object#hashCode).
*
* <p>Do NOT call this method unless the method you're looking for is guaranteed to exist. A fatal
* error will result otherwise. Note: a method can fail to exist if it was added in a newer
* version of a library (you may be depending on version N of a library which added a method to a
* class, but someone else could depend on version N-1 which didn't have that method).
*
* @return a MethodSymbol representing the method symbol resolved from the context of this type
*/
public static MethodSymbol resolveExistingMethod(VisitorState state, TypeSymbol base, Name name, Iterable<Type> argTypes, Iterable<Type> tyargTypes) {
Resolve resolve = Resolve.instance(state.context);
Enter enter = Enter.instance(state.context);
Log log = Log.instance(state.context);
DeferredDiagnosticHandler handler = new DeferredDiagnosticHandler(log);
try {
return resolve.resolveInternalMethod(/*pos*/
null, enter.getEnv(base), base.type, name, com.sun.tools.javac.util.List.from(argTypes), com.sun.tools.javac.util.List.from(tyargTypes));
} finally {
log.popDiagnosticHandler(handler);
}
}
Aggregations