com.sun.tools.javac.comp.AttrContext Java Examples

The following examples show how to use com.sun.tools.javac.comp.AttrContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavacTrees.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: JavaCompiler.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Emit plain Java source for a class.
 *
 * @param env  The attribution environment of the outermost class
 *             containing this class.
 * @param cdef The class definition to be printed.
 */
JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
    JavaFileObject outFile
            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
            cdef.sym.flatname.toString(),
            JavaFileObject.Kind.SOURCE,
            null);
    if (inputFiles.contains(outFile)) {
        log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile);
        return null;
    } else {
        BufferedWriter out = new BufferedWriter(outFile.openWriter());
        try {
            new Pretty(out, true).printUnit(env.toplevel, cdef);
            if (verbose)
                log.printVerbose("wrote.file", outFile);
        } finally {
            out.close();
        }
        return outFile;
    }
}
 
Example #4
Source File: Types.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a symbol for a class that implements a given functional interface
 * and overrides its functional descriptor. This routine is used for two
 * main purposes: (i) checking well-formedness of a functional interface;
 * (ii) perform functional interface bridge calculation.
 */
public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) {
    if (targets.isEmpty()) {
        return null;
    }
    Symbol descSym = findDescriptorSymbol(targets.head.tsym);
    Type descType = findDescriptorType(targets.head);
    ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
    csym.completer = null;
    csym.members_field = new Scope(csym);
    MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym);
    csym.members_field.enter(instDescSym);
    Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym);
    ctype.supertype_field = syms.objectType;
    ctype.interfaces_field = targets;
    csym.type = ctype;
    csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile;
    return csym;
}
 
Example #5
Source File: JavacTrees.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: Types.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a symbol for a class that implements a given functional interface
 * and overrides its functional descriptor. This routine is used for two
 * main purposes: (i) checking well-formedness of a functional interface;
 * (ii) perform functional interface bridge calculation.
 */
public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) {
    if (targets.isEmpty()) {
        return null;
    }
    Symbol descSym = findDescriptorSymbol(targets.head.tsym);
    Type descType = findDescriptorType(targets.head);
    ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
    csym.completer = null;
    csym.members_field = new Scope(csym);
    MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym);
    csym.members_field.enter(instDescSym);
    Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym);
    ctype.supertype_field = syms.objectType;
    ctype.interfaces_field = targets;
    csym.type = ctype;
    csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile;
    return csym;
}
 
Example #7
Source File: JavaCompiler.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Emit plain Java source for a class.
 *  @param env    The attribution environment of the outermost class
 *                containing this class.
 *  @param cdef   The class definition to be printed.
 */
JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
    JavaFileObject outFile
       = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
                                           cdef.sym.flatname.toString(),
                                           JavaFileObject.Kind.SOURCE,
                                           null);
    if (inputFiles.contains(outFile)) {
        log.error(cdef.pos(), Errors.SourceCantOverwriteInputFile(outFile));
        return null;
    } else {
        try (BufferedWriter out = new BufferedWriter(outFile.openWriter())) {
            new Pretty(out, true).printUnit(env.toplevel, cdef);
            if (verbose)
                log.printVerbose("wrote.file", outFile);
        }
        return outFile;
    }
}
 
Example #8
Source File: Types.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a symbol for a class that implements a given functional interface
 * and overrides its functional descriptor. This routine is used for two
 * main purposes: (i) checking well-formedness of a functional interface;
 * (ii) perform functional interface bridge calculation.
 */
public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) {
    if (targets.isEmpty()) {
        return null;
    }
    Symbol descSym = findDescriptorSymbol(targets.head.tsym);
    Type descType = findDescriptorType(targets.head);
    ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
    csym.completer = Completer.NULL_COMPLETER;
    csym.members_field = WriteableScope.create(csym);
    MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym);
    csym.members_field.enter(instDescSym);
    Type.ClassType ctype = new Type.ClassType(Type.noType, List.nil(), csym);
    ctype.supertype_field = syms.objectType;
    ctype.interfaces_field = targets;
    csym.type = ctype;
    csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile;
    return csym;
}
 
