Reflection should not be used to increase accessibility of classes, methods, or fields
publicT 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(), "属性方法执行异常"); } }


![反射避开field.setAccessible(true); field.set(t, lineArray[i]); 赋值 反射避开field.setAccessible(true); field.set(t, lineArray[i]); 赋值](http://www.mshxw.com/aiimages/31/785411.png)
