自定义BeanUtils工具类,代码如下
package com.chinayanghe.fico.utils;
import cn.com.bizunited.cp.common.utils.FastJsonUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Value;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyBeanUtils extends BeanUtils {
public static Map ConvertObjToMap(Object obj){
Map<String,String> reMap = new HashMap<String,String>();
if (obj == null){
return null;
}
Field[] fields = obj.getClass().getDeclaredFields();
try {
for(int i=0;i<fields.length;i++){
try {
Field f = obj.getClass().getDeclaredField(fields[i].getName());
f.setAccessible(true);
Object o = f.get(obj);
if(o!=null){
if(o instanceof ArrayList){
reMap.put(fields[i].getName(), FastJsonUtils.toJson(o));
}else{
reMap.put(fields[i].getName(), o.toString());
}
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
} catch (SecurityException e) {
e.printStackTrace();
}
return reMap;
}
public static void copyBeanNotNull2Bean(Object databean, Object tobean){
PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(databean);
for(int i = 0; i < origDescriptors.length; ++i) {
String name = origDescriptors[i].getName();
if(!"class".equals(name) && PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(databean, name);
if(isNotEmpty(value)) {
copyProperty(tobean, name, value);
}
} catch (IllegalArgumentException var6) {
;
} catch (Exception var7) {
;
}
}
}
}
public static boolean isNotEmpty(Object str) {
boolean flag = true;
if(str != null && !str.equals("")) {
if(str.toString().length() > 0) {
}
} else {
flag = false;
}
return flag;
}
public static boolean isEmpty(Object str) {
return !isNotEmpty(str);
}
public static <T1, T2> T2 copyData(T1 obj, Class<T2> clazz2) {
Class clazz1 = obj.getClass();
final T2 obj2 = getInstance(clazz2);
Field[] fields1 = clazz1.getDeclaredFields();
Field[] fields2 = clazz2.getDeclaredFields();
for (Field f1 : fields1) {
for (Field f2 : fields2) {
copyField(obj, obj2, f1, f2);
}
}
return obj2;
}
public static <T> List<T> copyData1(List<Map<String, String>> list, Class<T> clazz) {
List<T> objList = new ArrayList<>();
Field[] fields = clazz.getDeclaredFields();
for(Map<String, String> m:list){
final T obj = getInstance(clazz);
for (Field field : fields) {
System.out.println(field);
if(field.isAnnotationPresent(Value.class)){
Value va = field.getAnnotation(Value.class);
field.setAccessible(true);
try {
if(field.getType()==BigDecimal.class){
field.set(obj, new BigDecimal(m.get(va.value())));
}if((field.getType()==Integer.class)||field.getType()==int.class){
field.set(obj, new Integer(m.get(va.value())));
}if((field.getType()==Long.class)||field.getType()==long.class){
field.set(obj, new Long(m.get(va.value())));
}if((field.getType()==Short.class)||field.getType()==short.class){
field.set(obj, new Short(m.get(va.value())));
}if((field.getType()==Byte.class)||field.getType()==byte.class){
field.set(obj, new Byte(m.get(va.value())));
}if((field.getType()==Boolean.class)||field.getType()==boolean.class){
field.set(obj, new Boolean(m.get(va.value())));
}else{
field.set(obj, m.get(va.value()));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
field.setAccessible(false);
}
}
objList.add(obj);
}
return objList;
}
public static <T1, T2> T2 copyData(T1 obj, Class<T2> clazz2, String[] fieldNames) {
Class clazz1 = obj.getClass();
final T2 obj2 = getInstance(clazz2);
Field[] fields1 = clazz1.getDeclaredFields();
Field[] fields2 = clazz2.getDeclaredFields();
for (String fieldName : fieldNames) {
for (Field f1 : fields1) {
if (fieldName.equals(f1.getName())) {
for (Field f2 : fields2) {
if (fieldName.equals(f2.getName())) {
copyField(obj, obj2, f1, f2);
}
}
}
}
}
return obj2;
}
private static <T1, T2> void copyField(T1 obj, T2 obj2, Field f1, Field f2) {
try {
if (f1.getName().equals(f2.getName())
& f1.getType().isAssignableFrom(f2.getType())) {
f1.setAccessible(true);
Object val = f1.get(obj);
f2.setAccessible(true);
f2.set(obj2, val);
f2.setAccessible(false);
f1.setAccessible(false);
}
} catch (IllegalAccessException e) {
}
}
public static <T> T getInstance(Class<T> tClass) {
try {
T t = tClass.newInstance();
return t;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}