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

The following examples show how to use com.sun.tools.javac.comp.Env. 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: BoxingAndSuper.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
    List<JCTree> result = super.translateTopLevelClass(env, cdef, make);
    Map<Symbol, JCMethodDecl> declarations = new HashMap<>();
    Set<Symbol> toDump = new TreeSet<>(symbolComparator);

    new TreeScanner() {
        @Override
        public void visitMethodDef(JCMethodDecl tree) {
            if (tree.name.toString().startsWith("dump")) {
                toDump.add(tree.sym);
            }
            declarations.put(tree.sym, tree);
            super.visitMethodDef(tree);
        }
    }.scan(result);

    for (Symbol d : toDump) {
        dump(d, declarations, new HashSet<>());
    }

    return result;
}
 
Example #2
Source File: JavacTrees.java    From java-n-IDE-for-Android with Apache License 2.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 javaide with GNU General Public License v3.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: ManAttr.java    From manifold with Apache License 2.0 6 votes vote down vote up
default boolean handleNegationOverloading( JCTree.JCUnary tree )
{
  if( tree.getTag() != Tag.NEG )
  {
    return false;
  }

  // Attribute arguments
  ReflectUtil.LiveMethodRef checkNonVoid = ReflectUtil.method( chk(), "checkNonVoid", JCDiagnostic.DiagnosticPosition.class, Type.class );
  ReflectUtil.LiveMethodRef attribExpr = ReflectUtil.method( this, "attribExpr", JCTree.class, Env.class );
  Type expr = (Type)checkNonVoid.invoke( tree.arg.pos(), attribExpr.invoke( tree.arg, getEnv() ) );

  // Handle operator overloading
  Symbol.MethodSymbol overloadOperator = ManAttr.resolveNegationMethod( types(), tree.getTag(), expr );
  if( overloadOperator != null )
  {
    overloadOperator = new OverloadOperatorSymbol( overloadOperator, false );
    IDynamicJdk.instance().setOperator( tree, (Symbol.OperatorSymbol)overloadOperator );
    Type owntype = overloadOperator.type.isErroneous()
                   ? overloadOperator.type
                   : types().memberType( expr, overloadOperator ).getReturnType();
    setResult( tree, owntype );
    return true;
  }
  return false;
}
 
Example #5
Source File: TwrAvoidNullCheck.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
    List<JCTree> result = super.translateTopLevelClass(env, cdef, make);

    new TreeScanner() {
        @Override
        public void visitBinary(JCBinary tree) {
            hasNullCheck |= tree.operator.getSimpleName().contentEquals("!=") &&
                            "resource".equals(String.valueOf(TreeInfo.name(tree.lhs))) &&
                            TreeInfo.isNull(tree.rhs);
            super.visitBinary(tree);
        }
    }.scan(result);

    return result;
}
 
Example #6
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 #7
Source File: Types.java    From openjdk-jdk9 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 = 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 #8
Source File: JavacTrees.java    From javaide with GNU General Public License v3.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 #9
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 #10
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);
        }
    });
}
 
Example #11
Source File: TypeAnnotations.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void validateTypeAnnotationsSignatures(final Env<AttrContext> env, final JCClassDecl tree) {
    annotate.validate(() -> { //validate annotations
        JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);
        try {
            attr.validateTypeAnnotations(tree, true);
        } finally {
            log.useSource(oldSource);
        }
    });
}
 
Example #12
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 #13
Source File: JavacElements.java    From openjdk-8 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 #14
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 #15
Source File: JavacElements.java    From openjdk-8-source 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 #16
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 #17
Source File: Symbol.java    From openjdk-8 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);
        }
    });
}
 
Example #18
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 #19
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 #20
Source File: JavaCompiler.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Prepare attributed parse trees, in conjunction with their attribution contexts,
 * for source or code generation.
 * If any errors occur, an empty list will be returned.
 * @return a list containing the classes to be generated
 */
public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) {
    ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = new ListBuffer<>();
    for (Env<AttrContext> env: envs)
        desugar(env, results);
    return stopIfError(CompileState.FLOW, results);
}
 
Example #21
Source File: JavacTrees.java    From openjdk-jdk9 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 #22
Source File: JavacTrees.java    From openjdk-jdk8u-backup 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 #23
Source File: JavacTurbineJavaCompiler.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public Env<AttrContext> attribute(Env<AttrContext> env) {
  if (compileStates.isDone(env, CompileState.ATTR)) {
    return env;
  }
  Env<AttrContext> result = super.attribute(env);
  if (strictJavaDeps != null) {
    strictJavaDeps.postAttribute(result);
  }
  transitive.postAttribute(result);
  return result;
}
 
Example #24
Source File: TypeAnnotations.java    From openjdk-jdk9 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.
 */
public void organizeTypeAnnotationsSignatures(final Env<AttrContext> env, final JCClassDecl tree) {
    annotate.afterTypes(() -> {
        JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);
        try {
            new TypeAnnotationPositions(true).scan(tree);
        } finally {
            log.useSource(oldSource);
        }
    });
}
 
Example #25
Source File: JavacResolution.java    From EasyMPermission with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static Env<AttrContext> getEnvOfMemberEnter(MemberEnter memberEnter) {
	Field f = getMemberEnterDotEnv();
	try {
		return (Env<AttrContext>) f.get(memberEnter);
	} catch (Exception e) {
		return null;
	}
}
 
Example #26
Source File: JavacElements.java    From openjdk-8 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 #27
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 #28
Source File: Symbol.java    From hottub 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);
        }
    });
}
 
Example #29
Source File: JavacTrees.java    From openjdk-jdk8u-backup 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 #30
Source File: TypeAnnotations.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 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.
 */
public void organizeTypeAnnotationsSignatures(final Env<AttrContext> env, final JCClassDecl tree) {
    annotate.afterTypes(() -> {
        JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);
        try {
            new TypeAnnotationPositions(true).scan(tree);
        } finally {
            log.useSource(oldSource);
        }
    });
}