RFR: 8246778: Compiler implementation for Sealed Classes (Second Preview) [v2]

David Holmes david.holmes at oracle.com
Tue Dec 1 02:40:42 UTC 2020

On 1/12/2020 10:39 am, Mandy Chung wrote:
> On Mon, 30 Nov 2020 20:57:32 GMT, Harold Seigel <hseigel at openjdk.org> wrote:
>>> src/java.base/share/classes/java/lang/Class.java line 4480:
>>>> 4478:     }
>>>> 4479:
>>>> 4480:     private native Class<?>[] getPermittedSubclasses0();
>>> Does this JVM method return the permitted subclasses or subinterfaces with the following conditions enforced by JLS:
>>>     - If a sealed class C belongs to a named module, then every class named in the permits clause of the declaration of C must belong to the same module as C
>>>     - If a sealed class C belongs to an unnamed module, then every class named in the permits clause of the declaration of C must belong to the same package as C
>>> I didn't check the VM implementation.
>>> If the return array contains only classes as specified above, `checkPackageAccessForClasses` can be simplified.
>> The JVM method that returns the permitted subclasses (and interfaces) does not weed out permitted subclasses based on the above module requirements.  It returns all the classes listed in the PermittedSubclasses attribute that it is able to load.
> So it could also return a class listed in `PermittedSubclasses` attribute but not a subclass of this sealed class, right?
> The specification of `Class::getPermittedSubclasses` says:
>> Returns an array containing {@code Class} objects representing the direct subclasses or direct implementation classes permitted to extend or direct subinterfaces or subclasses permitted to extend or implement this class or interface if it is sealed.
> I expect that `Class::getPermittedSubclasses` should do more work and return only the subclasses or subinterfaces permitted at runtime.

These subtleties are what I was also getting at in the CSR discussion. 
If Foo lists Bar as a permitted subtype, but Bar does not extend Foo, is 
that an error? Should Bar not be returned by getPermittedSubclasses? The 
answer depends on exactly how you specify getPermittedSubclasses() and 
whether that aligns with reading the classfile attribute, or actually 
checking the runtime relationships.


> -------------
> PR: https://git.openjdk.java.net/jdk/pull/1483

More information about the compiler-dev mailing list