反射IllegalArgumentException导致

Adi*_*tya 10 java reflection classcastexception illegalargumentexception

更新 - 使问题更清楚.

在通过反射调用方法时获取ClassCastException的可能原因是什么?

在尝试通过反射调用方法时,我将以下stacktrace作为我的应用程序的一部分.

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)
Run Code Online (Sandbox Code Playgroud)

我尝试了一个示例类,并将不同类型的各种参数传递给它,但我总是得到一个这个例外.

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)
Run Code Online (Sandbox Code Playgroud)

更新 - 这是我为尝试重新创建异常而编写的示例代码

用于创建代理类的接口

package my.tests;

public interface ReflectionsInterface { 
    public abstract void doSomething();
}
Run Code Online (Sandbox Code Playgroud)

这是测试类

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;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我无法理解何时会出现参数类型不匹配并导致ClassCastException. 我无法重新创建异常,并想知道它为什么会出现.任何重新创建它的工作代码,或在这种情况下抛出此异常的源代码引用都会很好

我已经浏览了Method.class javadocs和源代码,我无法弄清楚为什么会出现这个错误.

joh*_*902 16

ClassCastException通过修改你的示例代码重新创建了:invokeMethod使用正确的参数调用10000次,然后使用错误的错误调用它.

班上的main方法Reflections

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");
}
Run Code Online (Sandbox Code Playgroud)

班上的invokeMethod方法Reflections

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

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)
Run Code Online (Sandbox Code Playgroud)

我的解释ClassCastException:

当您invokeMethod第一次调用时,JVM使用较慢的路径,这对程序员来说更容易调试(因此它更慢!),因此argument type mismatch当您传递错误的参数时它会显示更友好的消息.

当您invokeMethod多次调用时(在我的测试中有16次足够),JVM GeneratedMethodAccessor***在运行时生成了一个更快的错误检查.因此,java.lang.ClassCastException@603a3e21当你传递一个不好的论点时,它会显示如此丑陋的信息.