// ... 其它业务代码
// 去除 java.lang.ClassCastException 类型转换异常
Class<Enterprise> clazz = Enterprise.class;
final Method getter = BeanUtil.getBeanDesc(clazz).getGetter(fieldVal);
final Method setter = BeanUtil.getBeanDesc(clazz).getSetter(fieldVal);
Object conditionValObj = null;
if (ObjectUtil.isNotNull(setter)) {
Enterprise enterprise = clazz.newInstance();
Class<?> returnType = getter.getReturnType();
setter.setAccessible(true);
if (returnType == Double.class) {
double parseDouble = Double.parseDouble(conditionVal);
setter.invoke(enterprise, parseDouble);
} else if (returnType == Integer.class) {
int parseInt = Integer.parseInt(conditionVal);
setter.invoke(enterprise, parseInt);
}
conditionValObj = getter.invoke(enterprise);
}
// ... 其它业务代码
java 处理类型转换异常ClassCastException的另一种实现方式(hutool-all)
最新推荐文章于 2024-08-14 23:12:09 发布