Java reflection to obtain generic type

This link: https://blog.csdn.net/hongxingxiaonan/article/details/49202613
           

 Application Java reflection technology, to obtain a class member, methods, and constructors are relatively easy, but in order to achieve the generic type which comprises a relatively difficult. Look at a simple example, and the role of each step is described in detail.
class Demo {public
Private List <Integer> List1;
Private List <the Set <Integer >> the ListSet;
public the Set <String> fun1 (the Map <Integer, String> Map) {return null;}

public static void getFieldGenericType () {
the try {
clazz = Demo.class class;
Field, Field clazz.getDeclaredField = ( "List1");
the type type = Field.getGenericType (); // obtain the type field
ParameterizedTypeImpl parameterizedType = (ParameterizedTypeImpl) type; // turn into strong specific implementation class
type [] genericTypes = parameterizedType.getActualTypeArguments () ; // acquisition of generic type comprising
System.out.println (genericTypes [0]);
} the catch (a NoSuchFieldException E) {
E.
}
}

Public static void main (String [] args) {

getFieldGenericType ();
}
}
        The above code correctly printed out list1 members of the class Demo generic type Integer. The first step in obtaining a code by getGenericType type Type () method. Type this in fact represents a List <Interger>, Type implementation class can be Class, can also be ParameterizedTypeImpl. If the field type is a generic through getGenericType () is taken to ParameterizedTypeImpl, if it is taken into the normal class is Class. We get to the compiler is not generic Class information, the information acquisition method by getType List field is only, not List <Interger>, so we obtain the generic type by the above method; first type the strong two-step turned into ParameterizedTypeImpl type, here stole a lazy, knowing that there is no real type to determine the specific type of implementation class, do not write during development; the fourth step from ParameterizedTypeImpl get into all pan type type. ParameterizedTypeImpl is a parameterized type, generic type we want to get the equivalent parameters type, there are several generic type, getActualTypeArguments () returns the number of Type parameter.
        Type just said Class is a subclass, and () acquired by getActualTypeArguments is a Type array, as if caught in an infinite loop to them. Yes, in a real code indeed exist nested generic type, for example, the second field listSet code above. With just based on how many layers to obtain nested generics can also be parsed. Parsing the ListSet generic code information are as follows:
public static void getFieldGenericType1 () {
the try {
Class clazz = Demo.class;
Field, Field clazz.getDeclaredField = ( "the ListSet");
the Type type = Field.getGenericType (); // obtain the field type
ParameterizedTypeImpl ParameterizedType = (ParameterizedTypeImpl) type; // turn into strong specific category
type [] genericTypes = parameterizedType.getActualTypeArguments () ; // generic type comprising the obtaining

ParameterizedTypeImpl setType = (ParameterizedTypeImpl) genericTypes [ 0]; Representative again // Set <Integer> is converted into a strong Type ParameterizedTypeImpl
Type [] = setTypeArguments setType.getActualTypeArguments ();
System.out.println(setTypeArguments[0]);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
         同样的道理,获取方法中的泛型信息也不在话下
public static void getMethodGenericType() {
try {
Class clazz = Demo.class;
Method method = clazz.getMethod("fun1", Map.class);
Type type = method.getGenericReturnType();
ParameterizedTypeImpl parameterizedType = (ParameterizedTypeImpl) type;
Type[] genericTypes = parameterizedType.getActualTypeArguments();
System.out.println("return generic type " + genericTypes[0]);

Type mapType = method.getGenericParameterTypes()[0];
ParameterizedTypeImpl mapParamType = (ParameterizedTypeImpl) mapType; // again representing Set <Integer> is converted into a strong Type ParameterizedTypeImpl
Type [] = mapArgs mapParamType.getActualTypeArguments ();
System.out.println ( "Generic Method First type param" + mapArgs [ 0]);
} the catch (Exception E) {
e.printStackTrace ();
}
}
        the above illustrates a method of the generic information parsing members and methods, but for an ordinary variable can be parsed information in his generic runtime ? The answer is no, because he's Class information for an ordinary variable we can only get, but also just been said Class is not generic information. If you want to get generic information, you must first get to ParameterizedTypeImpl can.
----------------
Disclaimer: This article is the original article CSDN bloggers "hongxingxiaonan", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/hongxingxiaonan/article/details/49202613

Guess you like

Origin www.cnblogs.com/kelelipeng/p/11949512.html