JDK-8291594 : Add methods to Elements for record constructors
  • Type: CSR
  • Component: core-libs
  • Sub-Component: javax.lang.model
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 20
  • Submitted: 2022-07-31
  • Updated: 2022-08-05
  • Resolved: 2022-08-05
Related Reports
CSR :  
Description
Summary
-------

Add methods to `Elements` to determine if `record` constructors are canonical or compact.

Problem
-------

The language concepts of a `record`'s canonical constructor and compact constructor are not surfaced in the `javax.lang.model` API.

Solution
--------

Add predicates to `Elements` to support this functionality.

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

    diff --git a/src/java.compiler/share/classes/javax/lang/model/util/Elements.java b/src/java.compiler/share/classes/javax/lang/model/util/Elements.java
    index 98584cd9a27..f567c7f3b1b 100644
    --- a/src/java.compiler/share/classes/javax/lang/model/util/Elements.java
    +++ b/src/java.compiler/share/classes/javax/lang/model/util/Elements.java
    @@ -760,6 +760,51 @@ public interface Elements {
             return null;
         }
     
    +    /**
    +     * {@return {@code true} if the executable element can be
    +     * determined to be a canonical constructor of a record, {@code
    +     * false} otherwise}
    +     * Note that in some cases there may be insufficient information
    +     * to determine if a constructor is a canonical constructor, such
    +     * as if the executable element is built backed by a class
    +     * file. In such cases, {@code false} is returned.
    +     *
    +     * @implSpec
    +     * The default implementation of this method unconditionally
    +     * returns {@code false}.
    +     *
    +     * @param e  the executable being examined
    +     * @jls 8.10.4.1 Normal Canonical Constructors
    +     * @since 20
    +     */
    +    default boolean isCanonicalConstructor(ExecutableElement e) {
    +        return false;
    +    }
    +
    +    /**
    +     * {@return {@code true} if the executable element can be
    +     * determined to be a compact constructor of a record, {@code
    +     * false} otherwise}
    +     * By definition, a compact constructor is also a {@linkplain
    +     * #isCanonicalConstructor(ExecutableElement) canonical
    +     * constructor}.
    +     * Note that in some cases there may be insufficient information
    +     * to determine if a constructor is a compact constructor, such as
    +     * if the executable element is built backed by a class file. In
    +     * such cases, {@code false} is returned.
    +     *
    +     * @implSpec
    +     * The default implementation of this method unconditionally
    +     * returns {@code false}.
    +     *
    +     * @param e  the executable being examined
    +     * @jls 8.10.4.2 Compact Canonical Constructors
    +     * @since 20
    +     */
    +    default boolean isCompactConstructor(ExecutableElement e) {
    +        return false;
    +    }
    +
         /**
          * {@return the file object for this element or {@code null} if
          * there is no such file object}


Comments
Moving to Approved.
05-08-2022

[~briangoetz], specification in the CSR and PR updated accordingly.
01-08-2022

Can you be explicit about whether a compact constructor is also identified as a canonical constructor? Because a compact constructor is an alternate syntactic form for specifying the canonical constructor. I'd expect to see both return true for a compact constructor, but the spec doesn't say whether this is true or not.
01-08-2022