jeudi 23 avril 2020

Correlate Java field type and generic parameter

Consider the following classes:

class A {
    B<Integer> b;
}

class B<T> {
    T t;
}

I need to write the follwing method:

Object create(Class clazz) {
    // todo
}

such that when create(A.class) is called, the returned value is an instance of A which contains an Integer in b.t. Naturally the method must not have any knowledge about A besides its class.

It is well known how to find out that the type of b in A is B<Integer>.

I am looking for a way to find out that the field t in B is intended to have the type of the first type parameter of B.





Aucun commentaire:

Enregistrer un commentaire