/**
*
*/
package net.cloudsun.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import net.cloudsun.base.entity.User;
/**
* 熊浪
*/
public class ObjectNullToStringUtils {
public static Object checkFieldValueNull(Object bean) {
boolean result = true;
if (bean == null) {
return null;
}
Class<?> cls = bean.getClass();
Method[] methods = cls.getDeclaredMethods();
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
try {
//第一种方法
field.setAccessible(true);
if (field.get(bean) == null) {
field.set(bean, "");
}
//第二种方法
// String fieldGetName = parGetName(field.getName());
// if (!checkGetMet(methods, fieldGetName)) {
// continue;
// }
// Method fieldGetMet = cls
// .getMethod(fieldGetName, new Class[] {});
// Object fieldVal = fieldGetMet.invoke(bean, new Object[] {});
// if (fieldVal != null) {
// if ("".equals(fieldVal)) {
// result = true;
// } else {
// result = false;
// }
// } else {
// field.setAccessible(true);//把private方法设置为可见,要不然会报错的
// field.set(bean, "1232");// 把null值赋值为空字符串
// }
} catch (Exception e) {
continue;
}
}
// return result 是判断是否有null或是""空值
return bean;
}
/**
* 拼接某属性的 get方法
*
* @param fieldName
* @return String
*/
public static String parGetName(String fieldName) {
if (null == fieldName || "".equals(fieldName)) {
return null;
}
int startIndex = 0;
if (fieldName.charAt(0) == '_')
startIndex = 1;
return "get"
+ fieldName.substring(startIndex, startIndex + 1).toUpperCase()
+ fieldName.substring(startIndex + 1);
}
/**
* 拼接某属性的set方法
*
* @param fieldName
* @return String
*/
public static String parSetName(String fieldName) {
if (null == fieldName || "".equals(fieldName)) {
return null;
}
int startIndex = 0;
if (fieldName.charAt(0) == '_')
startIndex = 1;
return "set"
+ fieldName.substring(startIndex, startIndex + 1).toUpperCase()
+ fieldName.substring(startIndex + 1);
}
/**
* 判断是否存在某属性的 get方法
*
* @param methods
* @param fieldGetMet
* @return boolean
*/
public static boolean checkGetMet(Method[] methods, String fieldGetMet) {
for (Method met : methods) {
if (fieldGetMet.equals(met.getName())) {
return true;
}
}
return false;
}
public static void main(String[] args) {
User user = new User();//测试类
user.setId(1);
System.out.println(ObjectNullToStringUtils.checkFieldValueNull(user));
}
}
*
*/
package net.cloudsun.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import net.cloudsun.base.entity.User;
/**
* 熊浪
*/
public class ObjectNullToStringUtils {
public static Object checkFieldValueNull(Object bean) {
boolean result = true;
if (bean == null) {
return null;
}
Class<?> cls = bean.getClass();
Method[] methods = cls.getDeclaredMethods();
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
try {
//第一种方法
field.setAccessible(true);
if (field.get(bean) == null) {
field.set(bean, "");
}
//第二种方法
// String fieldGetName = parGetName(field.getName());
// if (!checkGetMet(methods, fieldGetName)) {
// continue;
// }
// Method fieldGetMet = cls
// .getMethod(fieldGetName, new Class[] {});
// Object fieldVal = fieldGetMet.invoke(bean, new Object[] {});
// if (fieldVal != null) {
// if ("".equals(fieldVal)) {
// result = true;
// } else {
// result = false;
// }
// } else {
// field.setAccessible(true);//把private方法设置为可见,要不然会报错的
// field.set(bean, "1232");// 把null值赋值为空字符串
// }
} catch (Exception e) {
continue;
}
}
// return result 是判断是否有null或是""空值
return bean;
}
/**
* 拼接某属性的 get方法
*
* @param fieldName
* @return String
*/
public static String parGetName(String fieldName) {
if (null == fieldName || "".equals(fieldName)) {
return null;
}
int startIndex = 0;
if (fieldName.charAt(0) == '_')
startIndex = 1;
return "get"
+ fieldName.substring(startIndex, startIndex + 1).toUpperCase()
+ fieldName.substring(startIndex + 1);
}
/**
* 拼接某属性的set方法
*
* @param fieldName
* @return String
*/
public static String parSetName(String fieldName) {
if (null == fieldName || "".equals(fieldName)) {
return null;
}
int startIndex = 0;
if (fieldName.charAt(0) == '_')
startIndex = 1;
return "set"
+ fieldName.substring(startIndex, startIndex + 1).toUpperCase()
+ fieldName.substring(startIndex + 1);
}
/**
* 判断是否存在某属性的 get方法
*
* @param methods
* @param fieldGetMet
* @return boolean
*/
public static boolean checkGetMet(Method[] methods, String fieldGetMet) {
for (Method met : methods) {
if (fieldGetMet.equals(met.getName())) {
return true;
}
}
return false;
}
public static void main(String[] args) {
User user = new User();//测试类
user.setId(1);
System.out.println(ObjectNullToStringUtils.checkFieldValueNull(user));
}
}