--- old/src/share/classes/com/sun/tools/javac/comp/Lower.java 2011-02-18 15:52:25.000000000 -0800 +++ new/src/share/classes/com/sun/tools/javac/comp/Lower.java 2011-02-18 15:52:24.000000000 -0800 @@ -1425,25 +1425,55 @@ } } - /** Optionally replace a try statement with an automatic resource - * management (ARM) block. + /** + * Optionally replace a try statement with the desugaring of a + * try-with-resources statement. The canonical desugaring of + * + * try ResourceSpecification + * Block + * + * is + * + * { + * final VariableModifiers_minus_final R #resource = Expression; + * Throwable #primaryException = null; + * + * try ResourceSpecificationtail + * Block + * catch (Throwable #t) { + * #primaryException = t; + * throw #t; + * } finally { + * if (#resource != null) { + * if (#primaryException != null) { + * try { + * #resource.close(); + * } catch(Throwable #suppressedException) { + * #primaryException.addSuppressed(#suppressedException); + * } + * } else { + * #resource.close(); + * } + * } + * } + * * @param tree The try statement to inspect. - * @return An ARM block, or the original try block if there are no - * resources to manage. + * @return A a desugared try-with-resources tree, or the original + * try block if there are no resources to manage. */ - JCTree makeArmTry(JCTry tree) { + JCTree makeTwrTry(JCTry tree) { make_at(tree.pos()); twrVars = twrVars.dup(); - JCBlock armBlock = makeArmBlock(tree.resources, tree.body, 0); + JCBlock twrBlock = makeTwrBlock(tree.resources, tree.body, 0); if (tree.catchers.isEmpty() && tree.finalizer == null) - result = translate(armBlock); + result = translate(twrBlock); else - result = translate(make.Try(armBlock, tree.catchers, tree.finalizer)); + result = translate(make.Try(twrBlock, tree.catchers, tree.finalizer)); twrVars = twrVars.leave(); return result; } - private JCBlock makeArmBlock(List resources, JCBlock block, int depth) { + private JCBlock makeTwrBlock(List resources, JCBlock block, int depth) { if (resources.isEmpty()) return block; @@ -1497,16 +1527,16 @@ int oldPos = make.pos; make.at(TreeInfo.endPos(block)); - JCBlock finallyClause = makeArmFinallyClause(primaryException, expr); + JCBlock finallyClause = makeTwrFinallyClause(primaryException, expr); make.at(oldPos); - JCTry outerTry = make.Try(makeArmBlock(resources.tail, block, depth + 1), + JCTry outerTry = make.Try(makeTwrBlock(resources.tail, block, depth + 1), List.of(catchClause), finallyClause); stats.add(outerTry); return make.Block(0L, stats.toList()); } - private JCBlock makeArmFinallyClause(Symbol primaryException, JCExpression resource) { + private JCBlock makeTwrFinallyClause(Symbol primaryException, JCExpression resource) { // primaryException.addSuppressed(catchException); VarSymbol catchException = new VarSymbol(0, make.paramName(2), @@ -1525,22 +1555,30 @@ List catchClauses = List.of(make.Catch(catchExceptionDecl, catchBlock)); JCTry tryTree = make.Try(tryBlock, catchClauses, null); - // if (resource != null) resourceClose; - JCExpression nullCheck = makeBinary(JCTree.NE, - make.Ident(primaryException), - makeNull()); - JCIf closeIfStatement = make.If(nullCheck, + // if (primaryException != null) {try...} else resourceClose; + JCIf closeIfStatement = make.If(makeNonNullCheck(make.Ident(primaryException)), tryTree, makeResourceCloseInvocation(resource)); - return make.Block(0L, List.of(closeIfStatement)); + + // if (#resource != null) { if (primaryException ... } + return make.Block(0L, + List.of(make.If(makeNonNullCheck(resource), + closeIfStatement, + null))); } private JCStatement makeResourceCloseInvocation(JCExpression resource) { // create resource.close() method invocation - JCExpression resourceClose = makeCall(resource, names.close, List.nil()); + JCExpression resourceClose = makeCall(resource, + names.close, + List.nil()); return make.Exec(resourceClose); } + private JCExpression makeNonNullCheck(JCExpression expression) { + return makeBinary(JCTree.NE, expression, makeNull()); + } + /** Construct a tree that represents the outer instance * . Never pick the current `this'. * @param pos The source code position to be used for the tree. @@ -3573,7 +3611,7 @@ if (tree.resources.isEmpty()) { super.visitTry(tree); } else { - result = makeArmTry(tree); + result = makeTwrTry(tree); } }