/**
* 封装数据,针对targetClass拥有所有orignClass的属性,且非内部类的情况
* @param orignInstance
* @param targetInstance
* @param orignClass
* @param targetClass
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void encapsulateData(Object orignInstance, Object targetInstance, Class orignClass, Class targetClass)
throws IllegalAccessException, InvocationTargetException {
Field[] fs = orignClass.getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Method getMethod = ReflectUtil.getMethod(fs[i], orignClass);
Method setMethod = ReflectUtil.setMethod(fs[i], targetClass, fs[i].getType());
if (getMethod == null || setMethod == null) {
continue;
}
setMethod.invoke(targetInstance, getMethod.invoke(orignInstance));
}
}
反射获取set,get的工具类
反射:数据封装
最新推荐文章于 2024-02-22 19:49:58 发布