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
- csr of
-
JDK-8227046 compiler implementation for sealed classes
- Resolved
- relates to
-
JDK-8227048 JLS changes for Sealed Classes (Preview)
- Resolved