反射找不到构造函数

时间:2018-06-24 14:53:07

标签: java android reflection

我上了那个课:

public class DNDRunner {
    private NotificationManager mNoMan;

    public DNDRunner(Context context) {
        mNoMan = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void run(String param) {
        mNoMan.setZenMode(Integer.parseInt(param), null, "DNDRunner");
    }
}

然后我通过反射调用run方法:

try {
    Class mRunner = Class.forName(runner);
    Constructor constructor = mRunner.getConstructor(new Class[]{Context.class});
    Object object = constructor.newInstance(new Object[]{mContext});
    Method run = mRunner.getMethod("run", new Class[]{String.class});
    run.invoke(object, new Object[]{value});
} catch (Exception e) {
    Log.e(TAG, "Runner", e);
}

但是我得到了

java.lang.NoSuchMethodException: <init> [class android.content.Context]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getConstructor(Class.java:1725)

我做错了什么?构造函数显然在那里

更新:

进行以下测试:

private void executeRunner() {
    try {
        Class mRunner = Class.forName(runner);
        Constructor<?> constructor = mRunner.getDeclaredConstructor(Context.class);
        Log.d(TAG, "Is public constructor? " + Modifier.isPublic(constructor.getModifiers()));
        Object object = constructor.newInstance(mContext);
        Method run = mRunner.getDeclaredMethod("run", String.class);
        run.invoke(object, value);
    } catch (Exception e) {
        Log.e(TAG, "Runner", e);
    }
}

但是我遇到了同样的错误:

06-25 20:24:44.703  2420  2420 E OmniAction: Runner
06-25 20:24:44.703  2420  2420 E OmniAction: java.lang.NoSuchMethodException: <init> [class android.content.Context]
06-25 20:24:44.703  2420  2420 E OmniAction:    at java.lang.Class.getConstructor0(Class.java:2320)
06-25 20:24:44.703  2420  2420 E OmniAction:    at java.lang.Class.getDeclaredConstructor(Class.java:2166)
06-25 20:24:44.703  2420  2420 E OmniAction:    at org.omnirom.omnilib.actions.OmniAction.executeRunner(OmniAction.java:148)
06-25 20:24:44.703  2420  2420 E OmniAction:    at org.omnirom.omnilib.actions.OmniAction.execute(OmniAction.java:89)
06-25 20:24:44.703  2420  2420 E OmniAction:    at org.omnirom.omnibrain.EventService.execOmniActions(EventService.java:302)
06-25 20:24:44.703  2420  2420 E OmniAction:    at org.omnirom.omnibrain.EventService.execOnConnectActions(EventService.java:257)
06-25 20:24:44.703  2420  2420 E OmniAction:    at org.omnirom.omnibrain.EventService.-wrap5(Unknown Source:0)
06-25 20:24:44.703  2420  2420 E OmniAction:    at org.omnirom.omnibrain.EventService$2.onReceive(EventService.java:189)
06-25 20:24:44.703  2420  2420 E OmniAction:    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52497(LoadedApk.java:1313)
06-25 20:24:44.703  2420  2420 E OmniAction:    at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.$m$7(Unknown Source:4)
06-25 20:24:44.703  2420  2420 E OmniAction:    at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.run(Unknown Source:39)
06-25 20:24:44.703  2420  2420 E OmniAction:    at android.os.Handler.handleCallback(Handler.java:790)
06-25 20:24:44.703  2420  2420 E OmniAction:    at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 20:24:44.703  2420  2420 E OmniAction:    at android.os.Looper.loop(Looper.java:164)
06-25 20:24:44.703  2420  2420 E OmniAction:    at android.app.ActivityThread.main(ActivityThread.java:6499)
06-25 20:24:44.703  2420  2420 E OmniAction:    at java.lang.reflect.Method.invoke(Native Method)
06-25 20:24:44.703  2420  2420 E OmniAction:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
06-25 20:24:44.703  2420  2420 E OmniAction:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
06-25 20:24:44.705  1692  1697 I zygote64: Do full code cache collection, code=507KB, data=308KB

奇怪....:(

我不明白它如何找到该类,但无法读取构造函数。

在重新构建之前,我正在清理对先前代码的所有引用:

find out/ -name *OmniBrain* -exec rm -rf {} +

2 个答案:

答案 0 :(得分:0)

尝试一下:

Constructor<?> constructor = mRunner.getConstructor(Context.class); 
Object object = constructor.newInstance(this)

答案 1 :(得分:0)

您会笑的……proguard正在擦除未使用的方法,我将类保留在proguard配置中,但是忘记了这些方法……大声笑

https://gerrit.omnirom.org/c/android_packages_apps_OmniBrain/+/31227/3/proguard.flags