美文网首页
retrofit源码一引子

retrofit源码一引子

作者: crossroads | 来源:发表于2021-02-20 19:23 被阅读0次

前言

为了饭碗,从攻克retrofit源码开始,版本为2.9.0。学习retrofit之前,要先稍微了解下动态代理的基础哦~这里不讲解。如果忘记retrofit的基础用法,可以看retrofit基本用法复习一下

retrofit构建

   val retrofit = Retrofit.Builder()
            .baseUrl("http://localhost/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

这里用了构建者模式,首先看第一行Builder方法做了什么

  Builder(Platform platform) {
      this.platform = platform;
    }
  public Builder() {
      this(Platform.get());
    }

看来是初始化platform,查看Platform.get()方法

  private static final Platform PLATFORM = findPlatform();
  static Platform get() {
    return PLATFORM;
  }
  private static Platform findPlatform() {
    return "Dalvik".equals(System.getProperty("java.vm.name"))
        ? new Android() 
        : new Platform(true);
  }

如果java虚拟机的实现名称是“Dalvik”,就使用Android平台否则使用java8平台。platform的具体实现后续再看。
接下来查看build方法

 public Retrofit build() {
      if (baseUrl == null) {//1
        throw new IllegalStateException("Base URL required.");
      }
      okhttp3.Call.Factory callFactory = this.callFactory;//2
      if (callFactory == null) {//3
        callFactory = new OkHttpClient();
      }
      Executor callbackExecutor = this.callbackExecutor;//4
      if (callbackExecutor == null) {
        callbackExecutor = platform.defaultCallbackExecutor();//5
      }
      List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);//6
      callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));
      List<Converter.Factory> converterFactories =
          new ArrayList<>(
              1 + this.converterFactories.size() + platform.defaultConverterFactoriesSize());//7
      converterFactories.add(new BuiltInConverters());
      converterFactories.addAll(this.converterFactories);
      converterFactories.addAll(platform.defaultConverterFactories());
      return new Retrofit(callFactory, baseUrl,  unmodifiableList(converterFactories),
          unmodifiableList(callAdapterFactories), callbackExecutor,  validateEagerly);
    }

注释1可看出baseUrl是必须指定的。
注释2处callFactory默认为this.callFactory,这个值是在构建retrofit通过.client(OkHttpClient client)设置的,注释3处如果没有设置callFactory会默认new一个OkHttpClient对象。
注释4处callbackExecutor默认为this.callbackExecutor,这个值可以在构建retrofit通过.callbackExecutor(Executor executor) 方法设置,注释5处如果没有设置就用平台的Executor,主要用于将回调传递给UI线程。
注释6处callAdapterFactories主要用于存储对call进行转化的对象,后面会讲解。
注释7处converterFactories主要用于存储转化对象数据,例如在构建retrofit传的GsonConverterFactory可以将返回的数据转换为gson对象, 会面会讲解。

Call的创建过程

创建完retrofit,就可以调用如下代码来生成接口的动态代理对象

val service: MainNetWork = retrofit.create(MainNetwork::class.java)

interface MainNetwork {
    @GET("next_title.json")
    fun fetchNextTitle(): Call<TitleModel>
}

看看create方法做了什么吧

  public <T> T create(final Class<T> service) {
    validateServiceInterface(service);
    return (T)Proxy.newProxyInstance(service.getClassLoader(),new Class<?>[] {service},
            new InvocationHandler() {
              private final Platform platform = Platform.get();
              private final Object[] emptyArgs = new Object[0];

              @Override
              public @Nullable Object invoke(Object proxy, Method method, @Nullable Object[] args)
                  throws Throwable {
                // If the method is a method from Object then defer to normal invocation.
                if (method.getDeclaringClass() == Object.class) {
                  return method.invoke(this, args);
                }
                args = args != null ? args : emptyArgs;
                return platform.isDefaultMethod(method)
                    ? platform.invokeDefaultMethod(method, service, proxy, args)
                    : loadServiceMethod(method).invoke(args); //1
              }
            });
  }

这里create方法返回了一个Proxy.newProxyInstance动态代理对象,如上面的例子,如果我们调用service的fetchNextTitle() 其实最终会调用InvocationHandler的invoke方法。Proxy.newProxyInstance有三个参数:代理对象、调用的方法、方法的参数。注释1处的loadServiceMethod(method)中的method就是我们定义的fetchNextTitle() 方法。
那这个方法里做了什么呢?

