/** * 把list转为List<clazz>类型,并且把字典项转为字典值 * * @param list * @param clazz * @param <T> * @return */ public static <T> List<T> convertList(List list, Class<T> clazz) { List<T> result = new ArrayList<>(); list.forEach(item -> { T t = convertObj(item, clazz); if (t != null) { result.add(convertObj(item, clazz)); } }); return result; } /** * 实体类转换,并且把字典项转为字典值 * * @param obj * @param clazz * @param <T> * @return */ public static <T> T convertObj(Object obj, Class<T> clazz) { if (obj == null) { return null; } T t = JSON.parseObject(JSON.toJSONString(obj, SerializerFeature.WriteNullStringAsEmpty), clazz, Feature.InitStringFieldAsEmpty); //使用反射转换字典 for (int i = 0; i < clazz.getDeclaredFields().length; i++) { Field declaredField = clazz.getDeclaredFields()[i]; declaredField.setAccessible(true); try { if (declaredField.get(t) == null) { continue; } String key = declaredField.get(t).toString(); for (int j = 0; j < declaredField.getDeclaredAnnotations().length; j++) { Annotation declaredAnnotation = declaredField.getDeclaredAnnotations()[j]; String name = declaredAnnotation.annotationType().getName(); if (name.contains("Dictionary")) { InvocationHandler invocationHandler = Proxy.getInvocationHandler(declaredAnnotation); Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues"); memberValues.setAccessible(true); Map map = (Map) memberValues.get(invocationHandler); Object code = map.get("code"); if (code != null) { if (DicConstant.DIC_TABLE_INFO_PL.equals(code.toString())) { if (Constant.ZERO.equals(key)) { declaredField.set(t, "不传"); } else { declaredField.set(t, "上传周期内上传" + key + "次"); } } else { String value = getValueByCodeAndKey(code.toString(), key); declaredField.set(t, value == null ? key : value); } } } } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } return t; }