I have a generic class as below
public class X<T> {
Integer x1;
T x2;
}
Is it possible to get the type of X by providing the class of certain T? Some thing like below code.
public <T> Class<?> getType(Class<T> cls) {
return X<cls>.class;
}
===================== news =======================
Thank you all for your answers. Let me explain more details of my question.
I just want to deserialize an Object of X. Let say T = Integer.
if use Gson, the code would be:
X x = new Gson().fromJson("{x1: 1, x2: 2}", new TypeToken<X<Integer>>(){}.getType());
but I don't want to write so long everytime. I am thinking if there is way can simplify the code to be as below:
X x = deserializeFunc("{x1: 1, x2: 2}", Integer.class);