2018-06-03 22:26:41 +08:00

134 lines
4.0 KiB
Java

package cn.coyoteam.aweresf.util;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* JavaBean操作工具
* @author yangsong
*
*/
public class BeanUtils {
private static Logger logger = LoggerFactory.getLogger(BeanUtils.class);
/**
* 复制javaBean属性
* @param source 数据源对象
* @param target 复制对的目标对象
*/
public static void copyProperties(Object source, Object target){
try {
PropertyUtils.copyProperties(target,source);
} catch (IllegalAccessException e) {
logger.warn("copyProperties error ", e);
} catch (InvocationTargetException e) {
logger.warn("copyProperties error ", e);
} catch (NoSuchMethodException e) {
logger.warn("copyProperties error ", e);
}
}
/**
* JavaBean转为Map
* @param bean
* @return
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static final Map<String, Object> bean2Map(Object bean)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
Map<String, Object> returnMap = new HashMap<String, Object>();
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
/**
* MAPl转为JavaBean
* @param map
* @param obj
*/
public static void map2Bean(Map<String, Object> map, Object obj) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
setter.invoke(obj, value);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("",e);
}
}
/**
* MAPl转为JavaBean
* @param map
* @param clazz
*/
public static void map2Bean(Map<String, Object> map, Class<Object> clazz) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
Object obj = clazz.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
setter.invoke(obj, value);
}
}
} catch (Exception e) {
logger.error("",e);
}
return;
}
}