import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.Collection; import java.util.List; public class View { public Collection<String> c; public T<Integer> tt; public static void main(String[] args) throws NoSuchFieldException, SecurityException { T<Integer> t = new T(); TypeVariable<?>[] cs = t.getClass().getTypeParameters(); for (TypeVariable<?> c : cs) { // c instanceof ParameterizedType // 必须为false,class上不存在实际类型,只存在声明的类型,所以是TypeVariable System.out.println(c + " " + (c instanceof ParameterizedType) + " " + c.getGenericDeclaration()); } Type type = View.class.getDeclaredField("tt").getGenericType(); printType(type); printType(T2.class.getGenericSuperclass()); type = T.class.getDeclaredField("o").getGenericType(); printType(type); Type types [] = T.class.getMethods()[0].getGenericParameterTypes(); for(Type ty:types) printType(ty); } static void printType(Type type) { System.out.println("-----------------------------------------------"); if (ParameterizedType.class.isAssignableFrom(type.getClass())) { ParameterizedType pt = (ParameterizedType) type; System.out.println("is ParameterizedType..........."); System.out.println("typeName:" + pt.getTypeName() + " ownerType:" + pt.getOwnerType() + " rawType:" + pt.getRawType()); Type[] typs = pt.getActualTypeArguments(); for (Type tttt : typs) System.out.println(" getActualTypeArguments "+tttt.getTypeName()); } else if (TypeVariable.class.isAssignableFrom(type.getClass())) { System.out.println("not ParameterizedType that is TypeVariable"); TypeVariable t = (TypeVariable) type; System.out.println("getGenericDeclaration " + t.getGenericDeclaration()); System.out.println("name: " + t.getName()); System.out.println("bounds:" + t.getBounds()[0].getTypeName()); } else if(GenericArrayType.class.isAssignableFrom(type.getClass())) { System.out.println("GenericArrayType..."); GenericArrayType genericArrayType = (GenericArrayType)type; printType(genericArrayType.getGenericComponentType()); } else { System.out.println("unknow....."+type.getTypeName()); } } public static class T2<O> extends T<O> { } public static class T<O> { O o; public void show(List<String>[] pTypeArray, O[] vTypeArray) { } } }