src/share/classes/com/sun/tools/javac/comp/Lower.java

Print this page

        

@@ -1429,23 +1429,23 @@
      *  management (ARM) block.
      * @param tree  The try statement to inspect.
      * @return      An ARM block, 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 armBlock = makeTwrBlock(tree.resources, tree.body, 0);
         if (tree.catchers.isEmpty() && tree.finalizer == null)
             result = translate(armBlock);
         else
             result = translate(make.Try(armBlock, tree.catchers, tree.finalizer));
         twrVars = twrVars.leave();
         return result;
     }
 
-    private JCBlock makeArmBlock(List<JCTree> resources, JCBlock block, int depth) {
+    private JCBlock makeTwrBlock(List<JCTree> resources, JCBlock block, int depth) {
         if (resources.isEmpty())
             return block;
 
         // Add resource declaration or expression to block statements
         ListBuffer<JCStatement> stats = new ListBuffer<JCStatement>();

@@ -1495,20 +1495,20 @@
         JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(assign, rethrowStat));
         JCCatch catchClause = make.Catch(paramTree, catchBlock);
 
         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.<JCCatch>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),
                           syms.throwableType,
                           currentMethodSym);

@@ -1523,24 +1523,35 @@
         JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null);
         JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(addSuppressionStatement));
         List<JCCatch> catchClauses = List.<JCCatch>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.<JCStatement>of(closeIfStatement));
+
+        // if (#resource != null) { if (primaryException ...  }
+        return make.Block(0L,
+                          List.<JCStatement>of(make.If(makeNonNullCheck(resource),
+                                                       closeIfStatement,
+                                                       null)));
     }
 
     private JCStatement makeResourceCloseInvocation(JCExpression resource) {
-        // create resource.close() method invocation
-        JCExpression resourceClose = makeCall(resource, names.close, List.<JCExpression>nil());
+        // create resource.close() method invocation protected by a null-check
+
+        JCExpression resourceClose = makeCall(resource,
+                                              names.close,
+                                              List.<JCExpression>nil());
         return make.Exec(resourceClose);
+
+        //return make.If(makeNonNullCheck(resource), make.Exec(resourceClose), null);
+    }
+
+    private JCExpression makeNonNullCheck(JCExpression expression) {
+        return makeBinary(JCTree.NE, expression, makeNull());
     }
 
     /** Construct a tree that represents the outer instance
      *  <C.this>. Never pick the current `this'.
      *  @param pos           The source code position to be used for the tree.

@@ -3571,11 +3582,11 @@
     @Override
     public void visitTry(JCTry tree) {
         if (tree.resources.isEmpty()) {
             super.visitTry(tree);
         } else {
-            result = makeArmTry(tree);
+            result = makeTwrTry(tree);
         }
     }
 
 /**************************************************************************
  * main method