Clone in Java

Source: Internet
Author: User

New Testclonebean ();        B.setintegers (Lists.newarraylist (1));         = jsonutils.getobjectmapperinstance (). writevalueasstring (b);         = Jsonutils.getobjectmapperinstance (). ReadValue (S, Testclonebean.  Class);

1. Say Something first

Do not overwrite the Clone method, do not call the Clone method unless it is really necessary.

2.java in the clone two concepts shallow clone copy is a reference to the deep clone copy is an instance, open up new heap space in Java, the Clone method is a shallow clone, a class can be shallow cloning to implement cloneable (this interface simply illustrates that this class allows clone, It changes the behavior of the protected clone method in the superclass, as the 11th article in effective Java says it is an extremely atypical usage of the interface and is not worth emulating. 3. Shallow clone shallow clone appears as long as the implementation of the Cloneable interface and the @override clone method is public, but the actual application, to determine that you want a shallow clone it? If an object does not implement the Cloneable interface, it is also easy to use reflection to implement shallow copying of objects: the non-rigorous code is as follows:
     Public Static<T> t Simpleclone (t obj)throwsillegalaccessexception, 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); }        }        returnClonec; }

Of course there are a lot of tools: for example, Spring beanutils.copyproperties, Apache Beanutils.copyproperties, Cglib or Spring-cglib beancopier.

4. Deep cloning since the actual application is more like the use of deep cloning, then how to achieve it 1> beans to implement the Cloneable interface, the reliable implementation of the Clone method is not strict code as follows:
 Public classTestclonebeanImplementscloneable {PrivateList<integer>integers;  PublicList<integer>getintegers () {returnintegers; }      Public voidSetintegers (list<integer>integers) {         This. integers =integers; } @Override PublicTestclonebean Clone () {Try{Testclonebean T= (Testclonebean)Super. Clone (); T.setintegers (Lists.<Integer>newarraylist ()); if(collectionutils.isnotempty (integers)) { for(Integer i:integers) {t.getintegers (). Add (i); }            }            returnT; } Catch(clonenotsupportedexception e) {Throw NewRuntimeException (e); }} @Override PublicString toString () {returnTostringbuilder.reflectiontostring ( This); }}

2> the first way, would you do that? No. Then there is only one other way: serialization Ah! The non-rigorous code is as follows:
 Public classJsonutils {Private StaticObjectmapper Objectmapper =NewObjectmapper (); 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 Staticobjectmapper getobjectmapperinstance () {returnObjectmapper; } } 

Testcode:
New Testclonebean ();        B.setintegers (Lists.newarraylist (1));         = jsonutils.getobjectmapperinstance (). writevalueasstring (b);         = Jsonutils.getobjectmapperinstance (). ReadValue (S, Testclonebean.  Class);

Clone in Java

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.