java.lang.IllegalArgumentException: java.lang.ClassCastException

使用反射回调时出现了异常,这里做一下记录

Method method = instance.getClass().getMethod(setter, filedClazz);
method.invoke(instance, value);
java.lang.IllegalArgumentException: java.lang.ClassCastException@399fa127
    at sun.reflect.GeneratedMethodAccessor289.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

可以看到这个异常是类型不匹配导致的

1、核对一下参数和类型能否对应上

2、int和Integer是两个类型,需要将int的数字包装成Integer处理

原文地址:https://www.cnblogs.com/wwzyy/p/14330146.html