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

Project Coin: convert JDK core library code to use try-with-resources

XMLWordPrintable

    • b134
    • generic
    • generic
    • Verified

      This is being filed under classes_lang but also applies to java.math and java.util.

      There are a variety of places in the core library code, as well as in the corresponding test code, where try-with-resources can be employed in a straightforward fashion. This will ensure that files get closed properly, even if there is an exception while processing them. In some cases files aren't closed at all, resulting in resource leaks. Tests will also benefit from ensuring that files are closed, as files left open cannot be removed (on Windows) leading to test failures.

      For example (actual code from src/share/classes/java/util/Currency.java) the following line of code:

          props.load(new FileReader(propFile));

      leaks an open file reference to propFile. It can be replaced with the following:

          try (FileReader fr = new FileReader(propFile)) {
              props.load(fr);
          }
       
      which will ensure that propFile is closed in the normal case as well as if there is an error with loading properties from this file.

      In addition, making use of try-with-resources will help prove out and provide feedback for this feature of Java 7.

            smarks Stuart Marks
            smarks Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: