更新 - 質問をより明確にする。Reflections IllegalArgumentExceptionの原因
リフレクションでメソッドを呼び出すときにClassCastExceptionを取得する可能性がある原因は何ですか?
リフレクションでメソッドを呼び出そうとしているときに、アプリケーションの一部として次のstacktraceを取得しました。
java.lang.IllegalArgumentException: [email protected]
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)
UPDATE - ここでのサンプルコードはある私は、これは
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;
}
}
}
テストクラスである
プロキシクラスを作成するには、例外
インターフェイスを再作成しようとする
package my.tests;
public interface ReflectionsInterface {
public abstract void doSomething();
}
を書きました
私は元に戻すことができません私が引数の型の不一致とClassCastExceptionが発生すると、私はerlします。 私は例外を再作成することができず、なぜそれが来るのか知りたいと思います。それを再作成した作業コードまたはこの例外をスローするソースコードのリファレンスは良いでしょう。
私はMethod.classのjavadocsとソースコードを使いましたが、なぜこれを理解できないのですか?エラーが発生します。
コードに対して行った最近の編集により、スローされた例外が排除されました。あなたはこの新しいSSCCEであってもまだエラーが出ていると言っていますか?そうでない場合は、実際に問題が発生していることを示す前のコードに戻してください。 – Perception
エラーは私のアプリケーションコードから来ています。これは非常に精巧で、私はここに投稿できません。これは私の(失敗した)エラーを再現するか、なぜ来るのか理解しようとしています。 –