On 24 Mar, 17:54, Alan <
a...@malloys.org> wrote:
> On Mar 24, 9:47 am, Alessio Stalla <
alessiosta...@gmail.com> wrote:
>
> > Reflection is aware of generic type variables:
http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Generi...
>
> > What is lost at runtime is information about *instantiation* of those
> > variables: e.g. you can't distinguish a method returning List<String> from
> > one returning List<Integer>.
>
> I think you mean, you can't tell a List<String> from a List<Integer>
> once it's been returned. As you demonstrated, telling which kind of
> list a particular method will return can be done via reflection.
Only if the method is defined in a class that instantiates a type
variable, e.g. String get(int) in class MyList implements
List<String>, where the superclass is List<T> and defines T get(int).
But if you have class Foo<T> with a method T bar(), you can't tell via
reflection the distinction between new Foo<String>().bar() and new
Foo<Integer>().bar() - they're the same method of the same class at
runtime. Similarly if you have a method <T> List<T> foo(...) you can't
know about the different instantiations of T for calls to foo().
> > (Actually, not even all instantiations of
> > generic type variables are lost at runtime, seehttp://
gafter.blogspot.com/2006/12/super-type-tokens.htmlforan example).