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

后端 未结 6 1293
野的像风
野的像风 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:17

    There's no need to do any reflection for creating your List. Just pass in some additional type information (usually done by passing a class of the correct type).

    public static  List createListOfType(Class type) {
        return new ArrayList();
    }
    

    Now you have a list of the required type you can presumably/hopefully set it directly on your targetObject without any reflection.

提交回复
热议问题