https://blog.csdn.net/hellojoy/article/details/106943104
package com.toyor.hpqsharecloud.common;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Null2EmptyString {
/*
取Bean的属性和值对应关系的MAP
* @param __bean
* @return Map
*/
public static Map
Class<?> cls = bean.getClass();
Map
_// 取出bean里的所有方法
_Method[] methods = cls.getDeclaredMethods();
Field[] fields = cls.getDeclaredFields();
**for **(Field field : fields) {<br /> **try **{<br /> String fieldType = field.getType().getSimpleName();<br /> String fieldGetName = _parGetName_(field.getName());<br /> **if **(!_checkGetMet_(methods, fieldGetName)) {<br /> **continue**;<br /> }<br /> Method fieldGetMet = cls.getMethod(fieldGetName, **new **Class[] {});<br /> Object fieldVal = fieldGetMet.invoke(bean, **new **Object[] {});<br /> String result = **null**;<br /> _/*if ("Date".equals(fieldType)) {<br /> result = fmtDate((Date) fieldVal);<br /> } else {<br /> if (null != fieldVal) {<br /> result = String.valueOf(fieldVal);<br /> }<br /> }*/<br /> _valueMap.put(field.getName(), result);<br /> } **catch **(Exception e) {<br /> **continue**;<br /> }<br /> }<br /> **return **valueMap;
}
public static void setFieldValue(Object bean) {
Class<?> cls = bean.getClass();
_// 取出bean里的所有方法
_Method[] methods = cls.getDeclaredMethods();
Field[] fields = cls.getDeclaredFields();
**for **(Field field : fields) {<br /> **try **{<br /> String fieldSetName = _parSetName_(field.getName());<br /> **if **(!_checkSetMet_(methods, fieldSetName)) {<br /> **continue**;<br /> }<br /> Method fieldSetMet = cls.getMethod(fieldSetName, field.getType());<br /> _/*String value = valMap.get(field.getName());<br /> if (null != value && !"".equals(value)) {<br /> String fieldType = field.getType().getSimpleName();<br /> if ("String".equals(fieldType)) {<br /> fieldSetMet.invoke(bean, value);<br /> } else if ("Date".equals(fieldType)) {<br /> Date temp = parseDate(value);<br /> fieldSetMet.invoke(bean, temp);<br /> } else if ("Integer".equals(fieldType) || "int".equals(fieldType)) {<br /> Integer intval = Integer.parseInt(value);<br /> fieldSetMet.invoke(bean, intval);<br /> } else if ("Long".equalsIgnoreCase(fieldType)) {<br /> Long temp = Long.parseLong(value);<br /> fieldSetMet.invoke(bean, temp);<br /> } else if ("Double".equalsIgnoreCase(fieldType)) {<br /> Double temp = Double.parseDouble(value);<br /> fieldSetMet.invoke(bean, temp);<br /> } else if ("Boolean".equalsIgnoreCase(fieldType)) {<br /> Boolean temp = Boolean.parseBoolean(value);<br /> fieldSetMet.invoke(bean, temp);<br /> } else {<br /> System.out.println("not supper type" + fieldType);<br /> }<br /> }*/<br /> _} **catch **(Exception e) {<br /> **continue**;<br /> }<br /> }
}
/*
判断是否存在某属性的 set方法
* @param __methods
* @param __fieldSetMet
* @return boolean
*/
private static boolean checkSetMet(Method[] methods, String fieldSetMet) {
for (Method met : methods) {
if (fieldSetMet.equals(met.getName())) {
return true;
}
}
return false;
}
/*
判断是否存在某属性的 get方法
* @param __methods
* @param __fieldGetMet
* @return boolean
*/
private static boolean checkGetMet(Method[] methods, String fieldGetMet) {
for (Method met : methods) {
if (fieldGetMet.equals(met.getName())) {
return true;
}
}
return false;
}
/*
拼接某属性的 get方法
* @param __fieldName
* @return String
*/
private static String parGetName(String fieldName) {
if (null == fieldName || “”.equals(fieldName)) {
return null;
}
return “get” + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
}
/*
拼接在某属性的 set方法
* @param __fieldName
* @return String
*/
private static String parSetName(String fieldName) {
if (null == fieldName || “”.equals(fieldName)) {
return null;
}
return “set” + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
}
public static void List(List list){
list.forEach(o -> {
Null2EmptyString.Perform(o);
});
}
public static void Perform(Object bean){
try {
Class<? extends Object> type = bean.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
Field[] fields = type.getDeclaredFields();<br /> Map<String,String> fieldmap = **new **HashMap<>();<br /> **for**(**int **i = 0; i < fields.**length **;i++){<br /> fieldmap.put(fields[i].getName().toLowerCase(),fields[i].getType().getSimpleName().toLowerCase());<br /> }
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();<br /> **for **(**int **i = 0; i < propertyDescriptors.**length**; i++) {<br /> PropertyDescriptor descriptor = propertyDescriptors[i];<br /> String propertyName = descriptor.getName();<br /> **if **(!**"class"**.equals(propertyName)) {<br /> Method readMethod = descriptor.getReadMethod();<br /> Method writeMethod = descriptor.getWriteMethod();<br /> Object result = readMethod.invoke(bean, **new **Object[0]);<br /> **if **(result != **null**) {<br /> }**else**{<br /> **if**(writeMethod != **null**) {<br /> **try **{<br /> **if**(fieldmap.containsKey(propertyName.toLowerCase()) && fieldmap.get(propertyName.toLowerCase()).equals(**"string"**)) {<br /> _//Object[] oParam = new Object[]{};<br /> //oParam = new String[]{("")};<br /> //注意这里的参数。<br /> //writeMethod.invoke(bean, oParam);<br /> _writeMethod.invoke(bean, **""**);<br /> }<br /> } **catch **(IllegalArgumentException iea) {<br /> System.**_out_**.println(**"参数错误:" **+ propertyName);<br /> iea.printStackTrace();<br /> }<br /> }<br /> }
}<br /> }<br /> }<br /> **catch**(Exception e){<br /> e.printStackTrace();<br /> }<br /> }
}