Search in sources :

Example 21 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)

Example 22 with AttrContext

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

the class ClassDocImpl method searchClass.

private ClassDoc searchClass(String className) {
    Names names = tsym.name.table.names;
    // search by qualified name first
    ClassDoc cd = env.lookupClass(className);
    if (cd != null) {
        return cd;
    }
    // search inner classes
    // ### Add private entry point to avoid creating array?
    // ### Replicate code in innerClasses here to avoid consing?
    ClassDoc[] innerClasses = innerClasses();
    for (int i = 0; i < innerClasses.length; i++) {
        if (innerClasses[i].name().equals(className) || // ### 'name' of the inner class, rather than the true simple name.
        innerClasses[i].name().endsWith(className)) {
            return innerClasses[i];
        } else {
            ClassDoc innercd = ((ClassDocImpl) innerClasses[i]).searchClass(className);
            if (innercd != null) {
                return innercd;
            }
        }
    }
    // check in this package
    cd = containingPackage().findClass(className);
    if (cd != null) {
        return cd;
    }
    // make sure that this symbol has been completed
    if (tsym.completer != null) {
        tsym.complete();
    }
    if (tsym.sourcefile != null) {
        // ### This information is available only for source classes.
        Env<AttrContext> compenv = env.enter.getEnv(tsym);
        if (compenv == null)
            return null;
        Scope s = compenv.toplevel.namedImportScope;
        for (Scope.Entry e = s.lookup(names.fromString(className)); e.scope != null; e = e.next()) {
            if (e.sym.kind == Kinds.TYP) {
                ClassDoc c = env.getClassDoc((ClassSymbol) e.sym);
                return c;
            }
        }
        s = compenv.toplevel.starImportScope;
        for (Scope.Entry e = s.lookup(names.fromString(className)); e.scope != null; e = e.next()) {
            if (e.sym.kind == Kinds.TYP) {
                ClassDoc c = env.getClassDoc((ClassSymbol) e.sym);
                return c;
            }
        }
    }
    // not found
    return null;
}
Also used : Names(com.sun.tools.javac.util.Names) Scope(com.sun.tools.javac.code.Scope) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 23 with AttrContext

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

the class ClassDocImpl method importedClasses.

/**
 * Get the list of classes declared as imported.
 * These are called "single-type-import declarations" in the JLS.
 * This method is deprecated in the ClassDoc interface.
 *
 * @return an array of ClassDocImpl representing the imported classes.
 *
 * @deprecated  Import declarations are implementation details that
 *          should not be exposed here.  In addition, not all imported
 *          classes are imported through single-type-import declarations.
 */
@Deprecated
public ClassDoc[] importedClasses() {
    // information is not available for binary classfiles
    if (tsym.sourcefile == null)
        return new ClassDoc[0];
    ListBuffer<ClassDocImpl> importedClasses = new ListBuffer<ClassDocImpl>();
    Env<AttrContext> compenv = env.enter.getEnv(tsym);
    if (compenv == null)
        return new ClassDocImpl[0];
    Name asterisk = tsym.name.table.names.asterisk;
    for (JCTree t : compenv.toplevel.defs) {
        if (t.getTag() == JCTree.IMPORT) {
            JCTree imp = ((JCImport) t).qualid;
            if ((TreeInfo.name(imp) != asterisk) && (imp.type.tsym.kind & Kinds.TYP) != 0) {
                importedClasses.append(env.getClassDoc((ClassSymbol) imp.type.tsym));
            }
        }
    }
    return importedClasses.toArray(new ClassDocImpl[importedClasses.length()]);
}
Also used : JCImport(com.sun.tools.javac.tree.JCTree.JCImport) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTree(com.sun.tools.javac.tree.JCTree) AttrContext(com.sun.tools.javac.comp.AttrContext) Name(com.sun.tools.javac.util.Name)

Example 24 with AttrContext

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

the class JavacElements method getTreeAndTopLevel.

/**
 * Returns the tree node and compilation unit corresponding to this
 * element, or null if they can't be found.
 */
private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
    Symbol sym = cast(Symbol.class, e);
    Env<AttrContext> enterEnv = getEnterEnv(sym);
    if (enterEnv == null)
        return null;
    JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
    if (tree == null || enterEnv.toplevel == null)
        return null;
    return new Pair<JCTree, JCCompilationUnit>(tree, enterEnv.toplevel);
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 25 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) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

AttrContext (com.sun.tools.javac.comp.AttrContext)25 Symbol (com.sun.tools.javac.code.Symbol)10 Log (com.sun.tools.javac.util.Log)10 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)9 VariableElement (javax.lang.model.element.VariableElement)9 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)8 Element (javax.lang.model.element.Element)8 JavacScope (com.sun.tools.javac.api.JavacScope)6 JCTree (com.sun.tools.javac.tree.JCTree)6 ExecutableElement (javax.lang.model.element.ExecutableElement)6 Type (com.sun.tools.javac.code.Type)5 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)5 Context (com.sun.tools.javac.util.Context)5 Names (com.sun.tools.javac.util.Names)5 ArrayList (java.util.ArrayList)5 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 TreePath (com.sun.source.util.TreePath)4 Name (com.sun.tools.javac.util.Name)4 NewClassTree (com.sun.source.tree.NewClassTree)3 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)3