很简单直接的生成的方式,因为不存在修修改改的情况,如果是按手工一个一个参数去set有个POJO十几个变量,简直要命,通过这个就可以简单快速的生成一个可用的POJO
protected <T> T generator(T t) { Class clazz = t.getClass(); Field[] fields = clazz.getDeclaredFields(); Field[] fields1 = clazz.getSuperclass().getDeclaredFields(); Field[] fields2 = new Field[fields.length + fields1.length]; System.arraycopy(fields, 0, fields2, 0, fields.length); System.arraycopy(fields1, 0, fields2, fields.length, fields1.length); for (Field field : fields2) { try { if (field.getModifiers() == 26 || field.getModifiers() == 25) { continue; } else if (field.getType().getSimpleName().equals(String.class.getSimpleName())) { field.setAccessible(true); field.set(t, "insertTest"); } else if (field.getType().getSimpleName().equals(Long.class.getSimpleName()) || field.getType().getSimpleName().equals(long.class.getSimpleName())) { field.setAccessible(true); field.set(t, 10000L); } else if (field.getType().getSimpleName().equals(Integer.class.getSimpleName()) || field.getType().getSimpleName().equals(int.class.getSimpleName())) { field.setAccessible(true); field.set(t, 1); } else if (field.getType().getSimpleName().equals(Boolean.class.getSimpleName()) || field.getType().getSimpleName().equals(boolean.class.getSimpleName())) { field.setAccessible(true); field.set(t, true); } else if (field.getType().getSimpleName().equals(Date.class.getSimpleName())) { field.setAccessible(true); field.set(t, new Date()); } else if (field.getType().getName().startsWith("[")) { field.setAccessible(true); Object abc = Array.newInstance(field.getType().getComponentType(), 1); Object[] abcs = (Object[])abc; abcs[0] = generator(field.getType().getComponentType().newInstance()); field.set(t, abcs); } else { field.setAccessible(true); field.set(t, generator(field.getType().newInstance())); } } catch (IllegalAccessException | InstantiationException e) { e.printStackTrace(); //do nothing } } return t; } public static void toString(Map<String, Object> map) { for (Map.Entry<String, Object> entry : map.entrySet()) { System.out.println("key=" + entry.getKey() + "|" + " value=" + entry.getValue()); } System.out.println( "=================================================================a map============================================="); } public static void toString(Object o) { Class clazz = o.getClass(); Field[] fields = clazz.getDeclaredFields(); try { StringBuilder sb = new StringBuilder(); for (Field field : fields) { if (field.getType().getName().startsWith("[L")) { field.setAccessible(true); Object[] a = (Object[])field.get(o); for (Object t : a) { toString(t); } } else if (field.getModifiers() != (Modifier.PRIVATE + Modifier.STATIC + Modifier.FINAL)) { field.setAccessible(true); sb.append(field.getName()); sb.append("="); sb.append(field.get(o) != null ? field.get(o).toString() : "null"); sb.append("\r\n"); } } System.out.println(sb.toString()); System.out.println( "=================================================================a Object============================================="); } catch (Exception e) { e.printStackTrace(); } } public void toString(Object... objects) { for (Object o : objects) { toString(o); } }