更新 - 使问题更清楚。
通过反射调用方法时出现 ClassCastException 的可能原因是什么?
在尝试通过反射调用方法时,我在应用程序中得到了以下堆栈跟踪信息。
java.lang.IllegalArgumentException: java.lang.ClassCastException@21fea1fv
at sun.reflect.GeneratedMethodAccessor332.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.....
(remaining is my method stack trace)
我尝试了一个示例类并将不同类型的各种参数传递给它,但我总是得到这个异常。
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
更新 - 这是我为尝试重新创建异常而编写的示例代码
创建代理类的接口(interface)
package my.tests;
public interface ReflectionsInterface {
public abstract void doSomething();
}
这是测试类
package my.tests;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Reflections implements ReflectionsInterface {
public static void main(String[] args) {
Reflections reflections = new Reflections();
ReflectionsInterface reflectionsProxy = reflections.createProxy(ReflectionsInterface.class);
invokeMethod(reflectionsProxy, "doSomething", null);
}
public <T> T createProxy(Class<T> entityInterface) {
EntityInvocationHandler eih = new EntityInvocationHandler(this);
T cast = entityInterface.cast(Proxy.newProxyInstance(
entityInterface.getClassLoader(), new Class[]{entityInterface}, eih));
return cast;
}
public static void invokeMethod(Object obj, String methodName, Object... args) {
Method[] methods = obj.getClass().getMethods();
try {
for (Method method : methods) {
if (method.getName().equals(methodName)) {
method.invoke(obj, args);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void doSomething() {
System.out.println("woo");
}
private final static class EntityInvocationHandler implements InvocationHandler,
ReflectionsInterface {
private Reflections reflectionObj;
public EntityInvocationHandler(Reflections reflectionObj) {
super();
this.reflectionObj = reflectionObj;
}
@Override
public void doSomething() {
reflectionObj.doSomething();
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object invoke = method.invoke(this, args);
return invoke;
}
}
}
我无法理解什么时候会出现参数类型不匹配 并且会导致ClassCastException。 我无法重新创建异常,想知道它出现的原因。任何重新创建它的工作代码,或在这种情况下抛出此异常的源代码引用都是好的
我已经浏览了 Method.class javadocs 和源代码,我无法弄清楚为什么会出现这个错误。
最佳答案
我通过修改您的示例代码重新创建了 ClassCastException
:使用正确的参数调用 invokeMethod
10000 次,然后使用错误的参数调用它。
Reflections
类中的main
方法
public static void main(String[] args) {
Reflections reflections = new Reflections();
ReflectionsInterface reflectionsProxy = reflections
.createProxy(ReflectionsInterface.class);
for (int i = 0; i < 10000; i++)
invokeMethod(reflectionsProxy, ReflectionsInterface.class,
"doSomething");
invokeMethod(new Object(), ReflectionsInterface.class, "doSomething");
}
Reflections
类中的invokeMethod
方法
public static void invokeMethod(Object obj, Class<?> clazz,
String methodName, Object... args) {
Method[] methods = clazz.getMethods();
try {
for (Method method : methods) {
if (method.getName().equals(methodName)) {
method.invoke(obj, args);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
堆栈跟踪:
java.lang.IllegalArgumentException: java.lang.ClassCastException@603a3e21
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.twbbs.pccprogram.Reflections.invokeMethod(Reflections.java:33)
at org.twbbs.pccprogram.Reflections.main(Reflections.java:16)
我对 ClassCastException
的解释:
当你第一次调用invokeMethod
时,JVM使用较慢的路由,这更容易让程序员调试(所以它更慢!),所以它会显示一个更友好的argument当你传递一个错误的参数时,类型不匹配
消息。
当你多次调用invokeMethod
时(在我的测试中16次就足够了),JVM会在运行时生成一个GeneratedMethodAccessor***
,速度更快,更少的错误检查。所以当你传递一个错误的参数时,它会显示这样一个丑陋的 java.lang.ClassCastException@603a3e21
消息。
关于java - 反射 IllegalArgumentException 原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16042591/