Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8282461

Add useful predicates to ElementKind

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P4
    • 19
    • core-libs
    • None
    • source
    • minimal
    • Add new methods to an enum class.
    • Java API
    • SE

    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;
      +        };
      +    }
       }

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              darcy Joe Darcy
              Jonathan Gibbons
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: