@Test public void deepCopy_A$T_paramClass() throws Exception { Object arg = new Object(); Object res = ObjectUtil.deepCopy(arg); assertNotSame(res, arg); }
@SuppressWarnings("unchecked") public static <T> T deepCopy(T obj) { try { if (obj == null) { return null; } Class<?> clazz = obj.getClass(); T clone = (T) clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } while (true) { if (Object.class.equals(clazz)) { break; } clazz = clazz.getSuperclass(); Field[] sFields = clazz.getDeclaredFields(); for (int i = 0; i < sFields.length; i++) { Field field = sFields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } } return clone; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } }
ObjectUtil { @SuppressWarnings("unchecked") public static <T> T deepCopy(T obj) { try { if (obj == null) { return null; } Class<?> clazz = obj.getClass(); T clone = (T) clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } while (true) { if (Object.class.equals(clazz)) { break; } clazz = clazz.getSuperclass(); Field[] sFields = clazz.getDeclaredFields(); for (int i = 0; i < sFields.length; i++) { Field field = sFields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } } return clone; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } } }
ObjectUtil { @SuppressWarnings("unchecked") public static <T> T deepCopy(T obj) { try { if (obj == null) { return null; } Class<?> clazz = obj.getClass(); T clone = (T) clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } while (true) { if (Object.class.equals(clazz)) { break; } clazz = clazz.getSuperclass(); Field[] sFields = clazz.getDeclaredFields(); for (int i = 0; i < sFields.length; i++) { Field field = sFields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } } return clone; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } } private  ObjectUtil(); }
ObjectUtil { @SuppressWarnings("unchecked") public static <T> T deepCopy(T obj) { try { if (obj == null) { return null; } Class<?> clazz = obj.getClass(); T clone = (T) clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } while (true) { if (Object.class.equals(clazz)) { break; } clazz = clazz.getSuperclass(); Field[] sFields = clazz.getDeclaredFields(); for (int i = 0; i < sFields.length; i++) { Field field = sFields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } } return clone; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } } private  ObjectUtil(); @SuppressWarnings("unchecked") static T deepCopy(T obj); static List<T> deepCopyList(List<T> arg); }
ObjectUtil { @SuppressWarnings("unchecked") public static <T> T deepCopy(T obj) { try { if (obj == null) { return null; } Class<?> clazz = obj.getClass(); T clone = (T) clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } while (true) { if (Object.class.equals(clazz)) { break; } clazz = clazz.getSuperclass(); Field[] sFields = clazz.getDeclaredFields(); for (int i = 0; i < sFields.length; i++) { Field field = sFields[i]; field.setAccessible(true); if (!Modifier.isFinal(field.getModifiers())) { if (field.get(obj) instanceof List<?>) { List<?> copiedList = deepCopyList((List<?>) field.get(obj)); field.set(clone, copiedList); } else { field.set(clone, field.get(obj)); } } } } return clone; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } } private  ObjectUtil(); @SuppressWarnings("unchecked") static T deepCopy(T obj); static List<T> deepCopyList(List<T> arg);  }