-
Type:
CSR
-
Resolution: Approved
-
Priority:
P3
-
Component/s: tools
-
source, behavioral
-
minimal
-
Java API, Language construct
-
SE
Summary
Since the release of the "Sealed Classes" feature JEP 360 in Java SE 15, a small number of clarifications, enhancements and fixes have been identified. The feature will preview for a second time in JDK 16.
Problem
Following testing, as well as feedback and discussions, the following issues with the "Sealed Classes" preview feature have been identified:
-
The JLS is not clear about the use of context when applying the lexical grammar, particularly in the identification of what is described as contextual keywords. This affects, in particular, the new keywords introduced by this JEP:
sealed,non-sealed, andpermits. -
A local class is not be permitted to extend a
sealedclass or implement asealedinterface. Accordingly a local class should not be considered when determining the implicitly permitted subclasses of asealedclass or interface. -
The current specification for narrowing reference conversions is too permissive, in that knowledge of sealed hierarchies could be used to identify erroneous casts and
instanceofexpressions at compile-time. -
It has been suggested that the return type of method
java.lang.Class::permittedSubclassesshould not bejava.lang.constant.ClassDesc[], both for consistency issues and because it would force users to use thejava.lang.constantAPI which is mostly intended for advanced users. It has also been suggested that the name of the method should begetPermittedSubclassesalso on consistency basis.
Solution
-
Clarify the use of context when applying the lexical grammar, in particular by introducing a first class notion of contextual keywords (formerly described as "restricted identifiers" and "restricted keywords"). Then the character sequences
sealed,non-sealed, andpermitscan be defined as contextual keywords. -
Do not consider local classes when calculating the implicitly declared permitted direct subclasses of a
sealedclass or interface. -
Extend the notion of a narrowing reference conversion to explicitly navigate sealed hierarchies to determine if a conversion can be rejected at compile-time.
-
Change the return type of method
java.lang.Class::permittedSubclassestojava.lang.Class<?>[]and rename it asgetPermittedSubclasses
Specification
-
A new companion specification document that clarifies the treatment of contextual keywords is attached.
-
Section 8.1.6 Permitted Direct Subclasses, has been modified to state that if asealedclass C lacks apermitsclause, local classes are not considered as permitted direct subclasses of C. -
Section 5.1.6.1 Allowed Narrowing Reference Conversionhas been modified in order to clarify the cases for which a narrowing reference conversion exist ifsealedclasses or interfaces are involved in the conversion. -
The specification for method
java.lang.Class::permittedSubclasseshas been updated as follows:
- * Returns an array containing {@code ClassDesc} objects representing all the
+ * Returns an array containing {@code Class} objects representing all the
* direct subclasses or direct implementation classes permitted to extend or
* implement this class or interface if it is sealed. The order of such elements
* is unspecified. 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
+ * @return an array of class objects 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 Class<?>[] getPermittedSubclasses() {}
- csr of
-
JDK-8246778 Compiler implementation for Sealed Classes (Second Preview)
-
- Resolved
-
- relates to
-
JDK-8246776 JLS changes for Sealed Classes (Second Preview)
-
- Resolved
-
-
JDK-8246777 JVMS changes for Sealed Classes (Second Preview)
-
- Resolved
-
-
JDK-8246278 Refine API for sealing in java.lang.Class
-
- Closed
-