Search in sources :

Example 1 with JavacScope

use of com.sun.tools.javac.api.JavacScope in project bazel by bazelbuild.

the class Resolver method findClass.

/**
     * Finds the class literal with name {@code name}.
     *
     * <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 class.
     * @param path
     *            The tree path to the local scope.
     * @return The element for the class.
     */
public Element findClass(String name, TreePath path) {
    Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
    try {
        JavacScope scope = (JavacScope) trees.getScope(path);
        Env<AttrContext> env = scope.getEnv();
        return wrapInvocation(FIND_TYPE, env, names.fromString(name));
    } finally {
        log.popDiagnosticHandler(discardDiagnosticHandler);
    }
}
Also used : Log(com.sun.tools.javac.util.Log) JavacScope(com.sun.tools.javac.api.JavacScope) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 2 with JavacScope

use of com.sun.tools.javac.api.JavacScope in project bazel by bazelbuild.

the class Resolver method findMethod.

/**
     * Finds the method element for a given name and list of expected parameter
     * types.
     *
     * <p>
     * The method adheres to all the rules of Java's scoping (while also
     * considering the imports) for name resolution.
     *
     * @param methodName
     *            Name of the method to find.
     * @param receiverType
     *            Type of the receiver of the method
     * @param path
     *            Tree path.
     * @return The method element (if found).
     */
public Element findMethod(String methodName, TypeMirror receiverType, TreePath path, java.util.List<TypeMirror> argumentTypes) {
    Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
    try {
        JavacScope scope = (JavacScope) trees.getScope(path);
        Env<AttrContext> env = scope.getEnv();
        Type site = (Type) receiverType;
        Name name = names.fromString(methodName);
        List<Type> argtypes = List.nil();
        for (TypeMirror a : argumentTypes) {
            argtypes = argtypes.append((Type) a);
        }
        List<Type> typeargtypes = List.nil();
        boolean allowBoxing = true;
        boolean useVarargs = false;
        boolean operator = true;
        try {
            // For some reason we have to set our own method context, which is rather ugly.
            // TODO: find a nicer way to do this.
            Object methodContext = buildMethodContext();
            Object oldContext = getField(resolve, "currentResolutionContext");
            setField(resolve, "currentResolutionContext", methodContext);
            Element result = wrapInvocation(FIND_METHOD, env, site, name, argtypes, typeargtypes, allowBoxing, useVarargs, operator);
            setField(resolve, "currentResolutionContext", oldContext);
            return result;
        } catch (Throwable t) {
            Error err = new AssertionError("Unexpected Reflection error");
            err.initCause(t);
            throw err;
        }
    } finally {
        log.popDiagnosticHandler(discardDiagnosticHandler);
    }
}
Also used : Log(com.sun.tools.javac.util.Log) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) JavacScope(com.sun.tools.javac.api.JavacScope) AttrContext(com.sun.tools.javac.comp.AttrContext) Name(com.sun.tools.javac.util.Name) Type(com.sun.tools.javac.code.Type) TypeMirror(javax.lang.model.type.TypeMirror)

Example 3 with JavacScope

use of com.sun.tools.javac.api.JavacScope 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);
    }
}
Also used : Log(com.sun.tools.javac.util.Log) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) JavacScope(com.sun.tools.javac.api.JavacScope) AttrContext(com.sun.tools.javac.comp.AttrContext)

Aggregations

JavacScope (com.sun.tools.javac.api.JavacScope)3 AttrContext (com.sun.tools.javac.comp.AttrContext)3 Log (com.sun.tools.javac.util.Log)3 Element (javax.lang.model.element.Element)2 VariableElement (javax.lang.model.element.VariableElement)2 Type (com.sun.tools.javac.code.Type)1 Name (com.sun.tools.javac.util.Name)1 TypeMirror (javax.lang.model.type.TypeMirror)1