Java Basics-Deep cloning supplement

Source: Internet
Author: User
Tags what interface

Deep cloning articles a lot, here is recommended Java enhancement-object cloning (replication).
The above article is clear-organized, at a glance, but recently encountered in the project of the actual problem is that the object is to be cloned with a total of 207 subclass parent, regardless of the above two ways, it is necessary to modify the class file, more cumbersome.
So we try Spring's beanutils.copyproperties and prove that the Copyproperties method is not used for deep cloning. Finally holding the mentality of the attempt to use Jackson, what interface is not realized, even succeeded.

Codemaven
        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.13</version>        </dependency>
Realize
    @SuppressWarnings("all")    publicstaticdeepClonethrows IOException {        if (null == t) {            returnnull;        }        newObjectMapper();        byte[] bytes = objectMapper.writeValueAsBytes(t);        return (T) objectMapper.readValue(bytes, t.getClass());    }
Problem

Although generics are used here, it is not possible to clone all objects, such as the List<Person> --person type is a custom class--The returned type becomes List<LinkedHashMap<String,Object>> , so this method only fits into a single object.

Java Basics-Deep cloning supplement

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.