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

javax.lang.model for sealed classes

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 15
    • core-libs
    • None
    • behavioral
    • minimal
    • Hide
      The addition of the feature does not by itself bring a compatibility risk.

      Sealed classes are a preview feature in Java SE 15. It is possible that incompatible changes will be made to sealed classes in a later Java SE release, before they become final and permanent. It is also possible that sealed classes will be removed in a later Java SE release, without ever having become final and permanent.
      Show
      The addition of the feature does not by itself bring a compatibility risk. Sealed classes are a preview feature in Java SE 15. It is possible that incompatible changes will be made to sealed classes in a later Java SE release, before they become final and permanent. It is also possible that sealed classes will be removed in a later Java SE release, without ever having become final and permanent.
    • Java API
    • SE

    Description

      Summary

      Add support for sealed classes in javax.lang.model. Sealed classes are classes or interfaces that restrict which other classes or interfaces may extend or implement them.

      Problem

      Sealed classes, see (JEP 360), will be previewed in Java SE 15 and support will be needed for them in javax.lang.model. In particular new modifiers will be needed, corresponding to the new sealed and non-sealed class modifiers. In addition, it will be necessary to provide a way to access the permitted subclasses of a sealed class or interface.

      Solution

      Enhance javax.lang.model to support sealed classes as follows:

      • Add two new modifiers to javax.lang.model.element.Modifier representing the sealed and the non-sealed class modifiers.

      • Add a new method to javax.lang.model.element.TypeElement which returns a list containing the permitted subclasses. This list can only be empty if the given TypeElement is not sealed.

      Specification

      diff --git a/src/java.compiler/share/classes/javax/lang/model/element/Modifier.java b/src/java.compiler/share/classes/javax/lang/model/element/Modifier.java
      --- a/src/java.compiler/share/classes/javax/lang/model/element/Modifier.java
      +++ b/src/java.compiler/share/classes/javax/lang/model/element/Modifier.java
      @@ -59,6 +59,36 @@ public enum Modifier {
            */
            DEFAULT,
           /** The modifier {@code static} */          STATIC,
      +
      +    /**
      +     * {@preview Associated with sealed classes, a preview feature of the Java language.
      +     *
      +     *           This enum constant is associated with <i>sealed classes</i>, a preview
      +     *           feature of the Java language. Preview features
      +     *           may be removed in a future release, or upgraded to permanent
      +     *           features of the Java language.}
      +     *
      +     * The modifier {@code sealed}
      +     * @since 15
      +     */
      +    @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SEALED_CLASSES,
      +                                             essentialAPI=false)
      +    SEALED,
      +
      +    /**
      +     * {@preview Associated with sealed classes, a preview feature of the Java language.
      +     *
      +     *           This enum constant is associated with <i>sealed classes</i>, a preview
      +     *           feature of the Java language. Preview features
      +     *           may be removed in a future release, or upgraded to permanent
      +     *           features of the Java language.}
      +     *
      +     * The modifier {@code non-sealed}
      +     * @since 15
      +     */
      +    @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SEALED_CLASSES,
      +            essentialAPI=false)
      +    NON_SEALED,
           /** The modifier {@code final} */           FINAL,
           /** The modifier {@code transient} */       TRANSIENT,
           /** The modifier {@code volatile} */        VOLATILE,
      diff --git a/src/java.compiler/share/classes/javax/lang/model/element/TypeElement.java b/src/java.compiler/share/classes/javax/lang/model/element/TypeElement.java
      --- a/src/java.compiler/share/classes/javax/lang/model/element/TypeElement.java
      +++ b/src/java.compiler/share/classes/javax/lang/model/element/TypeElement.java
      @@ -204,6 +204,29 @@ public interface TypeElement extends Element, Parameterizable, QualifiedNameable
               return List.of();
           }
      
      +    /**
      +     * {@preview Associated with sealed classes, a preview feature of the Java language.
      +     *
      +     *           This method is associated with <i>sealed classes</i>, a preview
      +     *           feature of the Java language. Preview features
      +     *           may be removed in a future release, or upgraded to permanent
      +     *           features of the Java language.}
      +     * Returns the permitted subclasses of this type element in
      +     * declaration order.
      +     *
      +     * @implSpec The default implementations of this method returns an
      +     * empty and unmodifiable list.
      +     *
      +     * @return the permitted subclasses, or an empty list if there are none
      +     *
      +     * @since 15
      +     */
      +    @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SEALED_CLASSES,
      +                                 essentialAPI=false)
      +    default List<? extends TypeMirror> getPermittedSubclasses() {
      +        return List.of();
      +    }
      +
           /**
            * Returns the package of a top-level type and returns the
            * immediately lexically enclosing element for a {@linkplain

      Additional links

      Attachments

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              briangoetz Brian Goetz
              Gavin Bierman, Joe Darcy, Jonathan Gibbons
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: