javamock生成对象
来源:互联网 发布:成捷迅软件破解版 编辑:程序博客网 时间:2024/06/03 23:43
import java.lang.reflect.Field;import java.lang.reflect.ParameterizedType;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Random;public class Mock { private static final Boolean[] bools = new Boolean[] { true, false }; private static final char[] words = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; private static final Random r = new Random(); private static final int MAX_COLLECTION_LENGTH = 50; private static final int MAX_STRING_LENGTH = 30; @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T mock(Class<T> clazz) { if (clazz == Character.class || clazz == Character.TYPE) { return (T) (Character) words[r.nextInt(words.length)]; } else if (clazz == Boolean.class || clazz == Boolean.TYPE) { return (T) (Boolean) bools[r.nextInt(bools.length)]; } else if (clazz == Long.class || clazz == Long.TYPE) { return (T) (Long) r.nextLong(); } else if (clazz == Integer.class || clazz == Integer.TYPE) { return (T) (Integer) r.nextInt(); } else if (clazz == Short.class || clazz == Short.TYPE) { return (T) (Short) new Integer(r.nextInt(127)).shortValue(); } else if (clazz == Float.class || clazz == Float.TYPE) { return (T) (Float) r.nextFloat(); } else if (clazz == Double.class || clazz == Double.TYPE) { return (T) (Double) r.nextDouble(); } else if (clazz == String.class) { return (T) randString(r.nextInt(MAX_STRING_LENGTH)); } try { T instance = clazz.newInstance(); for (Field f : ReflectUtil.getFields(clazz)) { f.setAccessible(true); Length lengthAnno = f.getAnnotation(Length.class); if (f.getType() == Character.TYPE) { f.setChar(instance, words[r.nextInt(words.length)]); } else if (f.getType() == Character.class) { f.set(instance, words[r.nextInt(words.length)]); } else if (f.getType() == Boolean.TYPE) { f.setBoolean(instance, bools[r.nextInt(bools.length)]); } else if (f.getType() == Boolean.class) { f.set(instance, bools[r.nextInt(bools.length)]); } else if (f.getType() == Long.TYPE) { f.setLong(instance, r.nextLong()); } else if (f.getType() == Long.class) { f.set(instance, r.nextLong()); } else if (f.getType() == Integer.TYPE) { f.setInt(instance, r.nextInt()); } else if (f.getType() == Integer.class) { f.set(instance, r.nextInt()); } else if (f.getType() == Short.TYPE) { f.setShort(instance, new Integer(r.nextInt(127)).shortValue()); } else if (f.getType() == Short.class) { f.set(instance, new Integer(r.nextInt(127)).shortValue()); } else if (f.getType() == Float.TYPE) { f.setFloat(instance, r.nextFloat()); } else if (f.getType() == Float.class) { f.set(instance, r.nextFloat()); } else if (f.getType() == Double.TYPE) { f.setDouble(instance, r.nextDouble()); } else if (f.getType() == Double.class) { f.set(instance, r.nextDouble()); } else if (f.getType() == String.class) { f.set(instance, randString(r.nextInt(MAX_STRING_LENGTH))); } else if (f.getType() == List.class) { int size = r.nextInt(MAX_COLLECTION_LENGTH); List<Object> list = new ArrayList<Object>(size); ParameterizedType pt = null; for (int i = 0; i < size; i++) { pt = (ParameterizedType) f.getGenericType(); list.add(mock((Class) pt.getActualTypeArguments()[0])); } f.set(instance, list); } else if (f.getType() == Map.class) { int size = r.nextInt(MAX_COLLECTION_LENGTH); Map<Object, Object> map = new HashMap<Object, Object>(); ParameterizedType pt = null; for (int i = 0; i < size; i++) { pt = (ParameterizedType) f.getGenericType(); map.put(mock((Class) pt.getActualTypeArguments()[0]), mock((Class) pt.getActualTypeArguments()[1])); } f.set(instance, map); } else if (f.getType() == Date.class) { f.set(instance, new Date()); } else { f.set(instance, mock(f.getType())); } } return instance; } catch (Exception e) { throw new IllegalArgumentException(e); } } private static String randString(int count) { if (count == 0) { count = 1; } int length = words.length; char[] text = new char[count]; for (int i = 0; i < count; i++) { text[i] = words[r.nextInt(length)]; } return new String(text); }}
0 0
- javamock生成对象
- 动态生成对象
- javascript对象生成
- JAVA动态生成对象
- 动态生成DIV 对象
- 二。ObpAllocateObject 生成对象
- RGB生成 UIColor对象
- String生成对象问题
- 对象生成xml
- 符号对象的生成
- 对象的生成方式
- 生成设备对象
- 异常对象的生成
- 对象生成方式总结
- jQuery生成json对象
- 临时对象的生成
- git 查看生成对象
- xsd 生成java 对象
- 在windows10下通过Eclipse远程运行wordCount
- Form表单提交数据的几种方式
- LCD1602 4位数据显示
- sencha 安装教程
- JavaSE 学习参考:Java继承
- javamock生成对象
- 反转一个字符串和删除N层级目录的所有文件或者删除数据库所有表保留数据库;清除数据库所有表的数据
- 网络层
- 2017-03-10
- android:ListView缓存机制及BaseAdapter的三重境界(逗比式,普通式,文艺式)
- Python爬虫教程—python爬取糗事百科详解
- Struts旅程(一)Struts简介和原理
- linux下安装php5.6
- [sd card] mmc_blk层为sd card创建块设备流程