小标
2018-10-12
来源 :
阅读 1489
评论 0
摘要:本文主要向大家介绍了Java语言 对象的深拷贝代码实例,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了Java语言 对象的深拷贝代码实例,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
package com.ctrip.ibu.itinerary.common.util;
import com.google.common.primitives.Primitives;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Unsafe;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.IdentityHashMap;
import java.util.Map;
/**
* The utility to deep copy the given object.
*/
public final class CopyUtils {
private static final Logger log = LoggerFactory.getLogger(CopyUtils.class);
private CopyUtils() {
}
/**
* Deep-copy the given object.
*
* @param src the source object to be deep-copied.
* @param <T> the type of the given object.
* @return the deep-copied object.
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws InstantiationException
*/
public static <T> T copy(T src) throws IllegalAccessException, NoSuchFieldException, InstantiationException {
return doCopy(src, new IdentityHashMap<>());
}
/**
* Deep-copy the given object.
*
* @param src the source object to be deep-copied.
* @param visited the copied object collection.
* @param <T> the type of the given object.
* @return the deep-copied object.
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws InstantiationException
*/
private static <T> T doCopy(T src, Map<Object, Object> visited) throws IllegalAccessException, NoSuchFieldException, InstantiationException {
if (src == null) {
return null;
}
if (src.getClass().isAssignableFrom(String.class)
|| src.getClass().isEnum()
|| src.getClass() == Class.class) {
return src;
}
if (visited.containsKey(src)) {
return (T) visited.get(src);
}
if (src.getClass().isArray()) {
return copyArray(src, visited);
}
return copyObject(src, visited);
}
/**
* Gets the unsafe object to create a new instance.
*
* @return the {@link Unsafe} object.
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (Unsafe) f.get(null);
}
/**
* Deep-copy the array object.
*
* @param src the given object with array type.
* @param visited the copied object collection.
* @param <T> the type of the element in the given array.
* @return the deep-copied array object.
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws InstantiationException
*/
private static <T> T copyArray(T src, Map<Object, Object> visited) throws IllegalAccessException, NoSuchFieldException, InstantiationException {
int length = Array.getLength(src);
Object result =
Array.newInstance(
src.getClass().getComponentType(),
Array.getLength(src));
visited.put(src, result);
for (int i = 0; i < length; i++) {
Array.set(result, i, doCopy(Array.get(src, i), visited));
}
return (T) result;
}
/**
* Checks if the given field with the given modifier.
*
* @param field the given field to check.
* @param modifier the modifier of the given field.
* @return {@code true} if the field has the given modifier, otherwise {@code false}.
*/
private static boolean hasModifier(Field field, int modifier) {
return (field.getModifiers() & modifier) != 0;
}
/**
* Deep-copy the given object with a new instance.
*
* @param src the given object to be deep-copied.
* @param visited the copied object collection.
* @param <T> the type of the given object.
* @return the deep-copied object instance.
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号