How to get the Generic type at runtime?

By trying to create a Java Persistence like API, I started using some generic lists to be more specific

Collection<MyObjectType> x =null;

And I would like through reflection to create objects ofMyObjectType, add them to an ArrayList and return it.

Why not simply create directlyMyObjectTypes? well, that is because hat is mainly an screen name, in fact

there could be many classes containing a Collection of any object type, and there is a Manager which will fill

it with that Collection's generic objects.

I haven't found any code that helps me serve that purpose, but by debugging a main application, I can see the

next thing.

Using reflection over the field, I get a Field object representation.

Field f;

...

Type t = f.getGenericType();

After executing the getGenericType() method, thef variable has aFieldRepository object

namedgenericInfo, which holds aParametrizedTypeImpl object namedgenericType which is the

type somehow returned by thegetGenericType() method as aType (returning

java.util.Collection<myPackage.MyObjectType>), which is not useful, since I'm looking

only formyPackage.MyObjectType.

Now, that genericType object has 3 main objects (actualTypeArguments, ownerType, rawType).

TheactualTypeArguments is the one we care about since it is an Type[] which finally contains, for this

case, inactualTypeArguments[0] themyPackage.MyObjectType type which is the one I need.

THE BIG PROBLEM IS, all these variables are private and I haven't found any public method that lets me

access them.

Any ideas, or have you found a way to do this I'm requiring?

Thanks in advance.

[1899 byte] By [@lphazygmaa] at [2007-11-26 23:55:10]
# 1
Do you know about "erasure"? Java generics are implemented using erasure, which meansthe generics type info are ERASED after compilation, so are not available at run time.See 6.2 in http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
KathyMcDonnella at 2007-7-11 15:38:43 > top of Java-index,Core,Core APIs...