JDK-8307577 : Implementation for javax.lang.model for unnamed variables (Preview)
  • Type: CSR
  • Component: core-libs
  • Sub-Component: javax.lang.model
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 21
  • Submitted: 2023-05-06
  • Updated: 2023-09-27
  • Resolved: 2023-05-08
Related Reports
CSR :  
Relates :  
Description
Summary
-------

JEP 443: Unnamed Patterns and Variables introduces unnamed variables in Java. The `javax.lang.model` API need to be updated accordingly.

Problem
-------

JEP 443 repurposes the underscore token (`_`) to denote unnamed variables in Java in various locations:

- a local variable declaration statement in a block (JLS 14.4.2)
- a resource specification of a `try-with-resources` statement (JLS 14.20.3)
- the header of a basic for statement (JLS 14.14.1)
- the header of an enhanced `for` loop (JLS 14.14.2)
- an exception parameter of a `catch` block (JLS 14.20)
- a formal parameter of a lambda expression (JLS 15.27.1)

When any of the above variable elements is declared with the name `_`, they denote unnamed variables elements. Moreover, as they do not carry a distinguishable name, they cannot be referred throughout their enclosing scopes.

Solution
--------

We propose the following adjustments to the javax.lang.model API:

- Modify the method `javax.lang.model.element.VariableElement::getSimpleName()` to return empty name in the case of unnamed variables, making it consistent with unnamed packages, unnamed modules and anonymous classes. The `com.sun.source.Tree` API also returns an empty name for its version of the name through `com.sun.source.tree.VariableTree.getName()`.
- Add a new predicate method `javax.lang.model.element.VariableElement::isUnnamed()`. The default implementation of this method examines whether `getSimpleName()` returns an empty name.

Specification
-------------

The proposed API enhancements are attached as `specdiff.preliminary.00.zip`. These include the changes in javax/lang/model.

The changes to the specification and API are a subject of change until the CSR is finalized.


Comments
Moving to Approved.
08-05-2023

All comments addressed. Thank you!
08-05-2023

[~abimpoudis], please add an `@since` tag for VariableElement.isUnnamed(); otherwise, the changes look fine.
08-05-2023

(For future consideration, a "CanBeUnnamed" mix-in subinterface to allow the entities that can be unnamed to be grouped together under one type that captures that commonality. It be clear, I don't think such an interface should be added at this time.)
08-05-2023

Moving to Provisional, not Approved. For VariableElement.isUnamed, I recommend a more operational wording of the implSpec tag. Rather than "The default implementation of this method examines whether getSimpleName() returns an empty name." something like "The default implementation of this method calls getSimpleName() and returns true if the result is empty and false otherwise." Given the presence of VariableElement.isUnamed, I think Elements.isUnamed(VariableElement) can be dropped.
08-05-2023