java深度克隆大数据_Java - 深拷贝技巧
先讓我描述一下問題:
我在某Action(struts2.x)-A中寫了一個功能P,以當(dāng)前用戶的某個標(biāo)識F == 1時需要走這個功能,而且這個功能因某些原因已經(jīng)侵入到了其他一些method中。
順便一提,A中獲得當(dāng)前用戶session的method已經(jīng)被父類封裝好了。
然后我的代碼已經(jīng)push上去了,第二天有人告訴我能不能暫時去掉這個功能。
一個個注釋掉太麻煩了,
于是我決定在這個A中override獲得當(dāng)前用戶session的method,并將F賦值為0。
于是我只需要來個shallow copy就可以了。
比如我可以這樣:
給User來個implements Clonable
然后在getUserInfo()的Override中clone一個出來再賦值setF(0)
但這也許不太好,畢竟我需要動User。
我可以直接使用org.springframework.beans.BeanUtils.copyPropergties(source,target)
看了看源碼,里面又是sourcepd又是targetpd,pd是什么?
就當(dāng)他是用來描述java bean的媒介好了。
當(dāng)然,他也是shallow copy...
for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() != null &&
(ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null && sourcePd.getReadMethod() != null) {
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
} catch (Throwable ex) {
throw new FatalBeanException("Could not copy properties from source to target", ex);
}
}
}
}
無論如何,這個已經(jīng)解決我的問題了,A中調(diào)用getUserInfo()都是我clone的User,不會影響其他的Action.
但如果我那天用User下某個引用類型的Field的某個simple type的Field做標(biāo)記呢?
那我得deep clone,平時掌握的類庫不多,讓我自己解決的話我怎么弄?
也許我可以這樣做:
File f = new File("@#$%^&*");
f.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(u0);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
u1 = (User)ois.readObject();
光看一小段代碼感覺是個簡單粗暴的好辦法,只不過我得User及其下那些引用類型加上implements Serializable...
后來我找到了這樣一個東西:
uk.com.robust-it
cloning
1.9.0
可以這樣使用:
com.rits.cloning.Cloner cloner = new com.rits.cloning.Cloner();
u1 = cloner.deepClone(u0);
u1.getPet().setName("papapa");
System.out.println(u0);
System.out.println(u1);
輸出結(jié)果是clone后的u1的pet的名字變成了papapa而作為clone source的u0沒有任何變化,這就是deep clone.
忽略clone source是數(shù)組的情況,這個類進行deep clone的關(guān)鍵部分如下:
for (final Field field : fields) {
final int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers)) {
if (nullTransient && Modifier.isTransient(modifiers)) {
// request by Jonathan : transient fields can be null-ed
final Class> type = field.getType();
if (!type.isPrimitive()) {
field.set(newInstance, null);
}
} else {
final Object fieldObject = field.get(o);
final boolean shouldClone = (cloneSynthetics || (!cloneSynthetics && !field.isSynthetic())) && (cloneAnonymousParent || ((!cloneAnonymou sParent && !isAnonymousParent(field))));
final Object fieldObjectClone = clones != null ? (shouldClone ? cloneInternal(fieldObject, clones) : fieldObject) : fieldObject;
field.set(newInstance, fieldObjectClone);
if (dumpClonedClasses && fieldObjectClone != fieldObject) {
System.out.println("cloned field>" + field + " -- of class " + o.getClass());
}
}
}
}
遞歸下去找field的引用的引用的引用的引用....然后全是他們newInstance...
總結(jié)
以上是生活随笔為你收集整理的java深度克隆大数据_Java - 深拷贝技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java坦克大战图片素材包_坦克大战图片
- 下一篇: HTML实现在线取色器