I have the following class structure:
public class GenClass<T> {
public T elem;
}
I use it in the following way:
public class Test {
public GenClass<UUID> data;
Now I want to get the type of elem using the Field object of data(Test.class.getField("data")
)
But when I use getType
to retrieve the class the Generic information is stripped away.
How can I map the generic Information from getGenericType
to the class object to retrieve the field with a correct type?
Edit: Since there are some misunderstandings, I try to clarify my problem. Consider this example:
public class AClass<T, Q> {
public Q elem;
// some other code using T...
}
public class BClass<T, Q> {
public T elem;
// some other code using Q...
}
Now I want a function to get the class of elem:
public class Test {
public AClass<UUID, String> a;
public BClass<Integer, Float> b;
void do() throws Exception {
Field aField = Test.class.getField("a");
Field bField = Test.class.getField("b");
getType(aField, "elem"); // should return String.class
getType(bField, "elem"); // should return Integer.class
}
Class<?> getType(Field f, String classField) {
// ???
}
}
How did I need to implement getType
to get my desired result?