Sunday, 30 September 2018

Why I can not debug Proxy.newProxyInstance method?

I am using Retrofit on Android.

I define a service GitHubService.

public interface GithubService {

    @GET("users/{user}")
    Call<ResponseBody> fetchUserInfo(@Path("user") String user);

}

Then I create service.

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.github.com")
        .build();

GithubService service = retrofit.create(GithubService.class);
Call<ResponseBody> call = service.fetchUserInfo("coxier");
call.enqueue(...);

As you see, I use above code to get user info of github. And above code works well for me. Now I want to know how Retofit works so I read source code.The code below is Retrofit#create. After reading, I still don't know its magic then I decide to debug Retroft.

public <T> T create(final Class<T> service) {
  ...
  return (T) Proxy.newProxyInstance(...,
      new InvocationHandler() {
        ...
        @Override public Object invoke(..)
            throws Throwable {

          if (method.getDeclaringClass() == Object.class) {
            return method.invoke(this, args);
          }
          ...
          return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
        }
      });
}

I debug at if(method.getDeclaringClass() == Object.class) line ,however it crashes. I don't know why it crashes.

EDIT

I tested several devices.

  • Nexus 6P Android 8.0 : crash when debug
  • Xiaomi 6 Android 8.1: crash when debug
  • OnePlus 3 Android 8.0: crash when debug
  • Nexus 6P Android 7.0:works well when debug
  • Oppo Android 6.0: works well when debug
  • Samsung S4 Android 5.0: works well when debug

It may crash above Android 8.0.



from Why I can not debug Proxy.newProxyInstance method?

No comments:

Post a Comment