ParameterizedType

Posted frankyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]);
        }

    }

 

以上是关于ParameterizedType的主要内容,如果未能解决你的问题,请参考以下文章

ParameterizedType获取java泛型参数类型

ParameterizedType

ParameterizedType类的使用

GenericDAO 与 Guice,玩泛型和 ParameterizedType

类的TypeVariable和变量的ParameterizedType

parameterizedType.getActualTypeArguments()[0]是什么意思?