ParameterizedType

 

   private static class ParameterizedTypeTest<T extends Number> {
        private List<T> list = null;

        public static void main(String[] args) throws NoSuchFieldException {
            testTypeVariable();
        }

        private Map.Entry<String, Integer> map = null;

        private static void getGetActualTypeArguments() throws NoSuchFieldException {
            Field fieldMap = ParameterizedTypeTest.class.getDeclaredField("map");
            ParameterizedType typeMap = (ParameterizedType) fieldMap.getGenericType();
            System.out.println(typeMap.getActualTypeArguments()[0]);
            System.out.println(typeMap.getActualTypeArguments()[1]);
            System.out.println(typeMap.getRawType());
            System.out.println(typeMap.getOwnerType());
        }

        private List<String>[] t;

        private static void testGenericArrayType() throws NoSuchFieldException {
            Field fieldArray = ParameterizedTypeTest.class.getDeclaredField("t");
            GenericArrayType typeArray = (GenericArrayType) fieldArray.getGenericType();
            System.out.println(typeArray.getClass().getName());
            System.out.println(typeArray.getGenericComponentType());
        }

        private List<T> t1;
        private T t2;

        private static void testTypeVariable() throws NoSuchFieldException {
            Field field = ParameterizedTypeTest.class.getDeclaredField("t1");
            ParameterizedType type = (ParameterizedType) field.getGenericType();
            System.out.println(type.getActualTypeArguments()[0].getClass().getName());

            Field field2 = ParameterizedTypeTest.class.getDeclaredField("t2");
            TypeVariable type2 = (TypeVariable) field2.getGenericType();
            System.out.println(type2.getBounds()[0]);
        }

    }

 

posted @ 2020-07-16 08:59  FrankYou  阅读(117)  评论(0编辑  收藏