• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!

Casting and reflection issues in a method argument. okhttp3.OkHttpClient$Builder;->addInterceptor(···)

Search This thread

ElJaviLuki

New member
Sep 21, 2021
1
0
Badajoz, Spain
Hi guys, I'm trying to hook the constructor of
Java:
okhttp3.OkHttpClient$Builder
in order to add an interceptor that converts any OkHttp call into a curl call and log it (see https://github.com/mrmike/Ok2Curl by MrMike). But I'm having problems with reflection, say I must haven't understood it pretty well...

The idea is to call
Java:
.addInterceptor(okhttp3.Interceptor)
method JUST after the
Java:
OkHttpClient$Builder
instance has been constructed, but it's more painful than expected. (See code and code comments for further details)


Java:
@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);
                }
            });
        }
    }

Thanks in advance!