反映Java接口时收到异常

时间:2019-01-29 09:51:32

标签: java reflect

我想在我的一个SDK中反映一个接口并调用它,但是我收到了异常。以下是我的代码。请帮我解决这个问题

public class ReflectMain {
Class<?> obs = null;
InterProxy.ProxyCallback callback = new InterProxy.ProxyCallback() {
    @Override
    public void onInvoke() {
        Log.d("totolog", "invokeSuccess");
    }
};

public void reflectinterMain() {


    try {
        Class<?> reflectClass = Class.forName(ReflectClass.class.getName());
        obs = Class.forName(ReflectClass.ReflectInter.class.getName());
        Method method = reflectClass.getDeclaredMethod("addInter", obs);
        Object listener = Proxy.newProxyInstance(obs.getClassLoader(), new Class[]{obs}, new InterProxy(callback));
        method.setAccessible(true);
        method.invoke("addInter", new Object[]{listener});
        obs.newInstance();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (Exception e){
        e.printStackTrace();
    }
}

class InterProxy implements InvocationHandler {
public ProxyCallback callback = null;
interface ProxyCallback {
    void onInvoke();
}

InterProxy(ProxyCallback callback) {
    if (this.callback != null) {
        this.callback = callback;
    }
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Log.d("totolog", method.getName());
    if ("onRefletc".equals(method.getName())) {
        callback.onInvoke();
    }

    return proxy;
}

和反映类

class ReflectClass {

public ReflectInter inter = null;

void addInter(ReflectInter reflectInter) {
    inter = reflectInter;
    invoInter();
}

interface ReflectInter {
    public void onRefletc();
}

private void invoInter() {
    inter.onRefletc();
}

然后我收到了这样的例外。请帮助我分析此异常的原因及其解决方法。

     Caused by: java.lang.IllegalArgumentException: Expected receiver of type lab.mon.actlab.java.reflectInter.ReflectClass, but got java.lang.String
    at java.lang.reflect.Method.invoke(Native Method)
    at lab.mon.actlab.java.reflectInter.ReflectMain.reflectinterMain(ReflectMain.java:29)
    at lab.mon.actlab.java.reflect.ReflectActivity.onResume(ReflectActivity.java:87)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1354)
    at android.app.Activity.performResume(Activity.java:7079)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

我想知道为什么该怎么解决?

1 个答案:

答案 0 :(得分:1)

method.invoke的调用是不正确的,因为它希望将要在其上调用方法的对象的实例作为第一个参数。您正在传递方法的名称。