JDK-8289249 : Add methods to Elements for record constructors
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: javax.lang.model
  • Affected Version: 19
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2022-06-27
  • Updated: 2024-10-27
  • Resolved: 2022-08-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 20
20 b10Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Description
It would be helpful to have methods on `Elements` to determine if the constructor for a method is either a compact constructor or a canonical constructor.  Both terms are defined for records in JLS 8.10.4

For example,
    boolean Elements isCompactConstructor(ExecutableElement ee);
    boolean Elements isCanonicalConstructor(ExecutableElement ee);

It is not directly possible in the Language Model world to determine if a constructor is a compact constructor (unless it is maybe defined that the parameters are MANDATED). The test for a canonical constructor is moderately expensive and involves looping over the parameters checking the name and type against the corresponding value for each component.  Meanwhile, internally, `javac` knows there to  both queries, encoded in `Flags.RECORD` and `Flags.COMPACT_RECORD_CONSTRUCTOR`, both defined for `MethodSymbol`.
Comments
A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/9694 Date: 2022-07-31 02:43:49 +0000
27-10-2024

Changeset: 0657acf5 Author: Joe Darcy <darcy@openjdk.org> Date: 2022-08-05 22:18:59 +0000 URL: https://git.openjdk.org/jdk/commit/0657acf534b0200b13a7535598d69ad4532cd97e
05-08-2022

It is possible to approximate, but not implement exactly, the isCanonicalConstructor using only the javax.lang.model API. From JLS 8.10.4.1: https://docs.oracle.com/javase/specs/jls/se18/html/jls-8.html#jls-8.10.4 "The declaration of a (non-compact) canonical constructor must satisfy all of the following conditions, or a compile-time error occurs: Each formal parameter in the formal parameter list must have the same name and declared type as the corresponding record component. A formal parameter must be a variable arity parameter if and only if the corresponding record component is a variable arity record component. The constructor must not be generic (§8.8.4). The constructor must not have a throws clause. The constructor body must not contain an explicit constructor invocation statement (§8.8.7.1). All the other rules for constructor declarations in a normal class declaration must be satisfied (§8.8)." Validating the requirement "The constructor body must not contain an explicit constructor invocation statement" would need to use the tree API. However, it would be acceptable for default method that threw UnsupportedOperationException, or perhaps approximated the check using properties within the language model API.
26-07-2022

I'm guessing that it may be possible that these flags are only available on source file declarations, and not on class file declarations.
27-06-2022

We already have use-cases for these methods in the javadoc Standard Doclet.
27-06-2022