JDK-8282461 : Add useful predicates to ElementKind
  • Type: CSR
  • Component: core-libs
  • Sub-Component: javax.lang.model
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 19
  • Submitted: 2022-02-28
  • Updated: 2022-03-01
  • Resolved: 2022-03-01
Related Reports
CSR :  
Description
Summary
-------

Add additional predicates to categorize and group constants in `ElementKind`.

Problem
-------

Clients of the `ElementKind` API such as javadoc, may supply their own predicates. Such predicates have a greater risk of getting out of date with changes to the language than predicates supplied an maintained by the API itself.

Solution
--------

Add some natural predicates.

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

    --- a/src/java.compiler/share/classes/javax/lang/model/element/ElementKind.java
    +++ b/src/java.compiler/share/classes/javax/lang/model/element/ElementKind.java
    @@ -151,6 +150,17 @@ public enum ElementKind {
             return this == INTERFACE || this == ANNOTATION_TYPE;
         }
     
    +    /**
    +     * {@return {@code true} if this is a kind of declared type, a
    +     * {@linkplain #isClass() class} or an {@linkplain #isInterface()
    +     * interface}, and {@code false} otherwise}
    +     *
    +     * @since 19
    +     */
    +    public boolean isDeclaredType() {
    +        return isClass() || isInterface();
    +    }
    +
         /**
          * Returns {@code true} if this is a kind of field:
          * either {@code FIELD} or {@code ENUM_CONSTANT}.
    @@ -160,4 +170,50 @@ public enum ElementKind {
         public boolean isField() {
             return this == FIELD || this == ENUM_CONSTANT;
         }
    +
    +    /**
    +     * Returns {@code true} if this is a kind of executable: either
    +     * {@code METHOD} or {@code CONSTRUCTOR} or {@code STATIC_INIT} or
    +     * {@code INSTANCE_INIT}.
    +     *
    +     * @return {@code true} if this is a kind of executable
    +     * @since 19
    +     */
    +    public boolean isExecutable() {
    +        return switch(this){
    +        case METHOD, CONSTRUCTOR, STATIC_INIT, INSTANCE_INIT -> true;
    +        default -> false;
    +        };
    +    }
    +
    +    /**
    +     * Returns {@code true} if this is a kind of initializer: either
    +     * {@code STATIC_INIT} or {@code INSTANCE_INIT}.
    +     *
    +     * @return {@code true} if this is a kind of initializer
    +     * @since 19
    +     */
    +    public boolean isInitializer() {
    +        return switch(this){
    +        case STATIC_INIT, INSTANCE_INIT -> true;
    +        default -> false;
    +        };
    +    }
    +    /**
    +     * Returns {@code true} if this is a kind of variable: including
    +     * {@code ENUM_CONSTANT}, {@code FIELD}, {@code PARAMETER},
    +     * {@code LOCAL_VARIABLE}, {@code EXCEPTION_PARAMETER},
    +     * {@code RESOURCE_VARIABLE}, and {@code BINDING_VARIABLE}.
    +     *
    +     * @return {@code true} if this is a kind of variable
    +     * @since 19
    +     */
    +    public boolean isVariable() {
    +        return switch(this){
    +        case ENUM_CONSTANT, FIELD, PARAMETER,
    +             LOCAL_VARIABLE, EXCEPTION_PARAMETER, RESOURCE_VARIABLE,
    +             BINDING_VARIABLE -> true;
    +        default -> false;
    +        };
    +    }
     }


Comments
Moving to Approved.
01-03-2022