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