import org.apache.commons.beanutils.PropertyUtils;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 对象拷贝工具类
*/
public class BeanUtil {
/**
* 获取Obj对象的fieldName属性的值
*/
private static Object getFieldValue(Object obj, String fieldName) {
Object fieldValue = null;
if(null == obj) {
return null;
}
Method[] methods = obj.getClass().getDeclaredMethods();
for (Method method : methods) {
String methodName = method.getName();
if(methodName.startsWith("get") && methodName.substring(3).toUpperCase().equals(fieldName.toUpperCase())) {
try {
fieldValue = method.invoke(obj, new Object[] {});
} catch (Exception e) {
System.out.println("取值出错,方法名 " + methodName);
continue;
}
}
}
return fieldValue;
}
public static Map<String, Object> objectToMap(Object obj) throws Exception{
if(obj == null){
return null;
}
Map<String, Object> map = new HashMap<>();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
}
return map;
}
/**
* 拷贝对象属性到目标对象
* <p>
* <b>示例</b> BeanSource bs = new BeanSource(); bs.set(...); ... BeanTarget
* bt = BeanUtil.copyProperties(bs,BeanTarget.class);
*
* </p>
*
* @param sourceObj
* 源对象
* @param targetClazz
* 目标类
* @return 目标对象
*/
@SuppressWarnings("unchecked")
public static <T> T copyProperties(Object sourceObj, Class<?> targetClazz) {
T targetObj = null;
try {
if(sourceObj==null){
return targetObj;
}
targetObj = (T) targetClazz.newInstance();
PropertyUtils.copyProperties(targetObj, sourceObj);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return targetObj;
}
/**
* 拷贝对象属性到目标对象,应用于已存在了目标对象,使用源对象丰富目标对象的属性值
*/
public static boolean copyProperties(Object sourceObj, Object targetObj) {
boolean flag = false;
try {
PropertyUtils.copyProperties(targetObj, sourceObj);
flag = true;
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getMessage());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e.getMessage());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return flag;
}
/**
* 拷贝源列表对象到目标列表
* @param sourceList 源列表
* @param targetClazz 目标列表元类型
* @return 目标列表
*/
public static <T> List<T> copyBeans(List<?> sourceList, Class<?> targetClazz)
throws InstantiationException, IllegalAccessException {
if (sourceList == null) {
throw new IllegalArgumentException("No origin bean specified");
}
List<T> targetList = new ArrayList<T>();
for (Object sourceObj : sourceList) {
T targetObj = copyProperties(sourceObj, targetClazz);
targetList.add(targetObj);
}
return targetList;
}
/**
* 将一个 JavaBean 对象转化为一个 Map
* @param bean 要转化的JavaBean 对象
* @return 转化出来的 Map 对象
* @throws IntrospectionException 如果分析类属性失败
* @throws IllegalAccessException 如果实例化 JavaBean 失败
* @throws InvocationTargetException 如果调用属性的 setter 方法失败
*/
public static Map<String, Object> convertBean(Object bean) throws IntrospectionException, IllegalAccessException,InvocationTargetException {
Class<? extends Object> type = bean.getClass();
Map<String, Object> returnMap = new HashMap<String, Object>();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
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;
}
/**
* 判断对象是否为空或属性全为空
*/
public static boolean isBeanPropertiesNull(Object bean)throws IntrospectionException, IllegalAccessException,InvocationTargetException {
if (bean == null) {
return true;
}
Class<? extends Object> type = bean.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
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) {
return false;
}
}
}
return true;
}
}