JDK-7011742 : Allow Expressions in resource specification of try-with-resources
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2011-01-12
  • Updated: 2021-09-01
  • Resolved: 2012-05-03
Related Reports
Relates :  
Relates :  
Relates :  
Description
Expert group feedback has found a syntatic issue in the JSR 334 / Project Coin try-with-resources feature: the Resource alternatives

    VariableModifiersopt Type VariableDeclaratorId = Expression 
    Expression

cannot be fully distinguished based on the first few tokens because of code like:

   try (i < j ? ....  // ?: operator expression
   try (List<String> l ...  // declaring a parameterized type

This issue should be resolved before the next JCP milestone.  Remedies include restricting or even eliminating the Expression alternative.  One restriction which may preserve most of the expressibility would be to limit the Expression to be an Identifier.

Comments
EVALUATION Any number of enhancements may be imagined for delivered language features, but history shows that such enhancements are almost never delivered.
03-05-2012

EVALUATION In Java SE7, the JSR 334 expert group decided to drop general expression support (7013420) for this syntactic problem and other issues. Allowing a restricted for of expressions, such as final or effectively final variables, may be considered for JDK 8.
07-03-2011

EVALUATION The JSR 334 expert group has decided to remove support for the general expresssion form of the try-with-resources statement from JSR 334 in Java SE 7(7013420: Project Coin: remove general expression support from try-with-resources statement). However, it may still be of academic interest to investigate how the grammar could be altered to support this feature.
22-01-2011

EVALUATION A fine idea. Finding a solution may include validating the revised grammar has the desired properties using a automated parser builder.
12-01-2011