Reflection should not be used to increase accessibility of classes, methods, or fields
【true 反射避开field.setAccessible; field.set(t, lineArray[i]); 赋值】public T toJavaObject(String line) throws Exception {// 数据组文件没有标题行 单独处理if (StringUtils.isBlank(line)) {return null;} else {String[] lineArray = line.split(SymbolConstants.COMMA, -1);Class clazz = getFileType().findClass();T t = clazz.newInstance();Field[] fields = clazz.getDeclaredFields();int i = 0;for (Field field : fields) {// field.setAccessible(true);// field.set(t, lineArray[i]);setBodyValueByField(t, field, lineArray[i]);i++;}return t;}}private void setBodyValueByField(Object t, Field field, String stringValue) {try {Class> clazz = t.getClass();PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);Method method = pd.getWriteMethod();String type = field.getGenericType().toString();if (StringUtil.isBlank(stringValue)) {return;}switch (type) {case "class java.lang.String":method.invoke(t, stringValue);break;case "class java.lang.Integer":method.invoke(t, Double.valueOf(stringValue).intValue());break;case "class java.lang.Long":method.invoke(t, Double.valueOf(stringValue).longValue());break;case "class java.lang.Double":method.invoke(t, Double.valueOf(stringValue));break;case "class java.lang.Float":method.invoke(t, Double.valueOf(stringValue).floatValue());break;case "class java.lang.Character":method.invoke(t, stringValue.toCharArray()[0]);break;case "class java.math.BigDecimal":method.invoke(t, new BigDecimal(stringValue));break;case "class java.util.Date":method.invoke(t, DateUtil.getDate(stringValue, DateUtil.DATE_FORMAT_2));break;default:method.invoke(t, (Object) null);break;}} catch (Exception e) {log.error("{}属性方法执行异常", field.getName(), e);throw new HsjryBizException(EnumOpChannelError.BIZ_FAILED_ERROR.getCode(), "属性方法执行异常");}}
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