ServiceMethod<?> loadServiceMethod(Method method) {
    ServiceMethod<?> result = serviceMethodCache.get(method);
    if (result != null) return result;
    synchronized (serviceMethodCache) {
      result = serviceMethodCache.get(method);
      if (result == null) {
        result = ServiceMethod.parseAnnotations(this, method);
        serviceMethodCache.put(method, result);
      }
    }
    return result;
  }

首先会从serviceMethodCache查询传入的方法是否有缓存,如果有就用缓存的ServiceMethod,如果没有就创建一个,并加入serviceMethodCache缓存起来。接下来看ServiceMethod是创建的。

abstract class ServiceMethod<T> {
  static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {
    RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method);//1
    Type returnType = method.getGenericReturnType();//2
    if (Utils.hasUnresolvableType(returnType)) {
      throw methodError(
          method,
          "Method return type must not include a type variable or wildcard: %s",
          returnType);
    }
    if (returnType == void.class) {
      throw methodError(method, "Service methods cannot return void.");
    }

    return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);//3
  }

  abstract @Nullable T invoke(Object[] args);
}

注释1处各种注解解析,目的是解析请求参数生成RequestFactory用于生成okHttp的Request,
注释2处获取返回值的类型,
注释3处用于生成具体的网络请求。
先看到底是如何进行网络请求的, HttpServiceMethod.parseAnnotations内部实现是什么呢?会有点长,所以把里面牵扯到协程部分的代码去除。

 static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations(
      Retrofit retrofit, Method method, RequestFactory requestFactory) {
    boolean isKotlinSuspendFunction = requestFactory.isKotlinSuspendFunction;
    boolean continuationWantsResponse = false;
    boolean continuationBodyNullable = false;
    Annotation[] annotations = method.getAnnotations();//1
    Type adapterType;//2
    if (isKotlinSuspendFunction) {//协程相关
      ......
    } else {
      adapterType = method.getGenericReturnType();
    }
    CallAdapter<ResponseT, ReturnT> callAdapter =
        createCallAdapter(retrofit, method, adapterType, annotations);//3
    Type responseType = callAdapter.responseType();//4
    Converter<ResponseBody, ResponseT> responseConverter =
        createResponseConverter(retrofit, method, responseType);//5

    okhttp3.Call.Factory callFactory = retrofit.callFactory;//6
      return new CallAdapted<>(requestFactory, callFactory, responseConverter, callAdapter);//7

  }

注释1处返回method上存在的指定类型的注解,
注释2处为方法返回值类型,
注释3处创建CallAdapter对象,具体步骤后续讲解,注释4处的callAdapter的responseType是返回数据的真实类型,例如Call<TitleModel>,它就会返回TitleModel。
注释5处遍历converterFactories列表中存储的Converter.Factory,并返回一个合适的Converter<ResponseBody, ?>用于后面转换对象。此前我们在构建Retrofit 调用了addConverterFactory(GsonConverterFactory.create())将GsonConverterFactory(Converter.Factory的子类)添加到converterFactories列表中,表示返回的数据支持转换为Json对象。
注释6处获得okHttpClient对象
注释7处返回CallAdapted对象。
至此,loadServiceMethod(method).invoke(args)的loadServiceMethod(method)结束,接下来是调用它的invoke方法,这个方法又有什么呢?

  @Override
  final @Nullable ReturnT invoke(Object[] args) {
    Call<ResponseT> call = new OkHttpCall<>(requestFactory, args, callFactory, responseConverter);
    return adapt(call, args);
  }

继续看adapt方法,


    @Override
    protected ReturnT adapt(Call<ResponseT> call, Object[] args) {
      return callAdapter.adapt(call);
    }

既然回到了callAdapter,那么我们就看看callAdapter是什么吧。还记得HttpServiceMethod<ResponseT, ReturnT> parseAnnotations()方法注释3处

   CallAdapter<ResponseT, ReturnT> callAdapter =
        createCallAdapter(retrofit, method, adapterType, annotations);

创建的callAdapter么?这个callAdapter对象就是这里来的。咱们跟踪看看createCallAdapter内部是如何创建这个对象的。

  private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter(
      Retrofit retrofit, Method method, Type returnType, Annotation[] annotations) {
      return (CallAdapter<ResponseT, ReturnT>) retrofit.callAdapter(returnType, annotations);
  }

继续不断跟踪,可得到

    int start = callAdapterFactories.indexOf(skipPast) + 1;
    for (int i = start, count = callAdapterFactories.size(); i < count; i++) {
      CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this);
      if (adapter != null) {
        return adapter;
      }
    }

