1 /*
2 * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
1492 syms.throwableType,
1493 currentMethodSym);
1494 JCVariableDecl paramTree = make.VarDef(param, null);
1495 JCStatement assign = make.Assignment(primaryException, make.Ident(param));
1496 JCStatement rethrowStat = make.Throw(make.Ident(param));
1497 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(assign, rethrowStat));
1498 JCCatch catchClause = make.Catch(paramTree, catchBlock);
1499
1500 int oldPos = make.pos;
1501 make.at(TreeInfo.endPos(block));
1502 JCBlock finallyClause = makeArmFinallyClause(primaryException, expr);
1503 make.at(oldPos);
1504 JCTry outerTry = make.Try(makeArmBlock(resources.tail, block, depth + 1),
1505 List.<JCCatch>of(catchClause),
1506 finallyClause);
1507 stats.add(outerTry);
1508 return make.Block(0L, stats.toList());
1509 }
1510
1511 private JCBlock makeArmFinallyClause(Symbol primaryException, JCExpression resource) {
1512 // primaryException.addSuppressedException(catchException);
1513 VarSymbol catchException =
1514 new VarSymbol(0, make.paramName(2),
1515 syms.throwableType,
1516 currentMethodSym);
1517 JCStatement addSuppressionStatement =
1518 make.Exec(makeCall(make.Ident(primaryException),
1519 names.fromString("addSuppressedException"),
1520 List.<JCExpression>of(make.Ident(catchException))));
1521
1522 // try { resource.close(); } catch (e) { primaryException.addSuppressedException(e); }
1523 JCBlock tryBlock =
1524 make.Block(0L, List.<JCStatement>of(makeResourceCloseInvocation(resource)));
1525 JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null);
1526 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(addSuppressionStatement));
1527 List<JCCatch> catchClauses = List.<JCCatch>of(make.Catch(catchExceptionDecl, catchBlock));
1528 JCTry tryTree = make.Try(tryBlock, catchClauses, null);
1529
1530 // if (resource != null) resourceClose;
1531 JCExpression nullCheck = makeBinary(JCTree.NE,
1532 make.Ident(primaryException),
1533 makeNull());
1534 JCIf closeIfStatement = make.If(nullCheck,
1535 tryTree,
1536 makeResourceCloseInvocation(resource));
1537 return make.Block(0L, List.<JCStatement>of(closeIfStatement));
1538 }
1539
1540 private JCStatement makeResourceCloseInvocation(JCExpression resource) {
1541 // create resource.close() method invocation
1542 JCExpression resourceClose = makeCall(resource, names.close, List.<JCExpression>nil());
|
1 /*
2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
1492 syms.throwableType,
1493 currentMethodSym);
1494 JCVariableDecl paramTree = make.VarDef(param, null);
1495 JCStatement assign = make.Assignment(primaryException, make.Ident(param));
1496 JCStatement rethrowStat = make.Throw(make.Ident(param));
1497 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(assign, rethrowStat));
1498 JCCatch catchClause = make.Catch(paramTree, catchBlock);
1499
1500 int oldPos = make.pos;
1501 make.at(TreeInfo.endPos(block));
1502 JCBlock finallyClause = makeArmFinallyClause(primaryException, expr);
1503 make.at(oldPos);
1504 JCTry outerTry = make.Try(makeArmBlock(resources.tail, block, depth + 1),
1505 List.<JCCatch>of(catchClause),
1506 finallyClause);
1507 stats.add(outerTry);
1508 return make.Block(0L, stats.toList());
1509 }
1510
1511 private JCBlock makeArmFinallyClause(Symbol primaryException, JCExpression resource) {
1512 // primaryException.addSuppressed(catchException);
1513 VarSymbol catchException =
1514 new VarSymbol(0, make.paramName(2),
1515 syms.throwableType,
1516 currentMethodSym);
1517 JCStatement addSuppressionStatement =
1518 make.Exec(makeCall(make.Ident(primaryException),
1519 names.addSuppressed,
1520 List.<JCExpression>of(make.Ident(catchException))));
1521
1522 // try { resource.close(); } catch (e) { primaryException.addSuppressed(e); }
1523 JCBlock tryBlock =
1524 make.Block(0L, List.<JCStatement>of(makeResourceCloseInvocation(resource)));
1525 JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null);
1526 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(addSuppressionStatement));
1527 List<JCCatch> catchClauses = List.<JCCatch>of(make.Catch(catchExceptionDecl, catchBlock));
1528 JCTry tryTree = make.Try(tryBlock, catchClauses, null);
1529
1530 // if (resource != null) resourceClose;
1531 JCExpression nullCheck = makeBinary(JCTree.NE,
1532 make.Ident(primaryException),
1533 makeNull());
1534 JCIf closeIfStatement = make.If(nullCheck,
1535 tryTree,
1536 makeResourceCloseInvocation(resource));
1537 return make.Block(0L, List.<JCStatement>of(closeIfStatement));
1538 }
1539
1540 private JCStatement makeResourceCloseInvocation(JCExpression resource) {
1541 // create resource.close() method invocation
1542 JCExpression resourceClose = makeCall(resource, names.close, List.<JCExpression>nil());
|