JDK 9 RFR of JDK-8068948: Update java.base module to use new try-with-resources statement

joe darcy joe.darcy at oracle.com
Wed Jan 14 16:29:41 UTC 2015


Hello,

On 1/13/2015 11:53 PM, Ivan Gerasimov wrote:
> Hi!
>
> A useful change!

I agree :-) We haven't publicized the change very much yet, but it is 
part of JEP 213, Milling Project Coin, 
(http://openjdk.java.net/jeps/213) and went into JDK 9's javac late in 2014.

> Might it also make sense to allow anonymous variables in the 
> try-with-resource statement?
> So that something like `try (() -> System.out.println("closed")) {}` 
> would work...

I don't think that is necessary. In JDK 7, we started out allowing a 
general AutoCloseable expression in a try-with-resources statement and 
that proved problematic. Supporting a final / effectively final variable 
seems to be the right balance. The rationale is written up in the JSR 
334 materials.

>
> Would it make sense to also patch 
> jdk/src/java.base/windows/classes/java/io/FileDescriptor.java in a 
> similar manner, as it appears to contain the exactly same code?

Good catch.

Thanks,

-Joe

>
> Sincerely yours,
> Ivan
>
> On 14.01.2015 5:46, joe darcy wrote:
>> Hello,
>>
>> Earlier in JDK 9, a language change was made so that if a resource 
>> for a try-with-resources statement is already final or effectively 
>> final, a new resource variable does *not* need to be declared to 
>> manage the variable.
>>
>> The java.base module should take advantage of this language feature.
>>
>> I've run an experimental checker for such locations over the base 
>> module and found one candidate where the new language feature can be 
>> used; please review this patch for it:
>>
>> diff -r d873f6a7d16b 
>> src/java.base/unix/classes/java/io/FileDescriptor.java
>> --- a/src/java.base/unix/classes/java/io/FileDescriptor.java Tue Jan 
>> 13 14:33:54 2015 -0800
>> +++ b/src/java.base/unix/classes/java/io/FileDescriptor.java Tue Jan 
>> 13 18:40:54 2015 -0800
>> @@ -214,7 +214,7 @@
>>          if (!closed) {
>>              closed = true;
>>              IOException ioe = null;
>> -            try (Closeable c = releaser) {
>> +            try (releaser) {
>>                  if (otherParents != null) {
>>                      for (Closeable referent : otherParents) {
>>                          try {
>>
>> The build succeeds with this change and the java.io.FileDescriptor 
>> regression tests pass.
>>
>> Thanks,
>>
>> -Joe
>>
>>
>




More information about the core-libs-dev mailing list