把list转为List<clazz>类型,并把字典项转为字典值

    /*** 把list转为List类型,并且把字典项转为字典值** @param list* @param clazz* @param * @return*/public static  List convertList(List list, Class clazz) {List 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 * @return*/public static  T convertObj(Object obj, Class 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;}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部