Search in sources :

Example 21 with Log

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);
    }
}
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)

Example 22 with Log

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).
 *
 * @return a MethodSymbol representing the method symbol resolved from the context of this type,
 *     or {@code null} if the method could not be resolved.
 */
@Nullable
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));
    } catch (FatalError e) {
        // the method could not be resolved
        return null;
    } finally {
        log.popDiagnosticHandler(handler);
    }
}
Also used : FatalError(com.sun.tools.javac.util.FatalError) Log(com.sun.tools.javac.util.Log) Enter(com.sun.tools.javac.comp.Enter) DeferredDiagnosticHandler(com.sun.tools.javac.util.Log.DeferredDiagnosticHandler) Resolve(com.sun.tools.javac.comp.Resolve) Nullable(javax.annotation.Nullable)

Example 23 with Log

use of com.sun.tools.javac.util.Log in project lombok by projectlombok.

the class JavacResolution method resolveMethodMember.

public Map<JCTree, JCTree> resolveMethodMember(JavacNode node) {
    ArrayDeque<JCTree> stack = new ArrayDeque<JCTree>();
    {
        JavacNode n = node;
        while (n != null) {
            stack.push(n.get());
            n = n.up();
        }
    }
    messageSuppressor.disableLoggers();
    try {
        EnvFinder finder = new EnvFinder(node.getContext());
        while (!stack.isEmpty()) stack.pop().accept(finder);
        TreeMirrorMaker mirrorMaker = new TreeMirrorMaker(node.getTreeMaker(), node.getContext());
        JCTree copy = mirrorMaker.copy(finder.copyAt());
        Log log = Log.instance(node.getContext());
        JavaFileObject oldFileObject = log.useSource(((JCCompilationUnit) node.top().get()).getSourceFile());
        try {
            memberEnterAndAttribute(copy, finder.get(), node.getContext());
            return mirrorMaker.getOriginalToCopyMap();
        } finally {
            log.useSource(oldFileObject);
        }
    } finally {
        messageSuppressor.enableLoggers();
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Log(com.sun.tools.javac.util.Log) JCTree(com.sun.tools.javac.tree.JCTree) ArrayDeque(java.util.ArrayDeque)

Example 24 with Log

use of com.sun.tools.javac.util.Log in project ceylon by eclipse.

the class TestLog method test.

static void test(boolean genEndPos) throws IOException {
    Context context = new Context();
    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
    Log log = Log.instance(context);
    log.multipleErrors = true;
    JavacFileManager.preRegister(context);
    ParserFactory pfac = ParserFactory.instance(context);
    final String text = "public class Foo {\n" + "  public static void main(String[] args) {\n" + "    if (args.length == 0)\n" + "      System.out.println(\"no args\");\n" + "    else\n" + "      System.out.println(args.length + \" args\");\n" + "  }\n" + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);
    CharSequence cs = fo.getCharContent(true);
    Parser parser = pfac.newParser(cs, false, genEndPos, false);
    JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    log.setEndPosTable(fo, tree.endPositions);
    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);
    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
Also used : Context(com.sun.tools.javac.util.Context) Options(com.sun.tools.javac.util.Options) Log(com.sun.tools.javac.util.Log) JCTree(com.sun.tools.javac.tree.JCTree) ParserFactory(com.sun.tools.javac.parser.ParserFactory) Parser(com.sun.tools.javac.parser.Parser) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) TreeScanner(com.sun.tools.javac.tree.TreeScanner)

Aggregations

Log (com.sun.tools.javac.util.Log)24 AttrContext (com.sun.tools.javac.comp.AttrContext)9 Element (javax.lang.model.element.Element)8 VariableElement (javax.lang.model.element.VariableElement)8 Context (com.sun.tools.javac.util.Context)7 JavaFileObject (javax.tools.JavaFileObject)6 ExecutableElement (javax.lang.model.element.ExecutableElement)5 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 JCTree (com.sun.tools.javac.tree.JCTree)4 JavacScope (com.sun.tools.javac.api.JavacScope)3 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)3 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)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 Name (com.sun.tools.javac.util.Name)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Supplier (com.google.common.base.Supplier)1 Suppliers (com.google.common.base.Suppliers)1