初始化Bean工具类

备注

java项目中写测试用例时,经常要初始化bean,如果bean的属性比较多,一个个set起来比较麻烦,可以借助反射的方式遍历属性,并给每个属性赋值;

代码

jdk8及以上版本

pom依赖


    org.apache.commons
    commons-lang3
    3.5


    org.apache.commons
    commons-collections4
    4.4

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;

/**
 * 

description:

*

copyright: copyright(C)2016-2099

*

Summary:

*

instructions:

* Date 2021/11/1

* Author mac * * @version 1.0 */ public class BeanInitUtils { public static T initBean(Class clazz, List 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); } }

不足之处

只能初始化常用的基本类型,不能初始化其他的引用类型,需要进一步改进;

你可能感兴趣的:(初始化Bean工具类)