標籤:
TestCloneBean b = new TestCloneBean(); b.setIntegers(Lists.newArrayList(1)); String s = JsonUtils.getObjectMapperInstance().writeValueAsString(b); TestCloneBean a = JsonUtils.getObjectMapperInstance().readValue(s, TestCloneBean.class);
1.先說一句
不去覆蓋clone方法,不去調用clone方法,除非真的有必要。
2.java中clone兩個概念淺複製 copy的是引用深複製 copy的是執行個體,開闢新的堆空間 java中的clone方法實現的是淺複製,一個類可被淺複製需實現Cloneable(此介面只是說明此類允許clone,它改變的是超類中受保護的clone方法的行為),如effective java中的第11條所說這是介面的一種極端非典型的用法,不值得效仿。3.淺複製淺複製看上去只要實現了Cloneable介面 且@Override clone方法為public後就可以了,但是實際應用了,確定你要的是淺clone嗎?若是一個對象沒有實現Cloneable介面,也可以很簡單的使用反射實現對象的淺複製:非嚴謹的code如下:
public static <T> T simpleClone(T obj) throws IllegalAccessException, InstantiationException { Class<T> c = (Class<T>) obj.getClass(); T cloneC = c.newInstance(); Field[] fields = c.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { field.setAccessible(true); Object value = field.get(obj); field.set(cloneC, value); } } return cloneC; }
當然有很多工具類了:比如,spring的BeanUtils.copyProperties, apache的BeanUtils.copyProperties,cglib或者spring-cglib的BeanCopier
4.深複製既然實際應用中更希望使用的是深複製,那麼如何?呢1> bean 自己實現Cloneable介面,靠譜的實現clone方法非嚴謹code如下:
public class TestCloneBean implements Cloneable { private List<Integer> integers; public List<Integer> getIntegers() { return integers; } public void setIntegers(List<Integer> integers) { this.integers = integers; } @Override public TestCloneBean clone() { try { TestCloneBean t = (TestCloneBean) super.clone(); t.setIntegers(Lists.<Integer> newArrayList()); if (CollectionUtils.isNotEmpty(integers)) { for (Integer i : integers) { t.getIntegers().add(i); } } return t; } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } } @Override public String toString() { return ToStringBuilder.reflectionToString(this); }}
2>第一種方法你會這麼做嗎?不會。那麼只能另外一種方式:序列化啊!非嚴謹code如下:
public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); static { objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); objectMapper.configure(JsonParser.Feature.INTERN_FIELD_NAMES, true); objectMapper.configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true); objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.setSerializationInclusion(Inclusion.NON_NULL); } public static ObjectMapper getObjectMapperInstance() { return objectMapper; } }
testCode:
TestCloneBean b = new TestCloneBean(); b.setIntegers(Lists.newArrayList(1)); String s = JsonUtils.getObjectMapperInstance().writeValueAsString(b); TestCloneBean a = JsonUtils.getObjectMapperInstance().readValue(s, TestCloneBean.class);
java中的clone