在 Retrofit 中一個(gè)重要點(diǎn)就是動(dòng)態(tài)代理屿愚,我們只需要定義 Service 接口,并在里面定義需要的網(wǎng)絡(luò)請求方法即可,具體這個(gè) Service 接口是怎樣實(shí)現(xiàn)的裹纳?
public <T> T create(final Class<T> service) {
Utils.validateServiceInterface(service);
if (validateEagerly) {
eagerlyValidateMethods(service);
}
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
new InvocationHandler() {
private final Platform platform = Platform.get();
@Override public 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);
}
if (platform.isDefaultMethod(method)) {
return platform.invokeDefaultMethod(method, service, proxy, args);
}
ServiceMethod<Object, Object> serviceMethod =
(ServiceMethod<Object, Object>) loadServiceMethod(method);
OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
return serviceMethod.callAdapter.adapt(okHttpCall);
}
});
}
其中看下 Proxy.newProxyInstance 和 InvocationHandler 是怎么使用的,自己寫個(gè)小荔枝紧武。
- 定義一個(gè)接口
public interface Service {
int getInt();
}
- 使用動(dòng)態(tài)代理
public class Main {
public static void main(String[] args) {
Service service = (Service) Proxy.newProxyInstance(
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
System.out.println(method.getName());
return 12;
}
});
System.out.println(service.toString());
System.out.println(service.getInt());
}
}
-----
輸出
Main$1@4b67cf4d
getInt
12
em 動(dòng)態(tài)代理就是使用 ...