@Test public void shallowCopyChangingOriginal() { D d = new D("ddd"); A a = new A("xpto", 2, null, d); A clone = Cloner.shallowCopy(a); a.setA("blah"); a.setB(100); a.setC(new Object()); a.getD().setD("xxx"); Assert.assertEquals("xpto", clone.a); Assert.assertEquals(2, clone.b); Assert.assertNull(clone.c); Assert.assertEquals("xxx", clone.d.d); }
@SuppressWarnings("unchecked") public static <T> T shallowCopy(T original) { Class<? extends T> clazz = (Class<? extends T>) original.getClass(); BeanClass<? extends T> beanClass = (BeanClass<? extends T>) classes.get(clazz); if (beanClass == null) { beanClass = BeanClass.buildFrom(clazz); classes.put(clazz, beanClass); } return shallowCopyHelper(original, beanClass); }
Cloner { @SuppressWarnings("unchecked") public static <T> T shallowCopy(T original) { Class<? extends T> clazz = (Class<? extends T>) original.getClass(); BeanClass<? extends T> beanClass = (BeanClass<? extends T>) classes.get(clazz); if (beanClass == null) { beanClass = BeanClass.buildFrom(clazz); classes.put(clazz, beanClass); } return shallowCopyHelper(original, beanClass); } }
Cloner { @SuppressWarnings("unchecked") public static <T> T shallowCopy(T original) { Class<? extends T> clazz = (Class<? extends T>) original.getClass(); BeanClass<? extends T> beanClass = (BeanClass<? extends T>) classes.get(clazz); if (beanClass == null) { beanClass = BeanClass.buildFrom(clazz); classes.put(clazz, beanClass); } return shallowCopyHelper(original, beanClass); }  }
Cloner { @SuppressWarnings("unchecked") public static <T> T shallowCopy(T original) { Class<? extends T> clazz = (Class<? extends T>) original.getClass(); BeanClass<? extends T> beanClass = (BeanClass<? extends T>) classes.get(clazz); if (beanClass == null) { beanClass = BeanClass.buildFrom(clazz); classes.put(clazz, beanClass); } return shallowCopyHelper(original, beanClass); }  @SuppressWarnings("unchecked") static T shallowCopy(T original); }
Cloner { @SuppressWarnings("unchecked") public static <T> T shallowCopy(T original) { Class<? extends T> clazz = (Class<? extends T>) original.getClass(); BeanClass<? extends T> beanClass = (BeanClass<? extends T>) classes.get(clazz); if (beanClass == null) { beanClass = BeanClass.buildFrom(clazz); classes.put(clazz, beanClass); } return shallowCopyHelper(original, beanClass); }  @SuppressWarnings("unchecked") static T shallowCopy(T original);  }