Search in sources :

Example 6 with JCExpressionStatement

use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project bazel by bazelbuild.

the class InternalUtils method constructor.

/**
     * Determines the symbol for a constructor given an invocation via
     * {@code new}.
     *
     * If the tree is a declaration of an anonymous class, then method returns
     * constructor that gets invoked in the extended class, rather than the
     * anonymous constructor implicitly added by the constructor (JLS 15.9.5.1)
     *
     * @param tree the constructor invocation
     * @return the {@link ExecutableElement} corresponding to the constructor
     *         call in {@code tree}
     */
public static ExecutableElement constructor(NewClassTree tree) {
    if (!(tree instanceof JCTree.JCNewClass)) {
        ErrorReporter.errorAbort("InternalUtils.constructor: not a javac internal tree");
        // dead code
        return null;
    }
    JCNewClass newClassTree = (JCNewClass) tree;
    if (RETURN_INVOKE_CONSTRUCTOR && tree.getClassBody() != null) {
        // anonymous constructor bodies should contain exactly one statement
        // in the form:
        //    super(arg1, ...)
        // or
        //    o.super(arg1, ...)
        //
        // which is a method invocation (!) to the actual constructor
        // the method call is guaranteed to return nonnull
        JCMethodDecl anonConstructor = (JCMethodDecl) TreeInfo.declarationFor(newClassTree.constructor, newClassTree);
        assert anonConstructor != null;
        assert anonConstructor.body.stats.size() == 1;
        JCExpressionStatement stmt = (JCExpressionStatement) anonConstructor.body.stats.head;
        JCTree.JCMethodInvocation superInvok = (JCMethodInvocation) stmt.expr;
        return (ExecutableElement) TreeInfo.symbol(superInvok.meth);
    }
    Element e = newClassTree.constructor;
    assert e instanceof ExecutableElement;
    return (ExecutableElement) e;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCTree(com.sun.tools.javac.tree.JCTree) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation)

Example 7 with JCExpressionStatement

use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project error-prone by google.

the class Template method pretty.

