getPermittedSubclasses() on j.l.rClass returning an array of ClassDesc
Dan Smith
daniel.smith at oracle.com
Thu Oct 29 21:38:08 UTC 2020
> On Oct 27, 2020, at 5:51 AM, forax at univ-mlv.fr wrote:
>
> ----- Mail original -----
>> De: "Chris Hegarty" <chris.hegarty at oracle.com>
>> À: "Vicente Romero" <vicente.romero at oracle.com>
>> Cc: "Remi Forax" <forax at univ-mlv.fr>, "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
>> Envoyé: Mardi 27 Octobre 2020 12:45:13
>> Objet: Re: getPermittedSubclasses() on j.l.rClass returning an array of ClassDesc
>
>> While I agree with all comments in this thread so far, I have one additional
>> comment relating to the type parameter.
>>
>> 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
You're not wrong, but I'm not sure this is reason not to provide better typing information. It certainly is the contract of the 'getPermittedSubclasses' method not to pollute it with non-subclasses of T.
That said, Class<?>[] seems to be the precedent followed by other methods:
Class<? super T> getSuperclass()
vs.
Class<?>[] getInterfaces()
Constructor<T> getConstructor(Class<?>)
vs.
Constructor<?>[] getConstructors()
So I guess we should do the same with 'getPermittedSubclasses'.
More information about the amber-spec-experts
mailing list