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

compiler implementation for sealed classes

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 15
    • tools
    • 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, Language construct

    Description

      Summary

      Enhance the Java programming language with sealed classes. Sealed classes are classes or interfaces that impose restrictions on which other classes or interfaces may extend or implement them.

      Problem

      Java's type system doesn't offer many options to control the degree to which a class can be extended: Classes are either freely extensible (the default), or they can be declared final in which case they can not be extended at all. This disallows certain library designs, as well as limiting the analysis a compiler can do in checking conversions and use-site exhaustiveness at compile-time.

      Solution

      The Java language will be enhanced by supporting sealed classes, which restricts which classes may be a subclass of a sealed class. This allows various new kinds of library design; for example, to include a class and all of its subclasses. They also potentially enable exhaustiveness analysis at the use-site, such as when switching over type patterns for an instance of a sealed class. These features will constitute a preview feature (JEP 12) in Java SE 15.

      Specification

      See attachments for JLS updates; tree API update below.

      com/sun/source:

      diff -r 9672de6ee0d7 src/jdk.compiler/share/classes/com/sun/source/tree/ClassTree.java
      --- a/src/jdk.compiler/share/classes/com/sun/source/tree/ClassTree.java Tue Apr 07 09:50:36 2020 -0700
      +++ b/src/jdk.compiler/share/classes/com/sun/source/tree/ClassTree.java Fri Apr 10 13:08:16 2020 -0400
      @@ -86,6 +87,28 @@
           List<? extends Tree> getImplementsClause();
      
           /**
      +     * {@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 subclasses permitted by this type declaration.
      +     *
      +     * @implSpec this implementation returns an empty list
      +     *
      +     * @return the subclasses
      +     *
      +     * @since 15
      +     */
      +    @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SEALED_CLASSES,
      +                                             essentialAPI=false)
      +    default List<? extends Tree> getPermitsClause() {
      +        return Collections.emptyList();
      +    }
      +
      +    /**
            * Returns the members declared in this type declaration.
            * @return the members
            */

      Additional links

      Attachments

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              briangoetz Brian Goetz
              Gavin Bierman
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: