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

Preview APIs support for sealed classes

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 15
    • core-libs
    • None
    • 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

      Summary

      Add support for sealed classes in core-libs. 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 core-libs. In particular some reflection support will be needed in order to find out if a class or interface is sealed or not and to access a class' permitted subclasses if applicable

      Solution

      Enhance core-libs to support sealed classes as follows.

      • Add a new predicate to java.lang.Class to query if the given class is sealed or not.

      • Also add another method to java.lang.Class which returns an array of java.lang.constant.ClassDesc containing the permitted subclasses. This array can only be empty if the given class is not sealed.

      Specification

      /**
       * {@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 an array containing {@code ClassDesc} objects representing all the
       * direct subclasses or direct implementation classes permitted to extend or implement this class or interface
       * if it is sealed. If this {@code Class} object represents a primitive type, {@code void}, an array type,
       * or a class or interface that is not sealed, an empty array is returned.
       *
       * @return an array of class descriptors of all the permitted subclasses of this class or interface
       *
       * @jls 8.1 Class Declarations
       * @jls 9.1 Interface Declarations
       * @since 15
       */
      @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SEALED_CLASSES, essentialAPI=false)
      public ClassDesc[] permittedSubclasses() {}
      
      /**
       * * {@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 {@code true} if and only if this {@code Class} object represents a sealed class or interface.
       * If this {@code Class} object represents a primitive type, {@code void}, or an array type, this method returns
       * {@code false}.
       *
       * @return {@code true} if and only if this {@code Class} object represents a sealed class or interface.
       *
       * @jls 8.1 Class Declarations
       * @jls 9.1 Interface Declarations
       * @since 15
       */
      @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SEALED_CLASSES, essentialAPI=false)
      @SuppressWarnings("preview")
      public boolean isSealed() {}

      Additional links

            vromero Vicente Arturo Romero Zaldivar
            briangoetz Brian Goetz
            Joe Darcy
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: