Search in sources :

Example 1 with Log

use of com.sun.tools.javac.util.Log 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 Log

use of com.sun.tools.javac.util.Log 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 Log

use of com.sun.tools.javac.util.Log in project checker-framework by typetools.

the class Resolver method findLocalVariableOrParameterOrField.

/**
 * 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 findLocalVariableOrParameterOrField(String name, TreePath path) {
    Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
    try {
        Env<AttrContext> env = getEnvForPath(path);
        Element res = wrapInvocationOnResolveInstance(FIND_VAR, env, names.fromString(name));
        if (res.getKind() == ElementKind.LOCAL_VARIABLE || res.getKind() == ElementKind.PARAMETER || res.getKind() == ElementKind.FIELD) {
            return (VariableElement) res;
        } else {
            // Most likely didn't find the variable 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) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 4 with Log

use of com.sun.tools.javac.util.Log in project checker-framework by typetools.

the class Resolver method findClassInPackage.

/**
 * 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 {
        Env<AttrContext> env = getEnvForPath(path);
        Element res = wrapInvocationOnResolveInstance(FIND_IDENT_IN_PACKAGE, env, pck, names.fromString(name), TYP);
        if (res.getKind() == ElementKind.CLASS) {
            return (ClassSymbol) res;
        } else {
            return null;
        }
    } finally {
        log.popDiagnosticHandler(discardDiagnosticHandler);
    }
}
Also used : Log(com.sun.tools.javac.util.Log) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 5 with Log

use of com.sun.tools.javac.util.Log in project checker-framework by typetools.

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 {
        Env<AttrContext> env = getEnvForPath(path);
        Element res = wrapInvocationOnResolveInstance(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;
        }
    } 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) AttrContext(com.sun.tools.javac.comp.AttrContext)

Aggregations

Log (com.sun.tools.javac.util.Log)17 AttrContext (com.sun.tools.javac.comp.AttrContext)8 Element (javax.lang.model.element.Element)7 VariableElement (javax.lang.model.element.VariableElement)7 Context (com.sun.tools.javac.util.Context)6 JavaFileObject (javax.tools.JavaFileObject)4 JavacScope (com.sun.tools.javac.api.JavacScope)3 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)3 JCTree (com.sun.tools.javac.tree.JCTree)3 Type (com.sun.tools.javac.code.Type)2 Parser (com.sun.tools.javac.parser.Parser)2 ParserFactory (com.sun.tools.javac.parser.ParserFactory)2 TreeScanner (com.sun.tools.javac.tree.TreeScanner)2 Name (com.sun.tools.javac.util.Name)2 Options (com.sun.tools.javac.util.Options)2 TypeMirror (javax.lang.model.type.TypeMirror)2 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)2 TreePath (com.sun.source.util.TreePath)1 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1 CompletionFailure (com.sun.tools.javac.code.Symbol.CompletionFailure)1