我做了以下守则:
public static <T> T mergeObjects(T draft, T existing)
throws IllegalAccessException, InstantiationException, NoSuchMethodException,
InvocationTargetException {
Class<?> clazz = draft.getClass();
Field[] fields = clazz.getDeclaredFields();
Object returnValue = clazz.getDeclaredConstructor().newInstance();
for (Field field : fields) {
field.setAccessible(true);
Object draftValue = field.get(draft);
Object existingValue = field.get(existing);
if (Objects.equals(existingValue, draftValue)) field.set(returnValue, existingValue);
else field.set(returnValue, draftValue);
}
return (T) returnValue;
}
但是,它给定点的幼 the问题。
- field.setAccessible(true);
- if (Objects.equals(existingValue, draftValue)) field.set(returnValue, existingValue);
- else field.set(returnValue, draftValue);
I have tried many things but not working. Can anyone know hoe to resolve it, please tell me.