备注
java项目中写测试用例时,经常要初始化bean,如果bean的属性比较多,一个个set起来比较麻烦,可以借助反射的方式遍历属性,并给每个属性赋值;
代码
jdk8及以上版本
pom依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
java代码
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* <p>description:</p>
* <p>copyright: copyright(C)2016-2099</p>
* <p>Summary: </p>
* <p>instructions: </p>
* Date 2021/11/1</p>
* Author mac
*
* @version 1.0
*/
public class BeanInitUtils {
public static <T> T initBean(Class<T> clazz, List<String> ignoreFields) {
if (clazz == null) {
return null;
}
if (CollectionUtils.isEmpty(ignoreFields)) {
ignoreFields = new ArrayList<>();
}
T obj = null;
try {
obj = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
if (ignoreFields.contains(fieldName)) {
continue;
}
field.setAccessible(true);
// 优先使用字段的默认值
if (field.get(obj) != null) {
if (!ignoreFields.contains(fieldName)) {
ignoreFields.add(fieldName);
continue;
}
}
if (field.getType().isInstance(int.class) || field.getType().isInstance((Integer.class))) {
field.set(obj, RandomUtils.nextInt(0, 100));
} else if (field.getType() == String.class) {
field.set(obj, "test" + RandomUtils.nextInt(0, 100));
} else if (field.getType() == double.class || field.getType() == Double.class) {
field.set(obj, RandomUtils.nextDouble(0, 100));
} else if (field.getType() == float.class || field.getType() == Float.class) {
field.set(obj, RandomUtils.nextFloat(0, 100));
} else if (field.getType() == byte.class || field.getType() == Byte.class) {
field.set(obj, RandomUtils.nextBytes(128));
} else if (field.getType() == Date.class) {
field.set(obj, new Date());
} else if (field.getType() == boolean.class || field.getType() == Boolean.class) {
field.set(obj, RandomUtils.nextBoolean());
} else if (field.getType() == long.class || field.getType() == Long.class) {
field.set(obj, RandomUtils.nextLong(0, 100));
} else if (field.getType() == char.class || field.getType() == Character.class) {
field.set(obj, RandomUtils.nextInt(Character.MIN_VALUE, Character.MAX_VALUE));
} else {
if (!ignoreFields.contains(fieldName)) {
ignoreFields.add(fieldName);
}
}
}
ignoreFields.forEach(item -> System.out.println(item + ":未初始化"));
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
static class Person {
private int age = 0;
private String name;
private Date birthday;
private String email;
private float weight;
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
", birthday=" + birthday +
", email='" + email + '\'' +
", weight=" + weight +
'}';
}
}
public static void main(String[] args) {
Person person = BeanInitUtils.initBean(Person.class, null);
System.out.println(person);
}
}
不足之处
只能初始化常用的基本类型,不能初始化其他的引用类型,需要进一步改进;