Example #9
Source File: Types.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a symbol for a class that implements a given functional interface
 * and overrides its functional descriptor. This routine is used for two
 * main purposes: (i) checking well-formedness of a functional interface;
 * (ii) perform functional interface bridge calculation.
 */
public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) {
    if (targets.isEmpty()) {
        return null;
    }
    Symbol descSym = findDescriptorSymbol(targets.head.tsym);
    Type descType = findDescriptorType(targets.head);
    ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
    csym.completer = null;
    csym.members_field = new Scope(csym);
    MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym);
    csym.members_field.enter(instDescSym);
    Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym);
    ctype.supertype_field = syms.objectType;
    ctype.interfaces_field = targets;
    csym.type = ctype;
    csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile;
    return csym;
}
 
Example #10
Source File: JavacTrees.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
    JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
    try {
        return attr.attribStatToTree(stat, env, tree);
    } finally {
        log.useSource(prev);
    }
}
 
Example #11
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
    if (scope instanceof JavacScope
            && member instanceof Symbol
            && type instanceof com.sun.tools.javac.code.Type) {
        Env<AttrContext> env = ((JavacScope) scope).env;
        return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
    } else
        return false;
}
 
Example #12
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
    JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
    try {
        return attr.attribStatToTree(stat, env, tree);
    } finally {
        log.useSource(prev);
    }
}
 
Example #13
Source File: Symbol.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void setLazyConstValue(final Env<AttrContext> env,
                              final Attr attr,
                              final JCTree.JCExpression initializer)
{
    setData(new Callable<Object>() {
        public Object call() {
            return attr.attribLazyConstantValue(env, initializer, type);
        }
    });
}
 
Example #14
Source File: JavacElements.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a symbol's enter environment, or null if it has none.
 */
private Env<AttrContext> getEnterEnv(Symbol sym) {
    // Get enclosing class of sym, or sym itself if it is a class
    // or package.
    TypeSymbol ts = (sym.kind != Kinds.PCK)
                    ? sym.enclClass()
                    : (PackageSymbol) sym;
    return (ts != null)
            ? enter.getEnv(ts)
            : null;
}
 
Example #15
Source File: JavacElements.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #16
Source File: JavacTrees.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
    JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
    try {
        return attr.attribExprToTree(expr, env, tree);
    } finally {
        log.useSource(prev);
    }
}
 
Example #17
Source File: MissingLNTEntryForFinalizerTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public com.sun.tools.javac.code.Type attribStat(JCTree tree, Env<AttrContext> env) {
    com.sun.tools.javac.code.Type result = super.attribStat(tree, env);
    if (tree.hasTag(TRY)) {
        JCTry tryTree = (JCTry)tree;
        lineNumber = env.toplevel.lineMap.getLineNumber(tryTree.finalizer.endpos);
    }
    return result;
}
 
Example #18
Source File: JavacTrees.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
    if (scope instanceof JavacScope
            && member instanceof Symbol
            && type instanceof com.sun.tools.javac.code.Type) {
        Env<AttrContext> env = ((JavacScope) scope).env;
        return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
    } else
        return false;
}
 
Example #19
Source File: JavacTrees.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
    JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
    try {
        return attr.attribStatToTree(stat, env, tree);
    } finally {
        log.useSource(prev);
    }
}
 
Example #20
Source File: JavacTrees.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public boolean isAccessible(Scope scope, TypeElement type) {
    if (scope instanceof JavacScope && type instanceof ClassSymbol) {
        Env<AttrContext> env = ((JavacScope) scope).env;
        return resolve.isAccessible(env, (ClassSymbol)type, true);
    } else
        return false;
}
 
Example #21
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public boolean isAccessible(Scope scope, TypeElement type) {
    if (scope instanceof JavacScope && type instanceof ClassSymbol) {
        Env<AttrContext> env = ((JavacScope) scope).env;
        return resolve.isAccessible(env, (ClassSymbol)type, true);
    } else
        return false;
}
 
Example #22
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
    JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
    try {
        return attr.attribExprToTree(expr, env, tree);
    } finally {
        log.useSource(prev);
    }
}
 
Example #23
Source File: JavacElements.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #24
Source File: JavaCompiler.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Perform dataflow checks on an attributed parse tree.
 */
protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
    try {
        if (shouldStop(CompileState.FLOW))
            return;

        if (relax || compileStates.isDone(env, CompileState.FLOW)) {
            results.add(env);
            return;
        }

        if (verboseCompilePolicy)
            printNote("[flow " + env.enclClass.sym + "]");
        JavaFileObject prev = log.useSource(
                env.enclClass.sym.sourcefile != null ?
                        env.enclClass.sym.sourcefile :
                        env.toplevel.sourcefile);
        try {
            make.at(Position.FIRSTPOS);
            TreeMaker localMake = make.forToplevel(env.toplevel);
            flow.analyzeTree(env, localMake);
            compileStates.put(env, CompileState.FLOW);

            if (shouldStop(CompileState.FLOW))
                return;

            results.add(env);
        } finally {
            log.useSource(prev);
        }
    } finally {
        if (taskListener != null) {
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
            taskListener.finished(e);
        }
    }
}
 
Example #25
Source File: JavacElements.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a symbol's enter environment, or null if it has none.
 */
private Env<AttrContext> getEnterEnv(Symbol sym) {
    // Get enclosing class of sym, or sym itself if it is a class
    // or package.
    TypeSymbol ts = (sym.kind != Kinds.PCK)
                    ? sym.enclClass()
                    : (PackageSymbol) sym;
    return (ts != null)
            ? enter.getEnv(ts)
            : null;
}
 
Example #26
Source File: JavaCompiler.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Perform dataflow checks on attributed parse trees.
 * These include checks for definite assignment and unreachable statements.
 * If any errors occur, an empty list will be returned.
 *
 * @returns the list of attributed parse trees
 */
public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) {
    ListBuffer<Env<AttrContext>> results = lb();
    for (Env<AttrContext> env : envs) {
        flow(env, results);
    }
    return stopIfError(CompileState.FLOW, results);
}
 
Example #27
Source File: JavaCompiler.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Attribute a parse tree.
 *
 * @returns the attributed parse tree
 */
public Env<AttrContext> attribute(Env<AttrContext> env) {
    if (compileStates.isDone(env, CompileState.ATTR))
        return env;

    if (verboseCompilePolicy)
        printNote("[attribute " + env.enclClass.sym + "]");
    if (verbose)
        log.printVerbose("checking.attribution", env.enclClass.sym);

    if (taskListener != null) {
        TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
        taskListener.started(e);
    }

    JavaFileObject prev = log.useSource(
            env.enclClass.sym.sourcefile != null ?
                    env.enclClass.sym.sourcefile :
                    env.toplevel.sourcefile);
    try {
        attr.attrib(env);
        if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) {
            //if in fail-over mode, ensure that AST expression nodes
            //are correctly initialized (e.g. they have a type/symbol)
            attr.postAttr(env);
        }
        compileStates.put(env, CompileState.ATTR);
    } finally {
        log.useSource(prev);
    }

    return env;
}
 
Example #28
Source File: TypeAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Separate type annotations from declaration annotations and
 * determine the correct positions for type annotations.
 * This version only visits types in signatures and should be
 * called from MemberEnter.
 * The method takes the Annotate object as parameter and
 * adds an Annotate.Worker to the correct Annotate queue for
 * later processing.
 */
public void organizeTypeAnnotationsSignatures(final Env<AttrContext> env, final JCClassDecl tree) {
    annotate.afterRepeated( new Worker() {
        @Override
        public void run() {
            JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);

            try {
                new TypeAnnotationPositions(true).scan(tree);
            } finally {
                log.useSource(oldSource);
            }
        }
    } );
}
 
Example #29
Source File: TypeAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void validateTypeAnnotationsSignatures(final Env<AttrContext> env, final JCClassDecl tree) {
    annotate.validate(new Worker() { //validate annotations
        @Override
        public void run() {
            JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);

            try {
                attr.validateTypeAnnotations(tree, true);
            } finally {
                log.useSource(oldSource);
            }
        }
    } );
}
 
Example #30
Source File: Symbol.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void setLazyConstValue(final Env<AttrContext> env,
                              final Attr attr,
                              final JCVariableDecl variable)
{
    setData(new Callable<Object>() {
        public Object call() {
            return attr.attribLazyConstantValue(env, variable, type);
        }
    });
}