0

我正在尝试挂钩的构造函数,okhttp3.OkHttpClient$Builder以便添加一个拦截器,将任何 OkHttp 调用转换为 curl 调用并记录它(参见@mrmike 的 Ok2Curl 库)。但是我在反思方面遇到了问题,说我一定没有很好地理解它......

这个想法是在构造实例之后调用.addInterceptor(okhttp3.Interceptor)方法OkHttpClient$Builder,但它比预期的更痛苦。(有关详细信息,请参阅代码和代码注释

@Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        Class builderClass = XposedHelpers.findClassIfExists("okhttp3.OkHttpClient$Builder", lpparam.classLoader);
        if(builderClass != null){
            XposedBridge.hookAllConstructors(builderClass, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    CurlInterceptor interceptor = new CurlInterceptor(new Loggable() {
                        @Override
                        public void log(String message) {
                            Log.v("Ok2Curl", message);
                        }
                    });

                    /*
                        Attempt nº1:
                        Cannot do it this way, it throws an exception because there's not (and there's actually not) a method
                        'okhttp3.OkHttpClient$Builder#addInterceptor(CurlInterceptor)' since CurlInterceptor is from the Ok2Curl library.

                        Maybe the '.callMethod()' cannot make implicit casting?
                     */
                    Object attempt1 = XposedHelpers.callMethod(param.thisObject, "addInterceptor", interceptor);

                    /*
                        Attempt nº2:
                        Declaring explicitly the argument classes that addInterceptor(···) has, does not help.
                        I get an exception: it could not find an ".addInterceptor(java.lang.Class, ...CurlInterceptor)" method, so
                        this means Interceptor.class was taken as one of the method arguments and not as an argument type.
                        Anyways, given the results from attempt 1, it seems that .callMethod() will not do implicit casting whatsoever...
                     */
                    Object attempt2 = XposedHelpers.callMethod(param.thisObject, "addInterceptor", Interceptor.class, interceptor);

                    /*
                        Attempt nº3:
                        Explicit casting from CurlInterceptor to okhttp3.Interceptor did not help...
                        
                        java.lang.NoSuchMethodError: okhttp3.OkHttpClient$Builder#addInterceptor(com.moczul.ok2curl.CurlInterceptor)#bestmatch
                     */
                    Object attempt3 = XposedHelpers.callMethod(param.thisObject, "addInterceptor", (okhttp3.Interceptor) interceptor);
                }
            });
        }
    }

提前致谢!

4

0 回答 0