因此,这个callAdapter是通过在构建Retrofit调用build方法时adapterFactories添加的对象的get方法,adapterFactories列表默认会添加defaultCallAdapterFactory,defaultCallAdapterFactory指的是ExecutorCallAdapterFactory,ExecutorCallAdapterFactory的get方法如下所示

    @Nullable
    public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
        if (getRawType(returnType) != Call.class) {
            return null;
        } else if (!(returnType instanceof ParameterizedType)) {
            throw new IllegalArgumentException("Call return type must be parameterized as Call<Foo> or Call<? extends Foo>");
        } else {
            final Type responseType = Utils.getParameterUpperBound(0, (ParameterizedType)returnType);
            final Executor executor = Utils.isAnnotationPresent(annotations, SkipCallbackExecutor.class) ? null : this.callbackExecutor;//1
            return new CallAdapter<Object, Call<?>>() {
                public Type responseType() {
                    return responseType;
                }

                public Call<Object> adapt(Call<Object> call) {
                    return (Call)(executor == null ? call : new DefaultCallAdapterFactory.ExecutorCallbackCall(executor, call));
                }
            };
        }
    }

注释1处的executor来自构建retrofit的build方法,即 platform.defaultCallbackExecutor(),其代码回顾下如下:

    Executor callbackExecutor = this.callbackExecutor;
      if (callbackExecutor == null) {
        callbackExecutor = platform.defaultCallbackExecutor();
      }

可以看出,get方法会获得CallAdapter对象,上面提到过它的responseType方法返回数据的真实类型,adapt方法则是创建ExecutorCallbackCall,用于将call的回调转发至UI线程。ExecutorCallbackCall的代码如下

static final class ExecutorCallbackCall<T> implements Call<T> {
        final Executor callbackExecutor;// 默认为platform.defaultCallbackExecutor()
        final Call<T> delegate; //OkHttpCall

        ExecutorCallbackCall(Executor callbackExecutor, Call<T> delegate) {
            this.callbackExecutor = callbackExecutor;
            this.delegate = delegate;
        }

        @Override
    public void enqueue(final Callback<T> callback) {
      Objects.requireNonNull(callback, "callback == null");

      delegate.enqueue(//1
          new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, final Response<T> response) {
              callbackExecutor.execute(//2
                  () -> {
                    if (delegate.isCanceled()) {
                      callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
                    } else {
                      callback.onResponse(ExecutorCallbackCall.this, response);
                    }
                  });
            }

            @Override
            public void onFailure(Call<T> call, final Throwable t) {
              callbackExecutor.execute(() -> callback.onFailure(ExecutorCallbackCall.this, t));//2
            }
          });
    }
       ......
    }

注释1处其实就是调用了OkHttpCall的enqueue方法,注释2处通过callbackExecutor将请求回调到UI线程。这里先看platform.defaultCallbackExecutor()是如何回调到UI线程的,这里只看Android platform。

static final class Android extends Platform {
   ......
    @Override
    public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }
    static final class MainThreadExecutor implements Executor {
      private final Handler handler = new Handler(Looper.getMainLooper());
      @Override
      public void execute(Runnable r) {
        handler.post(r);
      }
    }
  }

啊,逃不开的handler~
下面的OkHttpCall的enqueue是重中之重啊,下一节再讲,总结一下这一小节:
这一小节主要是引子,了解到retrofit在builder()方法中会根据不同的虚拟机初始化不同的平台platform,这个platform持有回调到UI线程的Executor,默认的 CallAdapter.Factory与Converter.Factory。了解到retrofit的build()方法中如果baseurl为null会抛出异常,没有okhttpclient会默认new一个,为converterFactories、callAdapterFactories、callbackExecutor成员变量赋值,默认值主要来自platform。了解到retrofit.create()方法内部是一个动态代理,接口的代理对象调用方法时,最终是调用okHttpCall的enqueue方法。在调用enqueue之前,会先注解解析出适合okhttpclient进行请求的request存储到RequestFactory中,然后将requestFactory、okhttpclient、从converterFactories中获取的Converter、从callAdapterFactories获取到的CallAdapter作为参数new一个CallAdapted对象,然后调用CallAdapted的adapt方法也就是调用了OkHttpCall的enqueue。
这里的Converter、CallAdapter的用法后续也会在讲解。

参考资料

刘望舒
retrofit2深度解析

相关文章

网友评论

      本文标题:retrofit源码一引子

      本文链接:https://www.haomeiwen.com/subject/yantfltx.html