views:

42

answers:

1

I have asked a question in : reflect a list object

I actually got my answer just want to understand why when do this I will hits illegalArgumentException : Can not set static final ArrayList SerialVersionUID to java.lang.long. But when I do one object reflect to another object no error.

List<ClassB> listB = (List<ClassB>) convert(listA, ArrayList.class); 
+1  A: 

There is a problem with the convert method when it tries to assign a final field. I suggest you modify the convert method as follows.

    for (Field targetField : targetClass.getDeclaredFields()) {
        if (!Modifier.isFinal(targetField.getModifiers())) {
            targetField.setAccessible(true);
            Field field =
                instance.getClass().getDeclaredField(targetField.getName());
            field.setAccessible(true);
            targetField.set(target, field.get(instance));
         }
     }
mR_fr0g
Why when reflect list got this problem if only object to object nothing happen ? Does it mean object to object it won't assign a final field? Sorry this is the part I don't understand