0

okhttp3.OkHttpClient$BuilderOkHttp 呼び出しを 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