/**
* 扩展类转化成父类
*
* @param sour
* @param clazz
* @param filters
* @return
* Object
* @exception/throws
*/
public static Object copyBean(Object sour, Class<?> clazz) {
Object target = null;
try {
target = ClassUtils.getInstanceByName(clazz.getName());
if (target == null)
return null;
BeanInfo sourBeanInfo = Introspector.getBeanInfo(sour.getClass());
PropertyDescriptor[] sourProperties = sourBeanInfo.getPropertyDescriptors();
BeanInfo targBeanInfo = Introspector.getBeanInfo(target.getClass());
PropertyDescriptor[] targProperties = targBeanInfo.getPropertyDescriptors();
List<?> listTarget = Arrays.asList(targProperties);
for (int i = 0; i < sourProperties.length; i++) {
String field = sourProperties[i].getName();
if ("class".equals(field)) {
continue;
}
if(!listTarget.contains(field)) {
continue;
}
Method getMethod = sourProperties[i].getReadMethod();
Method setMethod = sourProperties[i].getWriteMethod();
Object value = getMethod.invoke(sour, new Object[] {});
setMethod.invoke(target, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return target;
}
* 扩展类转化成父类
*
* @param sour
* @param clazz
* @param filters
* @return
* Object
* @exception/throws
*/
public static Object copyBean(Object sour, Class<?> clazz) {
Object target = null;
try {
target = ClassUtils.getInstanceByName(clazz.getName());
if (target == null)
return null;
BeanInfo sourBeanInfo = Introspector.getBeanInfo(sour.getClass());
PropertyDescriptor[] sourProperties = sourBeanInfo.getPropertyDescriptors();
BeanInfo targBeanInfo = Introspector.getBeanInfo(target.getClass());
PropertyDescriptor[] targProperties = targBeanInfo.getPropertyDescriptors();
List<?> listTarget = Arrays.asList(targProperties);
for (int i = 0; i < sourProperties.length; i++) {
String field = sourProperties[i].getName();
if ("class".equals(field)) {
continue;
}
if(!listTarget.contains(field)) {
continue;
}
Method getMethod = sourProperties[i].getReadMethod();
Method setMethod = sourProperties[i].getWriteMethod();
Object value = getMethod.invoke(sour, new Object[] {});
setMethod.invoke(target, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return target;
}