protected static Pretty pretty(Context context, final Writer writer) {
    final JCCompilationUnit unit = context.get(JCCompilationUnit.class);
    try {
        final String unitContents = unit.getSourceFile().getCharContent(false).toString();
        return new Pretty(writer, true) {

            {
                // Work-around for b/22196513
                width = 0;
            }

            @Override
            public void visitAnnotation(JCAnnotation anno) {
                if (anno.getArguments().isEmpty()) {
                    try {
                        print("@");
                        printExpr(anno.annotationType);
                    } catch (IOException e) {
                        // the supertype swallows exceptions too
                        throw new RuntimeException(e);
                    }
                } else {
                    super.visitAnnotation(anno);
                }
            }

            @Override
            public void printExpr(JCTree tree, int prec) throws IOException {
                EndPosTable endPositions = unit.endPositions;
                /*
           * Modifiers, and specifically flags like final, appear to just need weird special
           * handling.
           *
           * Note: we can't use {@code TreeInfo.getEndPos()} or {@code JCTree.getEndPosition()}
           * here, because they will return the end position of an enclosing AST node for trees
           * whose real end positions aren't stored.
           */
                int endPos = endPositions.getEndPos(tree);
                boolean hasRealEndPosition = endPos != Position.NOPOS;
                if (tree.getKind() != Kind.MODIFIERS && hasRealEndPosition) {
                    writer.append(unitContents.substring(tree.getStartPosition(), endPos));
                } else {
                    super.printExpr(tree, prec);
                }
            }

            @Override
            public void visitApply(JCMethodInvocation tree) {
                JCExpression select = tree.getMethodSelect();
                if (select != null && select.toString().equals("Refaster.emitCommentBefore")) {
                    String commentLiteral = (String) ((JCLiteral) tree.getArguments().get(0)).getValue();
                    JCExpression expr = tree.getArguments().get(1);
                    try {
                        print("/* " + commentLiteral + " */ ");
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    expr.accept(this);
                } else {
                    super.visitApply(tree);
                }
            }

            @Override
            public void printStat(JCTree tree) throws IOException {
                if (tree instanceof JCExpressionStatement && ((JCExpressionStatement) tree).getExpression() instanceof JCMethodInvocation) {
                    JCMethodInvocation invocation = (JCMethodInvocation) ((JCExpressionStatement) tree).getExpression();
                    JCExpression select = invocation.getMethodSelect();
                    if (select != null && select.toString().equals("Refaster.emitComment")) {
                        String commentLiteral = (String) ((JCLiteral) invocation.getArguments().get(0)).getValue();
                        print("// " + commentLiteral);
                        return;
                    }
                }
                super.printStat(tree);
            }

            @Override
            public void visitTry(JCTry tree) {
                if (tree.getResources().isEmpty()) {
                    super.visitTry(tree);
                    return;
                }
                try {
                    print("try (");
                    boolean first = true;
                    for (JCTree resource : tree.getResources()) {
                        if (!first) {
                            print(";");
                            println();
                        }
                        printExpr(resource);
                        first = false;
                    }
                    print(")");
                    printStat(tree.body);
                    for (JCCatch catchStmt : tree.getCatches()) {
                        printStat(catchStmt);
                    }
                    if (tree.getFinallyBlock() != null) {
                        print(" finally ");
                        printStat(tree.getFinallyBlock());
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCTree(com.sun.tools.javac.tree.JCTree) IOException(java.io.IOException) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) Pretty(com.sun.tools.javac.tree.Pretty) JCTry(com.sun.tools.javac.tree.JCTree.JCTry) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) EndPosTable(com.sun.tools.javac.tree.EndPosTable)

Example 8 with JCExpressionStatement

use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project lombok by rzwitserloot.

the class PrettyPrinter method visitForLoop.

@Override
public void visitForLoop(JCForLoop tree) {
    aPrint("for (");
    if (tree.init.nonEmpty()) {
        // ForInit is either a StatementExpressionList or a LocalVariableDeclaration
        if (tree.init.head instanceof JCVariableDecl) {
            boolean first = true;
            int dims = 0;
            for (JCStatement i : tree.init) {
                JCVariableDecl vd = (JCVariableDecl) i;
                if (first) {
                    printVarDefInline(vd);
                    dims = dims(vd.vartype);
                } else {
                    print(", ");
                    print(vd.name);
                    int dimDiff = dims(vd.vartype) - dims;
                    for (int j = 0; j < dimDiff; j++) print("[]");
                    if (vd.init != null) {
                        print(" = ");
                        print(vd.init);
                    }
                }
                first = false;
            }
        } else {
            boolean first = true;
            for (JCStatement exprStatement : tree.init) {
                if (!first)
                    print(", ");
                first = false;
                print(((JCExpressionStatement) exprStatement).expr);
            }
        }
    }
    print("; ");
    if (tree.cond != null)
        print(tree.cond);
    print("; ");
    boolean first = true;
    for (JCExpressionStatement exprStatement : tree.step) {
        if (!first)
            print(", ");
        first = false;
        print(exprStatement.expr);
    }
    print(") ");
    print(tree.body);
}
Also used : JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 9 with JCExpressionStatement

use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project lombok by rzwitserloot.

the class JavacHandlerUtil method isConstructorCall.

public static boolean isConstructorCall(final JCStatement statement) {
    if (!(statement instanceof JCExpressionStatement))
        return false;
    JCExpression expr = ((JCExpressionStatement) statement).expr;
    if (!(expr instanceof JCMethodInvocation))
        return false;
    JCExpression invocation = ((JCMethodInvocation) expr).meth;
    String name;
    if (invocation instanceof JCFieldAccess) {
        name = ((JCFieldAccess) invocation).name.toString();
    } else if (invocation instanceof JCIdent) {
        name = ((JCIdent) invocation).name.toString();
    } else {
        name = "";
    }
    return "super".equals(name) || "this".equals(name);
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement)

Example 10 with JCExpressionStatement

use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project ceylon-compiler by ceylon.

the class Attr method checkFirstConstructorStat.

//where
/** Check that given application node appears as first statement
         *  in a constructor call.
         *  @param tree   The application node
         *  @param env    The environment current at the application.
         */
boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) {
    JCMethodDecl enclMethod = env.enclMethod;
    if (enclMethod != null && enclMethod.name == names.init) {
        JCBlock body = enclMethod.body;
        if (body.stats.head.getTag() == JCTree.EXEC && ((JCExpressionStatement) body.stats.head).expr == tree)
            return true;
        // given tree allowed as last stmt in a Let
        if (body.stats.head.getTag() == JCTree.EXEC && (((JCExpressionStatement) body.stats.head).expr instanceof LetExpr) && ((LetExpr) ((JCExpressionStatement) body.stats.head).expr).stats.last().getTag() == JCTree.EXEC && ((JCExpressionStatement) ((LetExpr) ((JCExpressionStatement) body.stats.head).expr).stats.last()).expr == tree)
            return true;
    }
    log.error(tree.pos(), "call.must.be.first.stmt.in.ctor", TreeInfo.name(tree.meth));
    return false;
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) LetExpr(com.sun.tools.javac.tree.JCTree.LetExpr) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement)

Aggregations

JCExpressionStatement (com.sun.tools.javac.tree.JCTree.JCExpressionStatement)12 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)7 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)5 JCTree (com.sun.tools.javac.tree.JCTree)3 JCIf (com.sun.tools.javac.tree.JCTree.JCIf)3 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)3 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)3 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)2 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)2 JCBinary (com.sun.tools.javac.tree.JCTree.JCBinary)2 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)2 JCCatch (com.sun.tools.javac.tree.JCTree.JCCatch)2 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)2 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)2 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)2 JCTry (com.sun.tools.javac.tree.JCTree.JCTry)2 LetExpr (com.sun.tools.javac.tree.JCTree.LetExpr)2 Pretty (com.sun.tools.javac.tree.Pretty)2