Search in sources :

Example 81 with VariableTree

use of com.sun.source.tree.VariableTree in project ceylon by eclipse.

the class T6654037 method main.

public static void main(String[] args) throws Exception {
    // NOI18N
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    String code = "package test; public class Test {private void test() {Object o = null; boolean b = o != null && o instanceof String;} private Test() {}}";
    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath, "-Xjcov"), null, Arrays.asList(new MyFileObject(code)));
    CompilationUnitTree cut = ct.parse().iterator().next();
    ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
    MethodTree method = (MethodTree) clazz.getMembers().get(0);
    VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
    BinaryTree cond = (BinaryTree) condSt.getInitializer();
    JCTree condJC = (JCTree) cond;
    if (condJC.pos != 93)
        throw new IllegalStateException("Unexpected position=" + condJC.pos);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) BinaryTree(com.sun.source.tree.BinaryTree) JavaCompiler(javax.tools.JavaCompiler) JCTree(com.sun.tools.javac.tree.JCTree)

Example 82 with VariableTree

use of com.sun.source.tree.VariableTree in project ceylon-compiler by ceylon.

the class T6654037 method main.

public static void main(String[] args) throws Exception {
    // NOI18N
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    String code = "package test; public class Test {private void test() {Object o = null; boolean b = o != null && o instanceof String;} private Test() {}}";
    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath, "-Xjcov"), null, Arrays.asList(new MyFileObject(code)));
    CompilationUnitTree cut = ct.parse().iterator().next();
    ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
    MethodTree method = (MethodTree) clazz.getMembers().get(0);
    VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
    BinaryTree cond = (BinaryTree) condSt.getInitializer();
    JCTree condJC = (JCTree) cond;
    if (condJC.pos != 93)
        throw new IllegalStateException("Unexpected position=" + condJC.pos);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) BinaryTree(com.sun.source.tree.BinaryTree) JavaCompiler(javax.tools.JavaCompiler) JCTree(com.sun.tools.javac.tree.JCTree)

Example 83 with VariableTree

use of com.sun.source.tree.VariableTree in project ceylon-compiler by ceylon.

the class T6993301 method testExceptionParameterCorrectKind.

public void testExceptionParameterCorrectKind() throws IOException {
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    String code = "package test; public class Test { { try { } catch (NullPointerException ex) {} } }";
    final JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject(code)));
    CompilationUnitTree cut = ct.parse().iterator().next();
    ct.analyze();
    new TreePathScanner<Void, Void>() {

        @Override
        public Void visitVariable(VariableTree node, Void p) {
            Element el = Trees.instance(ct).getElement(getCurrentPath());
            assertNotNull(el);
            assertEquals(ElementKind.EXCEPTION_PARAMETER, el.getKind());
            return super.visitVariable(node, p);
        }
    }.scan(cut, null);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Element(javax.lang.model.element.Element) VariableTree(com.sun.source.tree.VariableTree) JavaCompiler(javax.tools.JavaCompiler)

Example 84 with VariableTree

use of com.sun.source.tree.VariableTree in project j2objc by google.

the class TreeConverter method convertLambda.

private TreeNode convertLambda(LambdaExpressionTree node, TreePath parent) {
    TreePath path = getTreePath(parent, node);
    LambdaExpression newNode = new LambdaExpression();
    convertFunctionalExpression((JCFunctionalExpression) node, parent, newNode);
    for (VariableTree param : node.getParameters()) {
        newNode.addParameter((VariableDeclaration) convert(param, path));
    }
    return newNode.setBody(convert(node.getBody(), path));
}
Also used : TreePath(com.sun.source.util.TreePath) VariableTree(com.sun.source.tree.VariableTree) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression)

Example 85 with VariableTree

use of com.sun.source.tree.VariableTree in project j2objc by google.

the class TreeConverter method convertForLoop.

private TreeNode convertForLoop(ForLoopTree node, TreePath parent) {
    TreePath path = getTreePath(parent, node);
    ForStatement newNode = new ForStatement().setExpression((Expression) convert(node.getCondition(), path)).setBody((Statement) convert(node.getStatement(), path));
    VariableDeclarationExpression lastVar = null;
    for (StatementTree initializer : node.getInitializer()) {
        if (initializer.getKind() == Kind.VARIABLE) {
            VariableTree var = (VariableTree) initializer;
            VariableDeclarationExpression newVar = convertVariableExpression(var, path);
            if (lastVar == null) {
                newNode.addInitializer(newVar);
                lastVar = newVar;
            } else {
                lastVar.addFragment(TreeUtil.remove(newVar.getFragment(0)));
            }
        } else {
            assert initializer.getKind() == Kind.EXPRESSION_STATEMENT;
            TreePath initializerPath = getTreePath(path, initializer);
            TreeNode expr = convert(((ExpressionStatementTree) initializer).getExpression(), initializerPath);
            newNode.addInitializer((Expression) expr);
        }
    }
    for (ExpressionStatementTree updater : node.getUpdate()) {
        newNode.addUpdater((Expression) convert(updater.getExpression(), getTreePath(path, updater)));
    }
    return newNode;
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) StatementTree(com.sun.source.tree.StatementTree) TreePath(com.sun.source.util.TreePath) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) JCFunctionalExpression(com.sun.tools.javac.tree.JCTree.JCFunctionalExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VariableTree(com.sun.source.tree.VariableTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement)

Aggregations

VariableTree (com.sun.source.tree.VariableTree)86 Tree (com.sun.source.tree.Tree)43 ClassTree (com.sun.source.tree.ClassTree)37 MethodTree (com.sun.source.tree.MethodTree)37 ExpressionTree (com.sun.source.tree.ExpressionTree)34 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)25 NewClassTree (com.sun.source.tree.NewClassTree)23 TreePath (com.sun.source.util.TreePath)23 IdentifierTree (com.sun.source.tree.IdentifierTree)22 JCTree (com.sun.tools.javac.tree.JCTree)21 MemberSelectTree (com.sun.source.tree.MemberSelectTree)19 AssignmentTree (com.sun.source.tree.AssignmentTree)17 ArrayList (java.util.ArrayList)17 BlockTree (com.sun.source.tree.BlockTree)16 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)13 Type (com.sun.tools.javac.code.Type)13 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)12 ReturnTree (com.sun.source.tree.ReturnTree)12 StatementTree (com.sun.source.tree.StatementTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)12