Class c = ArrayList. class;
C. isPrimitive (); // determines whether c is a basic data type.
C. isAssignableFrom (List. class); // determines whether c is a subclass or parent class of the List class.
C. getGenericType (); // obtain the generic type.
Instance: Obtain the generic type in the List <T> set through reflection.
[Java]
Package com.zf.tar get;
Import java. lang. reflect. Field;
Import java. lang. reflect. ParameterizedType;
Import java. lang. reflect. Type;
Import java. util. List;
Import java. util. Map;
Class T {
List <A>;
List <B> B;
// List l;
Map <Integer, String> map;
Int c;
}
Class {}
Class B {}
Public class Test9 {
Public static void main (String [] args ){
Class tc = T. class;
Field [] fields = tc. getDeclaredFields ();
For (Field f: fields ){
Class fc = f. getType ();
If (fc. isPrimitive ()){
System. out. println ("basic data type:" + f. getName () + "" + fc. getName ());
} Else {
If (fc. isAssignableFrom (List. class) {// you can check whether it is a List.
System. out. println ("List type:" + f. getName ());
Type gt = f. getGenericType (); // obtain the generic Type.
ParameterizedType pt = (ParameterizedType) gt;
Class lll = (Class) pt. getActualTypeArguments () [0];
System. out. println ("\ t" + lll. getName ());
}
}
}
}
}