JDK-7023233 : False positive for -Xlint:try with nested try with resources blocks
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2011-03-01
  • Updated: 2012-03-20
  • Resolved: 2011-04-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b134Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
> javac -version
javac 1.7.0-ea
> java -version
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b130)
Java HotSpot(TM) Client VM (build 21.0-b02, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
> uname -a
Linux riedquat 2.6.22.19-0.4-default #1 SMP 2009-08-14 02:09:16 +0200 i686 i686 i386 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
When enabling the try-warnings (-Xlint:all or -Xlint:try), there will be false positives for resources allocated in outer try-with-resources blocks and used in inner try-blocks.

A bug description can also be found in this blog entry:
http://www.riedquat.de/blog/2011-02-28-02


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the source code example below using javac -Xlint:all or javac -Xlint:try

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No warnings.
ACTUAL -
One warning:
> javac -Xlint:all XlintTryBug.java
XlintTryBug.java:5: warning: [try] auto-closeable resource out is never referenced in body of corresponding try statement
        try (final PrintWriter out = new PrintWriter(System.out)) {
             ^
1 warning


ERROR MESSAGES/STACK TRACES THAT OCCUR :
> javac -Xlint:all XlintTryBug.java
XlintTryBug.java:5: warning: [try] auto-closeable resource out is never referenced in body of corresponding try statement
        try (final PrintWriter out = new PrintWriter(System.out)) {
             ^
1 warning


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.*;

public class XlintTryBug {
    public static void main(final String... args) throws IOException {
        try (final PrintWriter out = new PrintWriter(System.out)) {
            for (final String arg : args) {
                try (final BufferedReader in = new BufferedReader(new FileReader(arg))) {
                    out.println(in.readLine());
                }
            }
        }
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Possible Workarounds:
* Do not use -Xlint:try
    This will require reconfiguration of compilation settings / scripts like Makefiles, build.xml etc. to explicitely -try for projects with -Xlint:all
* Always use the variable within its try block, i.e. by using it w/o side-effect.
The following line of code will already help:
assert out != null;

Comments
SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/e9b8fbb30f5a
03-03-2011

EVALUATION Logic for checking unusded twr resources does not handle recursion well. Will fix.
01-03-2011