import org.apache.commons.lang3.RandomStringUtils; import org.springframework.beans.BeanUtils; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.ThreadLocalRandom; import java.util.function.BiFunction; public class FillBeanUtil { public static <T> void fill(T target, Map<Class, Integer> map) throws Exception { Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable); PropertyDescriptor[] var7 = targetPds; int var8 = targetPds.length; for(int var9 = 0; var9 < var8; ++var9) { PropertyDescriptor targetPd = var7[var9]; Method writeMethod = targetPd.getWriteMethod(); if(writeMethod != null) { Class<?> t = writeMethod.getParameterTypes()[0]; if(List.class.equals(t)) { List list = new ArrayList(); Object tt = randomFill((Class) ((ParameterizedType) writeMethod.getGenericParameterTypes()[0]).getActualTypeArguments()[0], map); if(tt != null) { list.add(tt); } writeMethod.invoke(target, list); } else if(Map.class.equals(t)) { //Bean 表示无泛型对象 //目前只支持到 Map<Bean, Bean> 或 Map<Bean, List<Bean>> Map m = new HashMap(); Object k = randomFill((Class) ((ParameterizedType) writeMethod.getGenericParameterTypes()[0]).getActualTypeArguments()[0], map); Type tt = ((ParameterizedType) writeMethod.getGenericParameterTypes()[0]).getActualTypeArguments()[1]; Object v = null; if(tt instanceof Class) { v = randomFill((Class) tt, map); } else if(tt instanceof ParameterizedType) { List list = new ArrayList(); Object o = randomFill((Class) ((ParameterizedType) tt).getActualTypeArguments()[0], map); if(o != null) { list.add(o); } v = list; } m.put(k, v); writeMethod.invoke(target, m); } else { Object o = randomFill(writeMethod.getParameterTypes()[0], map); writeMethod.invoke(target, o); } } } } public static Class<?> getActualTypeArgument(Class<?> clazz) { Class<?> entitiClass = null; Type genericSuperclass = clazz.getGenericSuperclass(); if (genericSuperclass instanceof ParameterizedType) { Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass) .getActualTypeArguments(); if (actualTypeArguments != null && actualTypeArguments.length > 0) { entitiClass = (Class<?>) actualTypeArguments[0]; } } return entitiClass; } private static Object randomFill(Class clazz, Map<Class, Integer> map) { if (String.class.equals(clazz)) { return RandomStringUtils.randomAlphanumeric(20); } else if(BigDecimal.class.equals(clazz)) { return new BigDecimal(ThreadLocalRandom.current().nextDouble(0, 100)) .setScale(6, RoundingMode.UP); } else if(int.class.equals(clazz) || char.class.equals(clazz) || Integer.class.equals(clazz) || Character.class.equals(clazz) ) { return ThreadLocalRandom.current().nextInt(0, 100); } else if(Long.class.equals(clazz) || long.class.equals(clazz)) { return ThreadLocalRandom.current().nextLong(0, 10000); } else if(boolean.class.equals(clazz) || Boolean.class.equals(clazz)) { return (ThreadLocalRandom.current().nextInt(0) & 1) == 0; } else if(byte.class.equals(clazz) || Byte.class.equals(clazz)) { return (byte) ThreadLocalRandom.current().nextInt(0, 255); } else if(float.class.equals(clazz) || double.class.equals(clazz) || Float.class.equals(clazz) || Double.class.equals(clazz)) { return (float) ThreadLocalRandom.current().nextDouble(0, 100); } else if(LocalDateTime.class.equals(clazz)) { return LocalDateTime.now(); } try { return randomFillBean(clazz, map); } catch (Exception e) { return null; } } public static <T> T randomFillBean(Class<T> clazz) throws Exception { Map<Class, Integer> map = new HashMap<>(); return randomFillBean(clazz, map); } public static <T> T randomFillBean(Class<T> clazz, Map<Class, Integer> map) throws Exception { Integer i = map.get(clazz); if(i == null) { map.put(clazz, 1); } else if(i < 2) { map.put(clazz, i + 1); } else if(i >= 2) { return null; } T t = clazz.getConstructor().newInstance(); fill(t, map); return t; } }
//需要依赖 apache commons 和 spring-beans包