### Question:
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);  }### Answer:
@Test public void shallowCopySimple() { D d = new D("ddd"); A a = new A("xpto", 2, null, d); A clone = Cloner.shallowCopy(a); Assert.assertEquals(a, clone); }
@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); }
@Test public void shallowCopyInheritanceSimple() { G g = new G(); g.setA("aaa"); g.setB(5); g.setC(null); g.setD(new D("abcD")); g.setF(true); g.setG("ggg"); G clone = Cloner.shallowCopy(g); Assert.assertEquals(g, clone); }
@Test public void shallowCopyInheritanceChangingOriginal() { G g = new G(); g.setA("aaa"); g.setB(5); g.setC(null); g.setD(new D("abcD")); g.setF(true); g.setG("ggg"); G clone = Cloner.shallowCopy(g); g.setA("AAA"); g.setB(10); g.setC(new Object()); g.setD(new D("ABCd")); g.setF(false); g.setG("GGG"); Assert.assertEquals("aaa", clone.getA()); Assert.assertEquals(5, clone.getB()); Assert.assertNull(clone.getC()); Assert.assertEquals("abcD", clone.getD().getD()); Assert.assertEquals(true, clone.isF()); Assert.assertEquals("ggg", clone.getG()); }
@Test public void testFinalAttributes() { H h = new H(); h.c = "c2"; assertEquals("c2", Cloner.shallowCopy(h).c); }