You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

16 lines
688 B

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
public class GenericReflect {
private List<String> list;
public static void main(String[] args) throws NoSuchFieldException {
//利用反射获取被擦除的泛型
Field field = GenericReflect.class.getDeclaredField("list");
Type type = field.getGenericType();
if(type instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType) type;
System.out.println("获取到的泛型类型:"+parameterizedType.getActualTypeArguments()[0]);
}
}
}