欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

randomObject 根据类随机生成对象

程序员文章站 2022-04-18 13:57:28
...
  • 自动生成的时候用了 hutoolcommons-lang3 ,这个可以用 java 原生的 random 生成
		<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8</version>
        </dependency>
        
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.1.5</version>
        </dependency>
  • 运用 java 反射 实现对象的创建和属性的赋值
public class Util {

    /**
     * 根据 类 创建一个随机生成对象
     */
    public static Object randomObject(Class<?> clazz) {
        Class sourceClass = clazz;
        Object obj = null;
        try {
            obj = clazz.newInstance();
            //获取对象所有字段 包括父类
            ArrayList<Field> fields = new ArrayList<>();
            while (sourceClass != null) {
                fields.addAll(Arrays.asList(sourceClass.getDeclaredFields()));
                sourceClass = sourceClass.getSuperclass();
            }
            for (Field field : fields) {
                //todo:通过自定义注解去筛掉哪些需要自动赋值,哪些不需要
                Class<?> type = parseClass(field.getType());
                field.setAccessible(true);
                try {
                    if (String.class.isAssignableFrom(type)) {
                        field.set(obj, RandomUtil.randomString(5));
                    } else if (Date.class.isAssignableFrom(type)) {
                        field.set(obj, randomDate());
                    } else if (Boolean.class.isAssignableFrom(type)) {
                        field.set(obj, RandomUtil.randomBoolean());
                    } else if (Character.class.isAssignableFrom(type)) {
                        field.set(obj, RandomUtil.randomChar());
                    } else if (Number.class.isAssignableFrom(type)) {
                        if (Byte.class.isAssignableFrom(type)) {
                            field.set(obj, RandomUtil.randomBytes(1)[0]);
                        } else if (Short.class.isAssignableFrom(type)) {
                            short ashort = (short) (RandomUtil.randomInt(0, 99999) & 0x7FFF);
                            field.set(obj, ashort);
                        } else if (Integer.class.isAssignableFrom(type)) {
                            field.set(obj, RandomUtil.randomInt(0, 99999));
                        } else if (Long.class.isAssignableFrom(type)) {
                            field.set(obj, RandomUtil.randomLong(0, 9999));
                        } else if (Float.class.isAssignableFrom(type)) {
                            field.set(obj, RandomUtils.nextFloat(100, 9999));
                        } else if (Double.class.isAssignableFrom(type)) {
                            field.set(obj, RandomUtil.randomDouble(10, 99999, 2, RoundingMode.DOWN));
                        }
                    } else {
                        //以上类型都不是,就递归生成
                        field.set(obj, randomObject(type));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }

    private static Date randomDate() {
        DateTime baseDate = DateUtil.parse("1980-01-01");
        DateTime dateTime = RandomUtil.randomDate(baseDate, DateField.DAY_OF_YEAR, 0, 4000);
        return dateTime;
    }

    /**
     * 将基础类和包装类相对应
     * java.lang.Boolean#TYPE == boolean
     * java.lang.Character#TYPE == character
     * java.lang.Byte#TYPE == byte
     * java.lang.Short#TYPE == short
     * java.lang.Integer#TYPE == integer
     * java.lang.Long#TYPE == long
     * java.lang.Float#TYPE == float
     * java.lang.Double#TYPE == double
     * java.lang.Void#TYPE == void
     */
    private static Class<?> parseClass(Class<?> type) {
        Class newType = type;
        final Map<Class, Class> classes = new HashMap<>(9);
        classes.put(boolean.class, Boolean.class);
        classes.put(char.class, Character.class);
        classes.put(byte.class, Byte.class);
        classes.put(short.class, Short.class);
        classes.put(int.class, Integer.class);
        classes.put(long.class, Long.class);
        classes.put(float.class, Float.class);
        classes.put(double.class, Double.class);
        classes.put(void.class, Void.class);
        return classes.getOrDefault(type, type);
    }
}

参考:https://blog.csdn.net/qq_36666651/article/details/81215221

相关标签: Java基础