I would like to use a type which I get from a class’ field (using reflection) to instantiate a class with generics.
Note: I omitted the exceptions hoping for easy reading.
public class AClass {
class BClass<T> {
T aMemba;
}
public void AMethod() {
Class c = Class.forName("com.bla.flipper");
Field f = c.getField("flipIt");
// Here is my difficulty, I want to instantiate BClass with the type of
// field 'f' but the compiler won't let me.
Class typeClass = f.getType();
BClass<typeClass> = new BClass<typeClass>();
}
}
Is what I want to achieve reasonable? Any thought on how I can solve this problem?
Thanks!
You can capture the type argument of the type of typeClass: