Details
-
CSR
-
Resolution: Approved
-
P3
-
None
-
behavioral
-
minimal
-
-
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 thesealed
and thenon-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 givenTypeElement
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
- csr of
-
JDK-8227044 javax.lang.model for sealed classes
- Resolved
- relates to
-
JDK-8246280 Refine API to model sealed classes and interfaces in javax.lang.model
- Resolved