src/share/classes/com/sun/tools/javac/comp/Lower.java
Print this page
@@ -1523,24 +1523,29 @@
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));
}
private JCStatement makeResourceCloseInvocation(JCExpression resource) {
- // create resource.close() method invocation
- JCExpression resourceClose = makeCall(resource, names.close, List.<JCExpression>nil());
- return make.Exec(resourceClose);
+ // create resource.close() method invocation protected by a null-check
+
+ JCExpression resourceClose = makeCall(resource,
+ names.close,
+ List.<JCExpression>nil());
+
+ 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.