Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7023233

False positive for -Xlint:try with nested try with resources blocks

XMLWordPrintable

    • b134
    • x86
    • linux
    • Verified

      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;

            mcimadamore Maurizio Cimadamore
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: