How To Instantiate a java.util.ArrayList with Generic Class Using Reflection

后端 未结 6 1301
野的像风
野的像风 2020-12-10 13:37

How To Instantiate a java.util.ArrayList with Generic Class Using Reflection? I am writing a method that sets java.util.List on target object. A target object and a generic

6条回答
  •  清歌不尽
    2020-12-10 14:26

    Generics is largely a compile time feature. What you are trying to do is the same as

    public static void initializeList(Object targetObject, PropertyDescriptor prop, String gtype) {
        prop.getWriteMethod().invoke(targetObject, new ArrayList());
    }
    

    Note: This could change with Types in Java 7.

提交回复
热议问题