43 _ obtain the actual type parameters of the generic type through reflection, 43 type parameters
What frameworks often do,
Obtain the data type based on the method signature, convert it to the corresponding object, and fill it in.
Package com. itcast. day2; import java. lang. reflect. method; import java. lang. reflect. parameterizedType; import java. lang. reflect. type; import java. util. arrayList; import java. util. collection; import java. util. date; import java. util. vector; import com. itcast. day1.ReflectPoint; public class GenericExc {public static void main (String [] args) throws Exception {Method applyMethod = GenericExc. class. getMethod ("apply Vector ", Vector. class); // Type is a common high-level interface of all types in Java programming language. They include the original type, parameterized type, array type, type variable, and basic type. Type [] types = applyMethod. getGenericParameterTypes (); ParameterizedType pType = (ParameterizedType) types [0]; // ParameterizedType is a Type sub-interface System. out. println (pType. getRawType (); // obtain the original type System. out. println (pType. getActualTypeArguments () [0]); // get the actual type // when applyVector (Vector v), java. lang. class
// ApplyVector (Vector <Date> v) sun. reflect. generics. reflectiveObjects. parameterizedTypeImpl System. out. println (types [0]. getClass (); // After jdk1.8, a simple method is provided to directly obtain java. util. vector <java. util. date> System. out. println (types [0]. getTypeName ();} public void applyVector (Vector <Date> v ){}}