Casts to a sealed class check that the input type is castable to one of the permitted subclasses (which may themselves be sealed or final). When casting from one array type to another, the same check should be performed for a cast between the component types.
sealed class A permits B, C {}
class B extends A {}
class C extends A {}
jshell> (A) (Runnable) null // compiler error
| Error:
| incompatible types: java.lang.Runnable cannot be converted to A
| (A) (Runnable) null
| ^-------------^
jshell> (A[]) new Runnable[5] // no compiler error
| Exception java.lang.ClassCastException: class [Ljava.lang.Runnable; cannot be cast to class [LREPL.$JShell$17B$A; ([Ljava.lang.Runnable; is in module java.base of loader 'bootstrap'; [LREPL.$JShell$17B$A; is in unnamed module of loader jdk.jshell.execution.DefaultLoaderDelegate$RemoteClassLoader @3764951d)
sealed class A permits B, C {}
class B extends A {}
class C extends A {}
jshell> (A) (Runnable) null // compiler error
| Error:
| incompatible types: java.lang.Runnable cannot be converted to A
| (A) (Runnable) null
| ^-------------^
jshell> (A[]) new Runnable[5] // no compiler error
| Exception java.lang.ClassCastException: class [Ljava.lang.Runnable; cannot be cast to class [LREPL.$JShell$17B$A; ([Ljava.lang.Runnable; is in module java.base of loader 'bootstrap'; [LREPL.$JShell$17B$A; is in unnamed module of loader jdk.jshell.execution.DefaultLoaderDelegate$RemoteClassLoader @3764951d)
- csr for
-
JDK-8294586 Sealed check for casts isn't applied to array components
-
- Closed
-