Search in sources :

Example 6 with AttrContext

use of com.sun.tools.javac.comp.AttrContext in project ceylon-compiler by ceylon.

the class JavacTrees method getTree.

public JCTree getTree(Element element) {
    Symbol symbol = (Symbol) element;
    TypeSymbol enclosing = symbol.enclClass();
    Env<AttrContext> env = enter.getEnv(enclosing);
    if (env == null)
        return null;
    JCClassDecl classNode = env.enclClass;
    if (classNode != null) {
        if (TreeInfo.symbolFor(classNode) == element)
            return classNode;
        for (JCTree node : classNode.getMembers()) if (TreeInfo.symbolFor(node) == element)
            return node;
    }
    return null;
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 7 with AttrContext

use of com.sun.tools.javac.comp.AttrContext in project ceylon-compiler by ceylon.

the class DeclarationImpl method getPosition.

/**
     * {@inheritDoc}
     */
public SourcePosition getPosition() {
    // Find the toplevel.  From there use a tree-walking utility
    // that finds the tree for our symbol, and with it the position.
    Env<AttrContext> enterEnv = getEnterEnv();
    if (enterEnv == null)
        return null;
    JCTree.JCCompilationUnit toplevel = enterEnv.toplevel;
    JavaFileObject sourcefile = toplevel.sourcefile;
    if (sourcefile == null)
        return null;
    int pos = TreeInfo.positionFor(sym, toplevel);
    return new SourcePositionImpl(sourcefile, pos, toplevel.lineMap);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) SourcePositionImpl(com.sun.tools.apt.mirror.util.SourcePositionImpl) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 8 with AttrContext

use of com.sun.tools.javac.comp.AttrContext 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 9 with AttrContext

use of com.sun.tools.javac.comp.AttrContext in project lombok by rzwitserloot.

the class JavacResolution method memberEnterAndAttribute.

private void memberEnterAndAttribute(JCTree copy, Env<AttrContext> env, Context context) {
    MemberEnter memberEnter = MemberEnter.instance(context);
    Env<AttrContext> oldEnv = getEnvOfMemberEnter(memberEnter);
    setEnvOfMemberEnter(memberEnter, env);
    try {
        copy.accept(memberEnter);
    } catch (Exception ignore) {
        // intentionally ignored; usually even if this step fails, val will work (but not for val in method local inner classes and anonymous inner classes).
        AssertionLogger.assertLog("member enter failed.", ignore);
    } finally {
        setEnvOfMemberEnter(memberEnter, oldEnv);
    }
    attrib(copy, env);
}
Also used : MemberEnter(com.sun.tools.javac.comp.MemberEnter) AttrContext(com.sun.tools.javac.comp.AttrContext) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with AttrContext

use of com.sun.tools.javac.comp.AttrContext in project ceylon-compiler by ceylon.

the class JavacTrees method getAttrContext.

private Env<AttrContext> getAttrContext(TreePath path) {
    if (// implicit null-check
    !(path.getLeaf() instanceof JCTree))
        throw new IllegalArgumentException();
    // then the classes will already have been entered.
    if (javacTaskImpl != null) {
        try {
            javacTaskImpl.enter(null);
        } catch (IOException e) {
            throw new Error("unexpected error while entering symbols: " + e);
        }
    }
    JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
    Copier copier = new Copier(treeMaker.forToplevel(unit));
    Env<AttrContext> env = null;
    JCMethodDecl method = null;
    JCVariableDecl field = null;
    List<Tree> l = List.nil();
    TreePath p = path;
    while (p != null) {
        l = l.prepend(p.getLeaf());
        p = p.getParentPath();
    }
    for (; l.nonEmpty(); l = l.tail) {
        Tree tree = l.head;
        switch(tree.getKind()) {
            case COMPILATION_UNIT:
                //                    System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
                env = enter.getTopLevelEnv((JCCompilationUnit) tree);
                break;
            case ANNOTATION_TYPE:
            case CLASS:
            case ENUM:
            case INTERFACE:
                //                    System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
                env = enter.getClassEnv(((JCClassDecl) tree).sym);
                break;
            case METHOD:
                //                    System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
                method = (JCMethodDecl) tree;
                break;
            case VARIABLE:
                //                    System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
                field = (JCVariableDecl) tree;
                break;
            case BLOCK:
                {
                    //                    System.err.println("BLOCK: ");
                    if (method != null)
                        env = memberEnter.getMethodEnv(method, env);
                    JCTree body = copier.copy((JCTree) tree, (JCTree) path.getLeaf());
                    env = attribStatToTree(body, env, copier.leafCopy);
                    return env;
                }
            default:
                //                    System.err.println("DEFAULT: " + tree.getKind());
                if (field != null && field.getInitializer() == tree) {
                    env = memberEnter.getInitEnv(field, env);
                    JCExpression expr = copier.copy((JCExpression) tree, (JCTree) path.getLeaf());
                    env = attribExprToTree(expr, env, copier.leafCopy);
                    return env;
                }
        }
    }
    return field != null ? memberEnter.getInitEnv(field, env) : env;
}
Also used : JCTree(com.sun.tools.javac.tree.JCTree) IOException(java.io.IOException) AttrContext(com.sun.tools.javac.comp.AttrContext) TreePath(com.sun.source.util.TreePath) TreeCopier(com.sun.tools.javac.tree.TreeCopier) CatchTree(com.sun.source.tree.CatchTree) Tree(com.sun.source.tree.Tree) JCTree(com.sun.tools.javac.tree.JCTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree)

Aggregations

AttrContext (com.sun.tools.javac.comp.AttrContext)13 JCTree (com.sun.tools.javac.tree.JCTree)6 Symbol (com.sun.tools.javac.code.Symbol)4 Log (com.sun.tools.javac.util.Log)4 JavacScope (com.sun.tools.javac.api.JavacScope)3 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)2 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)2 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)2 Type (com.sun.tools.javac.code.Type)2 JCImport (com.sun.tools.javac.tree.JCTree.JCImport)2 ListBuffer (com.sun.tools.javac.util.ListBuffer)2 Name (com.sun.tools.javac.util.Name)2 Names (com.sun.tools.javac.util.Names)2 Element (javax.lang.model.element.Element)2 VariableElement (javax.lang.model.element.VariableElement)2 CatchTree (com.sun.source.tree.CatchTree)1 ClassTree (com.sun.source.tree.ClassTree)1 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 MethodTree (com.sun.source.tree.MethodTree)1 NewClassTree (com.sun.source.tree.NewClassTree)1