一、前言
基于dorado.core.jar开源包中的com.bstek.dorado.util.CloneUtils克隆工具类,对对象进行通过java.lang.reflect.Method映射克隆复制操作。
二、源码说明
package com.bstek.dorado.util;@b@@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@@b@public abstract class CloneUtils@b@{@b@ private static final String CLONE_METHOD = "clone";@b@ private static final Class<?>[] CLONE_METHOD_ARGTYPES = new Class[0];@b@ private static final Object[] CLONE_METHOD_ARGS = new Object[0];@b@@b@ public static Object clone(Object object)@b@ throws CloneNotSupportedException@b@ {@b@ Object clonedObject = null;@b@ Class cl = object.getClass();@b@ Method method = null;@b@ try {@b@ do@b@ try {@b@ method = cl.getDeclaredMethod("clone", CLONE_METHOD_ARGTYPES);@b@ }@b@ catch (NoSuchMethodException e)@b@ {@b@ cl = cl.getSuperclass();@b@ }@b@ while (method == null);@b@@b@ if (method != null) {@b@ boolean methodAccessible = method.isAccessible();@b@ if (!(methodAccessible)) method.setAccessible(true);@b@ try {@b@ clonedObject = method.invoke(object, CLONE_METHOD_ARGS);@b@ }@b@ finally {@b@ if (!(methodAccessible)) method.setAccessible(false);@b@ }@b@ }@b@ }@b@ catch (SecurityException e) {@b@ e.printStackTrace();@b@ }@b@ catch (InvocationTargetException e) {@b@ e.getCause().printStackTrace();@b@ }@b@ catch (IllegalArgumentException e) {@b@ e.printStackTrace();@b@ }@b@ catch (IllegalAccessException e) {@b@ e.printStackTrace();@b@ }@b@ return clonedObject;@b@ }@b@}