Search in sources :

Example 1 with JCCatch

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

the class JavacAST method buildTry.

private JavacNode buildTry(JCTry tryNode) {
    if (setAndGetAsHandled(tryNode))
        return null;
    List<JavacNode> childNodes = new ArrayList<JavacNode>();
    for (JCTree varDecl : getResourcesForTryNode(tryNode)) {
        if (varDecl instanceof JCVariableDecl) {
            addIfNotNull(childNodes, buildLocalVar((JCVariableDecl) varDecl, Kind.LOCAL));
        }
    }
    addIfNotNull(childNodes, buildStatement(tryNode.body));
    for (JCCatch jcc : tryNode.catchers) addIfNotNull(childNodes, buildTree(jcc, Kind.STATEMENT));
    addIfNotNull(childNodes, buildStatement(tryNode.finalizer));
    return putInMap(new JavacNode(this, tryNode, childNodes, Kind.STATEMENT));
}
Also used : ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 2 with JCCatch

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

the class PrettyPrinter method visitTry.

@Override
public void visitTry(JCTry tree) {
    aPrint("try ");
    List<?> resources = readObject(tree, "resources", List.nil());
    int len = resources.length();
    switch(len) {
        case 0:
            break;
        case 1:
            print("(");
            JCVariableDecl decl = (JCVariableDecl) resources.get(0);
            flagMod = -1L & ~FINAL;
            printVarDefInline(decl);
            print(") ");
            break;
        default:
            println("(");
            indent++;
            int c = 0;
            for (Object i : resources) {
                align();
                flagMod = -1L & ~FINAL;
                printVarDefInline((JCVariableDecl) i);
                if (++c == len) {
                    print(") ");
                } else {
                    println(";", (JCTree) i);
                }
            }
            indent--;
    }
    println("{");
    indent++;
    for (JCStatement stat : tree.body.stats) print(stat);
    indent--;
    aPrint("}");
    for (JCCatch catchBlock : tree.catchers) {
        printCatch(catchBlock);
    }
    if (tree.finalizer != null) {
        println(" finally {");
        indent++;
        for (JCStatement stat : tree.finalizer.stats) print(stat);
        indent--;
        aPrint("}");
    }
    println(tree);
}
Also used : JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 3 with JCCatch

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

the class Attr method visitTry.

public void visitTry(JCTry tree) {
    // Create a new local environment with a local
    Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
    boolean isTryWithResource = tree.resources.nonEmpty();
    // Create a nested environment for attributing the try block if needed
    Env<AttrContext> tryEnv = isTryWithResource ? env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) : localEnv;
    // Attribute resource declarations
    for (JCTree resource : tree.resources) {
        if (resource.getTag() == JCTree.VARDEF) {
            attribStat(resource, tryEnv);
            chk.checkType(resource, resource.type, syms.autoCloseableType, "try.not.applicable.to.type");
            //check that resource type cannot throw InterruptedException
            checkAutoCloseable(resource.pos(), localEnv, resource.type);
            VarSymbol var = (VarSymbol) TreeInfo.symbolFor(resource);
            var.setData(ElementKind.RESOURCE_VARIABLE);
        } else {
            attribExpr(resource, tryEnv, syms.autoCloseableType, "try.not.applicable.to.type");
        }
    }
    // Attribute body
    attribStat(tree.body, tryEnv);
    if (isTryWithResource)
        tryEnv.info.scope.leave();
    // Attribute catch clauses
    for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
        JCCatch c = l.head;
        Env<AttrContext> catchEnv = localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup()));
        Type ctype = attribStat(c.param, catchEnv);
        if (TreeInfo.isMultiCatch(c)) {
            //multi-catch parameter is implicitly marked as final
            c.param.sym.flags_field |= FINAL | UNION;
        }
        if (c.param.sym.kind == Kinds.VAR) {
            c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER);
        }
        chk.checkType(c.param.vartype.pos(), chk.checkClassType(c.param.vartype.pos(), ctype), syms.throwableType);
        attribStat(c.body, catchEnv);
        catchEnv.info.scope.leave();
    }
    // Attribute finalizer
    if (tree.finalizer != null)
        attribStat(tree.finalizer, localEnv);
    localEnv.info.scope.leave();
    result = null;
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) JCTree(com.sun.tools.javac.tree.JCTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch)

Example 4 with JCCatch

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

the class AttributeDefinitionBuilder method generateFieldInit.

