简体   繁体   中英

How to instantiate a List<someType> that is null and is inside an object using reflection? OR instantiate an object of the type of the list

I have an object that I need to parse and initialize. All fields of this object that are null and set all their string fields to "". The problem is when one of the fields of the object is List<someType> . Then I need to create a new object of this type and call the method on it again.

The field.getType() gives me the interface list, so that I can't do field.getType().newInstance() ;

I tried getting type of the list and tried to create an instance out of it, but this gives me exception :

Object obj1 = ((ParameterizedType) listType).getActualTypeArguments()[0].getClass().newInstance();

The complete code :

Field [] fields = segment.getClass().getDeclaredFields();
if (fields == null || fields.length < 0) {
    log.info("Field Array Null or Empty");
}
for (Field field : fields){
        if (field.getType().isAssignableFrom(List.class)) {
            if ( field.get(segment) == null) {
                log.info("++++++++++++++++++++++++ list is null +++++++++++++++++ = " + field.getName().getClass().getName() + " list type " + field.getType());
                //field.set(segment, field.getType().newInstance());
                //field.set(segment, (List<Object>) field.getType().newInstance());
                List<Object> list = new ArrayList<>();

                Type listType = field.getGenericType();
                if (listType instanceof ParameterizedType) {
                    Type elementType = ((ParameterizedType) listType).getActualTypeArguments()[0];
                    log.info("............ element type of list ............" + elementType);
                    Object obj1 = elementType.getClass().newInstance(); //HERE FAILS TO CREATE INSTANCE OF LIST TYPE
                    list.add(obj1);                            
                } 
                field.set(segment, list);
                for (Object obj : (List)field.get(segment)) {
                     log.info("++++++++++++++++++++++++ list segment +++++++++++++++++ = " + obj.toString());
                     populateUnusedFields(obj);
                }                       
          }
     }
 }

I need to create an object of the class specified as generic in the generic list.

Here :

Type elementType = ((ParameterizedType) listType).getActualTypeArguments()[0];

elementType refers to the generic type declared for the List field.
For example for a field List<String> xxx , the Type will be the java.lang.String Class instance.

And the problem is that then you do :

Object obj1 = elementType.getClass().newInstance(); //HERE FAILS TO CREATE INSTANCE OF LIST TYPE

But elementType.getClass() doesn't still return the java.lang.String Class. Instead of it returns the Class of java.lang.String Class that is just Class .

What you want is casting the Type to a Class to be able to use it then :

Class<?> clazz = (Class<?>) elementType;
Object obj1 = clazz.newInstance(); 

Or better since Class.newInstance() is deprecated :

Object obj1 = null;
try {
    obj1 = clazz.getDeclaredConstructor().newInstance();
} catch (InvocationTargetException e) {
    // handle the exception
} 

Note that a Type is not always a Class , it may be other things. But in the actual case, it will be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM