package com.platform.utils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.PropertyUtilsBean;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
/**
* bean 对象工具类
* 描述:<br>
*
* @author 李鹏军
* @version 1.0
* @since 1.0.0
*/
public class MyBeanUtils extends PropertyUtilsBean {
/**
* 主要功能:转换
* 注意事项:无
*
* @param dest 结果对象
* @param orig 来源对象
* @throws IllegalAccessException 异常
* @throws InvocationTargetException 异常
*/
@SuppressWarnings("all")
private static void convert(Object dest, Object orig)
throws IllegalAccessException,
InvocationTargetException {
// Validate existence of the specified beans
if (dest == null) {
throw new IllegalArgumentException("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
// Copy the properties, converting as necessary
if (orig instanceof DynaBean) {
DynaProperty[] origDescriptors = ((DynaBean) orig).getDynaClass().getDynaProperties();
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ((DynaBean) orig).get(name);
try {
getInstance().setSimpleProperty(dest, name, value);
} catch (Exception e) {
; // Should not happen
}
}
}
} else if (orig instanceof Map) {
Iterator names = ((Map) orig).keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ((Map) orig).get(name);
try {
getInstance().setSimpleProperty(dest, name, value);
} catch (Exception e) {
; // Should not happen
}
}
}
} else { /* if (orig is a standard JavaBean) */
PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(
orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(orig, name)
&& PropertyUtils.isWriteable(dest, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(orig,
name);
getInstance().setSimpleProperty(dest, name, value);
} catch (IllegalArgumentException ie) {
; // Should not happen
} catch (Exception e) {
; // Should not happen
}
}
}
}
}
/**
* 对象拷贝
* 数据对象空值不拷贝到目标对象
*
* @param databean 数据对象
* @param tobean 返回对象
* @throws Exception 异常
*/
public static void copyBeanNotNull2Bean(Object databean, Object tobean)
throws Exception {
PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(
databean);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(databean, name)
&& PropertyUtils.isWriteable(tobean, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(databean,
name);
if (value != null) {
getInstance().setSimpleProperty(tobean, name, value);
}
} catch (IllegalArgumentException ie) {
; // Should not happen
} catch (Exception e) {
; // Should not happen
}
}
}
}
/**
* 把orig和dest相同属性的value复制到dest中
*
* @param dest 结果
* @param orig 来源
* @throws Exception 异常
*/
public static void copyBean2Bean(Object dest, Object orig)
throws Exception {
convert(dest, orig);
}
/**
* 主要功能:bean 转换成 map
* 注意事项:无
*
* @param map map对象
* @param bean bean对象
*/
@SuppressWarnings("all")
public static void copyBean2Map(Map map, Object bean) {
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
for (int i = 0; i < pds.length; i++) {
PropertyDescriptor pd = pds[i];
String propname = pd.getName();
try {
Object propvalue = PropertyUtils.getSimpleProperty(bean,
propname);
map.put(propname, propvalue);
} catch (IllegalAccessException e) {
// e.printStackTrace();
} catch (InvocationTargetException e) {
// e.printStackTrace();
} catch (NoSuchMethodException e) {
// e.printStackTrace();
}
}
}
/**
* 主要功能:bean 转换成 map
* 注意事项:无
*
* @param map map对象
* @param bean bean对象
*/
@SuppressWarnings("all")
public static void copyBean2Map(Map map, Object bean,
List<String> fieldNameList) {
for (int i = 0; i < fieldNameList.size(); i++) {
String oldPropname = fieldNameList.get(i);
String propname = StringUtils.lineToHump(oldPropname);
try {
Object propvalue = getProp(bean, propname);
map.put(oldPropname, propvalue);
} catch (Exception e) {
}
}
}
/**
* 将Map内的key与Bean中属性相同的内容复制到BEAN中
*
* @param bean Object
* @param properties Map
*/
@SuppressWarnings("all")
public static void copyMap2Bean(Object bean, Map properties) {
if ((bean == null) || (properties == null)) {
return;
}
// map key 集合
Iterator names = properties.keySet().iterator();
// 逐个处理key转换
while (names.hasNext()) {
String name = (String) names.next();
if (name == null) {
continue;
}
Object value = properties.get(name);
try {
// name转换成驼峰
name = StringUtils.lineToHump(name);
// 获取属性类型
Class clazz = PropertyUtils.getPropertyType(bean, name);
if (null == clazz) {
continue;
}
String className = clazz.getNam