junit
junit
4.12
test
org.projectlombok
lombok
1.16.20
org.slf4j
slf4j-simple
1.7.25
com.alibaba
fastjson
1.2.35
commons-beanutils
commons-beanutils
1.9.3
package com.ydfind.object.model;
import lombok.Data;
@Data
public class Student {
private Integer id;
private Integer age;
private String name;
private String address;
private String phone;
private String sex;
public Student(){
}
public Student(Integer id, Integer age, String name, String address, String phone, String sex){
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.phone = phone;
this.sex = sex;
}
}
package com.ydfind.object.model;
public class Student1 {
private Integer id;
private Integer age;
private String name;
private String address;
private String phone;
private String sex;
public Student1(){
}
public Student1(Integer id, Integer age, String name, String address, String phone, String sex){
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.phone = phone;
this.sex = sex;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAddress(String address){
this.address = address;
}
public void getPhone(String phone){
this.phone = phone;
}
}
package com.ydfind.object;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
@Slf4j
public class ObjectExample {
/**
* 对象中字符串属性,若为空则设置为null
* @param obj
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
public static void changeStringToNull(Object obj) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
for (Class> clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
Field fields[] = clazz.getDeclaredFields();
// 遍历属性值,取得所有属性为 null 值的
for (Field field : fields) {
Type t = field.getGenericType();
if (t.getTypeName().equals(String.class.getTypeName())) {
Object property = PropertyUtils.getProperty(obj, field.getName());
if (property != null && "".equals(String.valueOf(property).trim())) {
log.info("将设置{}为null", field.getName());
PropertyUtils.setProperty(obj, field.getName(), null);
}
}
}
}
}
}
package com.ydfind.object;
import com.ydfind.object.model.Student;
import com.ydfind.object.model.Student1;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
public class ObjectExampleTest {
@Test
public void testChangeStringToNull() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
// public Student(Integer id, Integer age, String name, String address, String phone, String sex)
Student student = new Student(1, 18, "张三", "", " ", "男");
System.out.println("student define: " + student);
ObjectExample.changeStringToNull(student);
System.out.println("student process: " + student);
Student1 student1 = new Student1(1, 18, " ", "", " ", "男");
System.out.println("student1 define: " + student1);
ObjectExample.changeStringToNull(student1);
System.out.println("student1 process: " + student1);
}
}
可以看到Student对象可以正常运行,但address属性因为没有get方法,导致PropertyUtils.getProperty报错;