Retrofit的核心在於它的create方法中使用了動態代理,在這裏面主要是loadServiceMethod方法:java
如下代碼基於Retrofit2.5.0(跟2.3.0代碼存在明顯不一樣)正則表達式
public <T> T create(final Class<T> 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 Object invoke(Object proxy, Method method, @Nullable Object[] args) throws Throwable {
//省略無關代碼
return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
}
});
}
複製代碼
首先來看loadServiceMethod方法:json
Retrofit.loadServiceMethod(Method method)數組
private final Map<Method, ServiceMethod<?>> serviceMethodCache = new ConcurrentHashMap<>();
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是一個緩存的Map,這個方法主要就是執行了ServiceMethod.parseAnnotations緩存
這個方法返回ServiceMethod這個類:ide
abstract class ServiceMethod<T> {//抽象類
static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {//解析註解
RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method);//請求工廠初始化
Type returnType = method.getGenericReturnType();//獲取方法的返回類型
//省略無關代碼
return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);//返回解析結果
}
abstract T invoke(Object[] args);
}
複製代碼
繼續看RequestFactory.parseAnnotations(retrofit, method):ui
static RequestFactory parseAnnotations(Retrofit retrofit, Method method) {
return new Builder(retrofit, method).build();
}
複製代碼
在這裏又用到了建立者方法來建立這個請求工廠,進入Builder:this
Builder(Retrofit retrofit, Method method) {
this.retrofit = retrofit;//當前的retrofit實例
this.method = method;//請求的方法
this.methodAnnotations = method.getAnnotations();//註解
this.parameterTypes = method.getGenericParameterTypes();//參數的類型集合
this.parameterAnnotationsArray = method.getParameterAnnotations();//參數的註解集合
}
複製代碼
能夠看到這個建立類裏面包含了咱們在接口定義的方法的全部信息,包括註解和參數,再繼續來看它的build方法作了什麼:spa
RequestFactory build() {
for (Annotation annotation : methodAnnotations) {
parseMethodAnnotation(annotation);//重點1.解析方法上面的註解
}
//省略無關代碼
int parameterCount = parameterAnnotationsArray.length;
parameterHandlers = new ParameterHandler<?>[parameterCount];
for (int p = 0; p < parameterCount; p++) {
parameterHandlers[p] = parseParameter(p, parameterTypes[p], parameterAnnotationsArray[p]);//重點2.解析方法的參數
}
//省略無關代碼
return new RequestFactory(this);
}
複製代碼
在這裏,進行了對方法的具體解析,主要是兩個步驟代理
- 解析方法上面的註解parseMethodAnnotation
- 解析方法的請求參數註解parseParameter
到兩個方法裏面看看:
RequestFactory.parseMethodAnnotation
private void parseMethodAnnotation(Annotation annotation) {
if (annotation instanceof DELETE) {
parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false);
} else if (annotation instanceof GET) {
parseHttpMethodAndPath("GET", ((GET) annotation).value(), false);
} else if (annotation instanceof HEAD) {
parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false);
} else if (annotation instanceof PATCH) {
parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(), true);
} else if (annotation instanceof POST) {
parseHttpMethodAndPath("POST", ((POST) annotation).value(), true);
} else if (annotation instanceof PUT) {
parseHttpMethodAndPath("PUT", ((PUT) annotation).value(), true);
} else if (annotation instanceof OPTIONS) {
parseHttpMethodAndPath("OPTIONS", ((OPTIONS) annotation).value(), false);
} else if (annotation instanceof HTTP) {
HTTP http = (HTTP) annotation;
parseHttpMethodAndPath(http.method(), http.path(), http.hasBody());
} else if (annotation instanceof retrofit2.http.Headers) {
String[] headersToParse = ((retrofit2.http.Headers) annotation).value();
if (headersToParse.length == 0) {
throw methodError(method, "@Headers annotation is empty.");
}
headers = parseHeaders(headersToParse);
} else if (annotation instanceof Multipart) {
if (isFormEncoded) {
throw methodError(method, "Only one encoding annotation is allowed.");
}
isMultipart = true;
} else if (annotation instanceof FormUrlEncoded) {
if (isMultipart) {
throw methodError(method, "Only one encoding annotation is allowed.");
}
isFormEncoded = true;
}
}
複製代碼
在這裏,看到了熟悉的一幕,咱們日常使用Retrofit時在方法上面使用的@POST和@GET之類的註解,就是在這個方法裏面進行的解析,這裏先作一個判斷,繼續調用parseHttpMethodAndPath
RequestFactory.parseHttpMethodAndPath
private void parseHttpMethodAndPath(String httpMethod, String value, boolean hasBody) {
if (this.httpMethod != null) {
throw methodError(method, "Only one HTTP method is allowed. Found: %s and %s.",
this.httpMethod, httpMethod);
}
this.httpMethod = httpMethod;//請求方法
this.hasBody = hasBody;//是否有請求體
if (value.isEmpty()) {
return;
}
// Get the relative URL path and existing query string, if present.
int question = value.indexOf('?');
if (question != -1 && question < value.length() - 1) {
// Ensure the query string does not have any named parameters.
String queryParams = value.substring(question + 1);
Matcher queryParamMatcher = PARAM_URL_REGEX.matcher(queryParams);
if (queryParamMatcher.find()) {
throw methodError(method, "URL query string \"%s\" must not have replace block. "
+ "For dynamic query parameters use @Query.", queryParams);
}
}
this.relativeUrl = value;//相對請求地址
this.relativeUrlParamNames = parsePathParameters(value);//解析請求連接裏面的參數
}
複製代碼
在這裏進行了關於請求的方法等相關屬性的賦值,最後調用parsePathParameters解析咱們在註解裏面傳入的地址的參數:
RequestFactory.parsePathParameters
private static final String PARAM = "[a-zA-Z][a-zA-Z0-9_-]*";
private static final Pattern PARAM_URL_REGEX = Pattern.compile("\\{(" + PARAM + ")\\}");
static Set<String> parsePathParameters(String path) {
Matcher m = PARAM_URL_REGEX.matcher(path);
Set<String> patterns = new LinkedHashSet<>();
while (m.find()) {
patterns.add(m.group(1));
}
return patterns;
}
複製代碼
這裏應該都能看懂,使用正則表達式來匹配。
到這裏,完成了對方法上面的註解的解析,接下來,進行對方法的參數的解析:
RequestFactory.parseParameter
private ParameterHandler<?> parseParameter(
int p, Type parameterType, @Nullable Annotation[] annotations) {
ParameterHandler<?> result = null;
if (annotations != null) {
for (Annotation annotation : annotations) {
ParameterHandler<?> annotationAction =
parseParameterAnnotation(p, parameterType, annotations, annotation);//參數解析
//省略無關代碼
result = annotationAction;
}
}
//省略無關代碼
return result;
}
複製代碼
返回的ParameterHandler類爲參數的句柄,這裏是一個抽象類,裏面有Header,Path,Query等跟咱們在參數前面加的標註同名的實現類。
使用一個循環,來解析每一個參數的註解,這裏調用了parseParameterAnnotation方法,這個方法跟剛纔解析方法上面的註解parseMethodAnnotation很像,裏面進行了不少判斷,在參數裏面能夠加的註解不少,因此方法太長,這裏看一看咱們常常用到的GET請求的@Query註解的解析:
RequestFactory.parseParameterAnnotation
//省略無關代碼
} else if (annotation instanceof Query) {
validateResolvableType(p, type);
Query query = (Query) annotation;
String name = query.value();
boolean encoded = query.encoded();
Class<?> rawParameterType = Utils.getRawType(type);//獲取類型
gotQuery = true;
if (Iterable.class.isAssignableFrom(rawParameterType)) {//是不是集合
if (!(type instanceof ParameterizedType)) {
throw parameterError(method, p, rawParameterType.getSimpleName()
+ " must include generic type (e.g., "
+ rawParameterType.getSimpleName()
+ "<String>)");
}
ParameterizedType parameterizedType = (ParameterizedType) type;
Type iterableType = Utils.getParameterUpperBound(0, parameterizedType);
Converter<?, String> converter =
retrofit.stringConverter(iterableType, annotations);
return new ParameterHandler.Query<>(name, converter, encoded).iterable();
} else if (rawParameterType.isArray()) {//是不是集合的數組
Class<?> arrayComponentType = boxIfPrimitive(rawParameterType.getComponentType());
Converter<?, String> converter =
retrofit.stringConverter(arrayComponentType, annotations);
return new ParameterHandler.Query<>(name, converter, encoded).array();
} else {//普通類型
Converter<?, String> converter =
retrofit.stringConverter(type, annotations);
return new ParameterHandler.Query<>(name, converter, encoded);
}
} else if (annotation instanceof QueryName) {
//省略無關代碼
複製代碼
在這個方法裏面進行了類型和泛型相關的判斷,裏面都調用了retrofit.stringConverter方法:
retrofit.stringConverter
public <T> Converter<T, String> stringConverter(Type type, Annotation[] annotations) {
checkNotNull(type, "type == null");
checkNotNull(annotations, "annotations == null");
for (int i = 0, count = converterFactories.size(); i < count; i++) {
Converter<?, String> converter =
converterFactories.get(i).stringConverter(type, annotations, this);
if (converter != null) {
//noinspection unchecked
return (Converter<T, String>) converter;
}
}
// Nothing matched. Resort to default converter which just calls toString().
//noinspection unchecked
return (Converter<T, String>) BuiltInConverters.ToStringConverter.INSTANCE;
}
複製代碼
這個方法的做用是獲取請求參數的json解析器,一個循環從converterFactories數組中依次獲取加入的解析器工廠,咱們在以前建立Retrofit傳入的是GsonConverterFactory,打開這個類,並無發現stringConverter方法,再打開它的父類Converter.Factory,看到了這個方法:
public @Nullable Converter<?, String> stringConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return null;
}
複製代碼
返回的是空,說明這裏不須要使用解析工廠將請求的參數轉化爲String。因此直接調用最後一句
return (Converter<T, String>) BuiltInConverters.ToStringConverter.INSTANCE;
複製代碼
static final class ToStringConverter implements Converter<Object, String> {
static final ToStringConverter INSTANCE = new ToStringConverter();
@Override public String convert(Object value) {
return value.toString();
}
}
複製代碼
返回的是一個默認的轉化類,在這個類的convert方法使用的是類自身的toString來轉化。
到此爲止,就完成了咱們在接口中定義的那個方法的所有解析。
Retrofit使用了動態代理,因此每次執行咱們在接口中定義的方法會來到動態代理中的invoke方法,在這裏面,又執行了loadServiceMethod來實現對方法的解析,主要是兩個步驟: