> On Oct 27, 2020, at 5:51 AM, [email protected] wrote:
> 
> ----- Mail original -----
>> De: "Chris Hegarty" <[email protected]>
>> À: "Vicente Romero" <[email protected]>
>> Cc: "Remi Forax" <[email protected]>, "amber-spec-experts" 
>> <[email protected]>
>> 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'.

Reply via email to