I have generic class :
class Field<T> { }
And some other class (whose type I know only at runtime) with many get methods for example :
class A{
public Date getDate();
public String getName();
public Integer getNumber();
}
I want to create instances of class Field
for all get methods, with T
equals to return type of these get
methods. For this example Field<Date>
, Field<String>
, Field<Integer>
.
Can anybody help?
You use reflection typically for things that you only know at run-time. Generics information is erased at compile-time. So, while there are cases where you can mix the two, it is not common.
I want to create instances of class Field for all get methods, with T equals to return type of these get methods. For this example Field, Field, Field.
To answer your question literally:
Field<Date> dateField = new Field<Date>();
Field<String> nameField = new Field<String>();
Field<Integer> numberField = new Field<Integer>();
I don't believe you can create generic types of Field in this case, since generics are checked at compile time, while you are only able to get (via reflection) the return types of the declared methods of class A at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With