getPermittedSubclasses() on j.l.rClass returning an array of ClassDesc

Chris Hegarty chris.hegarty at oracle.com
Tue Oct 27 17:15:16 UTC 2020



> On 27 Oct 2020, at 11:51, forax at univ-mlv.fr wrote:
> 
>> ...
>> Since the set of permitted classes must be subclasses of T, should the
>> declaration be:
>> 
>> public Class<? extends T>[] getPermittedSubclasses() { .. }
> 
> 
> Hi Chris,
> in theory yes,
> in practice, an array of parametrized types is usually unsafe, in this peculiar case, it's always unsafe because you can write
> 
> Class<? extends Itf>[] array = Itf.class.getPermittedSubclasses();
> Object[] array2 = array;
> array2[0] = Object.class;
> 
> Itf itf = array[0].newInstance();  // CCE, the compiler insert a cast to Itf and Object doesn't implement Itf

Thanks for the explanation Remi - I fell afoul of generics 101 ;-)  

-Chris.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20201027/41aa6777/attachment.htm>


More information about the amber-spec-experts mailing list