private JCTree generateFieldInit() {
    long flags = (modifiers & Flags.STATIC);
    JCTree.JCExpression varInit = variableInit;
    if (hasInitFlag()) {
        varInit = variableInit;
    }
    JCTree.JCAssign init = owner.make().Assign(owner.makeUnquotedIdent(Naming.quoteFieldName(fieldName)), varInit);
    List<JCStatement> stmts;
    if (isDeferredInitError()) {
        // surround the init expression with a try/catch that saves the exception
        // doesn't matter
        String exceptionName = "x";
        // $initException$ = x
        JCStatement saveException = owner.make().Exec(owner.make().Assign(owner.makeUnquotedIdent(Naming.getToplevelAttributeSavedExceptionName()), owner.makeUnquotedIdent(exceptionName)));
        // value = null
        JCStatement nullValue = owner.make().Exec(owner.make().Assign(owner.makeUnquotedIdent(fieldName), owner.makeDefaultExprForType(this.attrType)));
        // the catch statements
        JCStatement initFlagFalse = owner.make().Exec(owner.make().Assign(owner.naming.makeUnquotedIdent(Naming.getInitializationFieldName(fieldName)), owner.make().Literal(false)));
        JCBlock handlerBlock = owner.make().Block(0, List.<JCTree.JCStatement>of(saveException, nullValue, initFlagFalse));
        // the catch block
        JCExpression throwableType = owner.makeJavaType(owner.syms().throwableType.tsym);
        JCVariableDecl exceptionParam = owner.make().VarDef(owner.make().Modifiers(0), owner.naming.makeUnquotedName(exceptionName), throwableType, null);
        JCCatch catchers = owner.make().Catch(exceptionParam, handlerBlock);
        // $initException$ = null
        JCTree.JCAssign nullException = owner.make().Assign(owner.makeUnquotedIdent(Naming.getToplevelAttributeSavedExceptionName()), owner.makeNull());
        // $init$value = true;
        JCTree.JCAssign initFlagTrue = owner.make().Assign(owner.naming.makeUnquotedIdent(Naming.getInitializationFieldName(fieldName)), owner.make().Literal(true));
        // save the value, mark the exception as null
        List<JCStatement> body = List.<JCTree.JCStatement>of(owner.make().Exec(init), owner.make().Exec(nullException), owner.make().Exec(initFlagTrue));
        // the try/catch
        JCTree.JCTry try_ = owner.make().Try(owner.make().Block(0, body), List.<JCTree.JCCatch>of(catchers), null);
        stmts = List.<JCTree.JCStatement>of(try_);
    } else {
        stmts = List.<JCTree.JCStatement>of(owner.make().Exec(init));
    }
    return owner.make().Block(flags, stmts);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 5 with JCCatch

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

the class StatementTransformer method transform.

public JCStatement transform(Tree.TryCatchStatement t) {
    Tree.TryClause tryClause = t.getTryClause();
    at(tryClause);
    JCBlock tryBlock = transform(tryClause.getBlock());
    Tree.ResourceList resList = tryClause.getResourceList();
    if (resList != null) {
        ArrayList<Tree.Resource> resources = new ArrayList<Tree.Resource>(resList.getResources());
        Collections.reverse(resources);
        for (Tree.Resource res : resources) {
            List<JCStatement> stats = List.nil();
            Tree.Expression resExpr;
            String resVarName;
            if (res.getExpression() != null) {
                resExpr = res.getExpression();
                resVarName = naming.newTemp("try");
            } else if (res.getVariable() != null) {
                Tree.Variable var = res.getVariable();
                resExpr = var.getSpecifierExpression().getExpression();
                resVarName = var.getIdentifier().getText();
            } else {
                throw new BugException(res, "missing resource expression");
            }
            boolean isDestroyable = typeFact().getDestroyableType().isSupertypeOf(resExpr.getTypeModel());
            Type resVarType = resExpr.getTypeModel();
            Type resVarExpectedType = isDestroyable ? typeFact().getDestroyableType() : typeFact().getObtainableType();
            // CloseableType $var = resource-expression
            JCExpression expr = expressionGen().transformExpression(resExpr);
            JCExpression javaType = makeJavaType(resVarType);
            JCVariableDecl var = makeVar(FINAL, resVarName, javaType, expr);
            stats = stats.append(var);
            if (!isDestroyable) {
                JCExpression resVar0 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
                JCMethodInvocation openCall = make().Apply(null, makeQualIdent(resVar0, "obtain"), List.<JCExpression>nil());
                stats = stats.append(make().Exec(openCall));
            }
            // Exception $tpmex = null;
            String innerExTmpVarName = naming.newTemp("ex");
            JCExpression innerExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
            JCVariableDecl innerExTmpVar = makeVar(innerExTmpVarName, innerExType, makeNull());
            stats = stats.append(innerExTmpVar);
            // $tmpex = ex;
            List<JCStatement> innerCatchStats = List.nil();
            Name innerCatchVarName = naming.tempName("ex");
            JCAssign exTmpAssign = make().Assign(makeUnquotedIdent(innerExTmpVarName), make().Ident(innerCatchVarName));
            innerCatchStats = innerCatchStats.append(make().Exec(exTmpAssign));
            // throw ex;
            JCThrow innerCatchThrow = make().Throw(make().Ident(innerCatchVarName));
            innerCatchStats = innerCatchStats.append(innerCatchThrow);
            JCBlock innerCatchBlock = make().Block(0, innerCatchStats);
            // $var.close() /// ((Closeable)$var).close()
            JCExpression exarg = makeUnquotedIdent(innerExTmpVarName);
            JCExpression resVar1 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
            JCMethodInvocation closeCall = make().Apply(null, makeQualIdent(resVar1, isDestroyable ? "destroy" : "release"), List.<JCExpression>of(exarg));
            JCBlock closeTryBlock = make().Block(0, List.<JCStatement>of(make().Exec(closeCall)));
            // try { $var.close() } catch (Exception closex) { $tmpex.addSuppressed(closex); }
            Name closeCatchVarName = naming.tempName("closex");
            JCExpression closeCatchExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
            JCVariableDecl closeCatchVar = make().VarDef(make().Modifiers(Flags.FINAL), closeCatchVarName, closeCatchExType, null);
            JCExpression addarg = make().Ident(closeCatchVarName);
            JCMethodInvocation addSuppressedCall = make().Apply(null, makeQualIdent(makeUnquotedIdent(innerExTmpVarName), "addSuppressed"), List.<JCExpression>of(addarg));
            JCCatch closeCatch = make().Catch(closeCatchVar, make().Block(0, List.<JCStatement>of(make().Exec(addSuppressedCall))));
            JCTry closeTry = at(res).Try(closeTryBlock, List.<JCCatch>of(closeCatch), null);
            // $var.close() /// ((Closeable)$var).close()
            JCExpression exarg2 = makeUnquotedIdent(innerExTmpVarName);
            JCExpression resVar2 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
            JCMethodInvocation closeCall2 = make().Apply(null, makeQualIdent(resVar2, isDestroyable ? "destroy" : "release"), List.<JCExpression>of(exarg2));
            // if ($tmpex != null) { ... } else { ... }
            JCBinary closeCatchCond = make().Binary(JCTree.NE, makeUnquotedIdent(innerExTmpVarName), makeNull());
            JCIf closeCatchIf = make().If(closeCatchCond, closeTry, make().Exec(closeCall2));
            // try { .... } catch (Exception ex) { $tmpex=ex; throw ex; }
            // finally { try { $var.close() } catch (Exception closex) { } }
            JCExpression innerCatchExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
            JCVariableDecl innerCatchVar = make().VarDef(make().Modifiers(Flags.FINAL), innerCatchVarName, innerCatchExType, null);
            JCCatch innerCatch = make().Catch(innerCatchVar, innerCatchBlock);
            JCBlock innerFinallyBlock = make().Block(0, List.<JCStatement>of(closeCatchIf));
            JCTry innerTry = at(res).Try(tryBlock, List.<JCCatch>of(innerCatch), innerFinallyBlock);
            stats = stats.append(innerTry);
            tryBlock = at(res).Block(0, stats);
        }
    }
    final List<JCCatch> catches;
    if (usePolymorphicCatches(t.getCatchClauses())) {
        catches = transformCatchesPolymorphic(t.getCatchClauses());
    } else {
        catches = transformCatchesIfElseIf(t.getCatchClauses());
    }
    final JCBlock finallyBlock;
    Tree.FinallyClause finallyClause = t.getFinallyClause();
    if (finallyClause != null) {
        at(finallyClause);
        finallyBlock = transform(finallyClause.getBlock());
    } else {
        finallyBlock = null;
    }
    if (!catches.isEmpty() || finallyBlock != null) {
        return at(t).Try(tryBlock, catches, finallyBlock);
    } else {
        return tryBlock;
    }
}
Also used : Variable(com.redhat.ceylon.compiler.typechecker.tree.Tree.Variable) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ArrayList(java.util.ArrayList) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) CName(com.redhat.ceylon.compiler.java.codegen.Naming.CName) Name(com.sun.tools.javac.util.Name) OptionName(com.sun.tools.javac.main.OptionName) JCIf(com.sun.tools.javac.tree.JCTree.JCIf) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) JCThrow(com.sun.tools.javac.tree.JCTree.JCThrow) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCBinary(com.sun.tools.javac.tree.JCTree.JCBinary) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTry(com.sun.tools.javac.tree.JCTree.JCTry) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch)

Aggregations

JCCatch (com.sun.tools.javac.tree.JCTree.JCCatch)8 JCTree (com.sun.tools.javac.tree.JCTree)7 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)6 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)4 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)3 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)3 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)3 JCTry (com.sun.tools.javac.tree.JCTree.JCTry)3 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)2 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)2 Variable (com.redhat.ceylon.compiler.typechecker.tree.Tree.Variable)2 Type (com.redhat.ceylon.model.typechecker.model.Type)2 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 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 JCExpressionStatement (com.sun.tools.javac.tree.JCTree.JCExpressionStatement)2 JCIf (com.sun.tools.javac.tree.JCTree.JCIf)2 Pretty (com.sun.tools.javac.tree.Pretty)2 IOException (java.io.IOException)2