Java clone, javaclone

Source: Internet
Author: User

Java clone, javaclone
 

TestCloneBean b = new TestCloneBean();        b.setIntegers(Lists.newArrayList(1));        String s = JsonUtils.getObjectMapperInstance().writeValueAsString(b);        TestCloneBean a = JsonUtils.getObjectMapperInstance().readValue(s, TestCloneBean.class);
 

 

1. First

Do not overwrite the clone method or call the clone method unless necessary.

2. in java, clone two concepts. clone copy refers to instances that reference deep clone copy, and opens up new heap space. In java, the clone method implements shallow clone, cloneable needs to be implemented for the shortest cloning of a class (this interface only indicates that this class allows clone, which changes the behavior of the protected clone method in the super class ), as article 11th in objective java says, this is an extremely atypical use of interfaces and is not worth doing. 3. The shortest clone seems that you only need to implement the Cloneable interface and the @ Override clone method is public. But it is actually used. Are you sure you want shortest clone? If an object does not implement the Cloneable interface, you can easily use reflection to implement the shallow copy of the object: the non-rigorous code is as follows:
    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;    }

Of course, there are many tool classes: for example, BeanUtils. copyProperties of spring, BeanUtils. copyProperties of apache, cglib or BeanCopier of spring-cglib

4. Since deep cloning is more expected to be used in actual applications, how can it be implemented? 1> bean implements the Cloneable interface by itself. The unreliable clone method is not the rigorous code below:
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> will you do this in the first method? No. There is only one other way: serialization! The non-rigorous code is as follows:
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);

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.