what-the-bank/sources/retrofit2/Retrofit.java

387 lines
16 KiB
Java

package retrofit2;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.BuiltInConverters;
import retrofit2.CallAdapter;
import retrofit2.Converter;
/* loaded from: classes.dex */
public final class Retrofit {
final HttpUrl baseUrl;
final List<CallAdapter.Factory> callAdapterFactories;
final Call.Factory callFactory;
final Executor callbackExecutor;
final List<Converter.Factory> converterFactories;
private final Map<Method, ServiceMethod<?>> serviceMethodCache = new ConcurrentHashMap();
final boolean validateEagerly;
Retrofit(Call.Factory factory, HttpUrl httpUrl, List<Converter.Factory> list, List<CallAdapter.Factory> list2, Executor executor, boolean z) {
this.callFactory = factory;
this.baseUrl = httpUrl;
this.converterFactories = list;
this.callAdapterFactories = list2;
this.callbackExecutor = executor;
this.validateEagerly = z;
}
public final <T> T create(Class<T> cls) {
validateServiceInterface(cls);
return (T) Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, new InvocationHandler(this, cls) { // from class: retrofit2.Retrofit.1
final Retrofit this$0;
final Class val$service;
private final Platform platform = Platform.get();
private final Object[] emptyArgs = new Object[0];
{
this.this$0 = this;
this.val$service = cls;
}
@Override // java.lang.reflect.InvocationHandler
public Object invoke(Object obj, Method method, Object[] objArr) throws Throwable {
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, objArr);
}
if (objArr == null) {
objArr = this.emptyArgs;
}
if (this.platform.isDefaultMethod(method)) {
return this.platform.invokeDefaultMethod(method, this.val$service, obj, objArr);
}
return this.this$0.loadServiceMethod(method).invoke(objArr);
}
});
}
private void validateServiceInterface(Class<?> cls) {
if (!cls.isInterface()) {
throw new IllegalArgumentException("API declarations must be interfaces.");
}
ArrayDeque arrayDeque = new ArrayDeque(1);
arrayDeque.add(cls);
while (!arrayDeque.isEmpty()) {
Class<?> cls2 = (Class) arrayDeque.removeFirst();
if (cls2.getTypeParameters().length != 0) {
StringBuilder sb = new StringBuilder("Type parameters are unsupported on ");
sb.append(cls2.getName());
if (cls2 != cls) {
sb.append(" which is an interface of ");
sb.append(cls.getName());
}
throw new IllegalArgumentException(sb.toString());
}
Collections.addAll(arrayDeque, cls2.getInterfaces());
}
if (this.validateEagerly) {
Platform platform = Platform.get();
for (Method method : cls.getDeclaredMethods()) {
if (!platform.isDefaultMethod(method) && !Modifier.isStatic(method.getModifiers())) {
loadServiceMethod(method);
}
}
}
}
final ServiceMethod<?> loadServiceMethod(Method method) {
ServiceMethod<?> serviceMethod;
ServiceMethod<?> serviceMethod2 = this.serviceMethodCache.get(method);
if (serviceMethod2 != null) {
return serviceMethod2;
}
synchronized (this.serviceMethodCache) {
serviceMethod = this.serviceMethodCache.get(method);
if (serviceMethod == null) {
serviceMethod = ServiceMethod.parseAnnotations(this, method);
this.serviceMethodCache.put(method, serviceMethod);
}
}
return serviceMethod;
}
public final CallAdapter<?, ?> callAdapter(Type type, Annotation[] annotationArr) {
return nextCallAdapter(null, type, annotationArr);
}
public final CallAdapter<?, ?> nextCallAdapter(CallAdapter.Factory factory, Type type, Annotation[] annotationArr) {
Objects.requireNonNull(type, "returnType == null");
Objects.requireNonNull(annotationArr, "annotations == null");
int indexOf = this.callAdapterFactories.indexOf(factory) + 1;
int size = this.callAdapterFactories.size();
for (int i = indexOf; i < size; i++) {
CallAdapter<?, ?> callAdapter = this.callAdapterFactories.get(i).get(type, annotationArr, this);
if (callAdapter != null) {
return callAdapter;
}
}
StringBuilder sb = new StringBuilder("Could not locate call adapter for ");
sb.append(type);
sb.append(".\n");
if (factory != null) {
sb.append(" Skipped:");
for (int i2 = 0; i2 < indexOf; i2++) {
sb.append("\n * ");
sb.append(this.callAdapterFactories.get(i2).getClass().getName());
}
sb.append('\n');
}
sb.append(" Tried:");
int size2 = this.callAdapterFactories.size();
while (indexOf < size2) {
sb.append("\n * ");
sb.append(this.callAdapterFactories.get(indexOf).getClass().getName());
indexOf++;
}
throw new IllegalArgumentException(sb.toString());
}
public final <T> Converter<T, RequestBody> requestBodyConverter(Type type, Annotation[] annotationArr, Annotation[] annotationArr2) {
return nextRequestBodyConverter(null, type, annotationArr, annotationArr2);
}
public final <T> Converter<T, RequestBody> nextRequestBodyConverter(Converter.Factory factory, Type type, Annotation[] annotationArr, Annotation[] annotationArr2) {
Objects.requireNonNull(type, "type == null");
Objects.requireNonNull(annotationArr, "parameterAnnotations == null");
Objects.requireNonNull(annotationArr2, "methodAnnotations == null");
int indexOf = this.converterFactories.indexOf(factory) + 1;
int size = this.converterFactories.size();
for (int i = indexOf; i < size; i++) {
Converter<T, RequestBody> converter = (Converter<T, RequestBody>) this.converterFactories.get(i).requestBodyConverter(type, annotationArr, annotationArr2, this);
if (converter != null) {
return converter;
}
}
StringBuilder sb = new StringBuilder("Could not locate RequestBody converter for ");
sb.append(type);
sb.append(".\n");
if (factory != null) {
sb.append(" Skipped:");
for (int i2 = 0; i2 < indexOf; i2++) {
sb.append("\n * ");
sb.append(this.converterFactories.get(i2).getClass().getName());
}
sb.append('\n');
}
sb.append(" Tried:");
int size2 = this.converterFactories.size();
while (indexOf < size2) {
sb.append("\n * ");
sb.append(this.converterFactories.get(indexOf).getClass().getName());
indexOf++;
}
throw new IllegalArgumentException(sb.toString());
}
public final <T> Converter<ResponseBody, T> responseBodyConverter(Type type, Annotation[] annotationArr) {
return nextResponseBodyConverter(null, type, annotationArr);
}
public final <T> Converter<ResponseBody, T> nextResponseBodyConverter(Converter.Factory factory, Type type, Annotation[] annotationArr) {
Objects.requireNonNull(type, "type == null");
Objects.requireNonNull(annotationArr, "annotations == null");
int indexOf = this.converterFactories.indexOf(factory) + 1;
int size = this.converterFactories.size();
for (int i = indexOf; i < size; i++) {
Converter<ResponseBody, T> converter = (Converter<ResponseBody, T>) this.converterFactories.get(i).responseBodyConverter(type, annotationArr, this);
if (converter != null) {
return converter;
}
}
StringBuilder sb = new StringBuilder("Could not locate ResponseBody converter for ");
sb.append(type);
sb.append(".\n");
if (factory != null) {
sb.append(" Skipped:");
for (int i2 = 0; i2 < indexOf; i2++) {
sb.append("\n * ");
sb.append(this.converterFactories.get(i2).getClass().getName());
}
sb.append('\n');
}
sb.append(" Tried:");
int size2 = this.converterFactories.size();
while (indexOf < size2) {
sb.append("\n * ");
sb.append(this.converterFactories.get(indexOf).getClass().getName());
indexOf++;
}
throw new IllegalArgumentException(sb.toString());
}
public final <T> Converter<T, String> stringConverter(Type type, Annotation[] annotationArr) {
Objects.requireNonNull(type, "type == null");
Objects.requireNonNull(annotationArr, "annotations == null");
int size = this.converterFactories.size();
for (int i = 0; i < size; i++) {
Converter<T, String> converter = (Converter<T, String>) this.converterFactories.get(i).stringConverter(type, annotationArr, this);
if (converter != null) {
return converter;
}
}
return BuiltInConverters.ToStringConverter.INSTANCE;
}
public final Builder newBuilder() {
return new Builder(this);
}
/* loaded from: classes.dex */
public static final class Builder {
private HttpUrl baseUrl;
private final List<CallAdapter.Factory> callAdapterFactories;
private Call.Factory callFactory;
private Executor callbackExecutor;
private final List<Converter.Factory> converterFactories;
private final Platform platform;
private boolean validateEagerly;
Builder(Platform platform) {
this.converterFactories = new ArrayList();
this.callAdapterFactories = new ArrayList();
this.platform = platform;
}
public Builder() {
this(Platform.get());
}
Builder(Retrofit retrofit) {
this.converterFactories = new ArrayList();
this.callAdapterFactories = new ArrayList();
Platform platform = Platform.get();
this.platform = platform;
this.callFactory = retrofit.callFactory;
this.baseUrl = retrofit.baseUrl;
int size = retrofit.converterFactories.size();
int defaultConverterFactoriesSize = platform.defaultConverterFactoriesSize();
for (int i = 1; i < size - defaultConverterFactoriesSize; i++) {
this.converterFactories.add(retrofit.converterFactories.get(i));
}
int size2 = retrofit.callAdapterFactories.size();
int defaultCallAdapterFactoriesSize = this.platform.defaultCallAdapterFactoriesSize();
for (int i2 = 0; i2 < size2 - defaultCallAdapterFactoriesSize; i2++) {
this.callAdapterFactories.add(retrofit.callAdapterFactories.get(i2));
}
this.callbackExecutor = retrofit.callbackExecutor;
this.validateEagerly = retrofit.validateEagerly;
}
public final Builder client(OkHttpClient okHttpClient) {
return callFactory((Call.Factory) Objects.requireNonNull(okHttpClient, "client == null"));
}
public final Builder callFactory(Call.Factory factory) {
this.callFactory = (Call.Factory) Objects.requireNonNull(factory, "factory == null");
return this;
}
public final Builder baseUrl(URL url) {
Objects.requireNonNull(url, "baseUrl == null");
return baseUrl(HttpUrl.get(url.toString()));
}
public final Builder baseUrl(String str) {
Objects.requireNonNull(str, "baseUrl == null");
return baseUrl(HttpUrl.get(str));
}
public final Builder baseUrl(HttpUrl httpUrl) {
Objects.requireNonNull(httpUrl, "baseUrl == null");
if (!"".equals(httpUrl.pathSegments().get(r0.size() - 1))) {
throw new IllegalArgumentException("baseUrl must end in /: ".concat(String.valueOf(httpUrl)));
}
this.baseUrl = httpUrl;
return this;
}
public final Builder addConverterFactory(Converter.Factory factory) {
this.converterFactories.add((Converter.Factory) Objects.requireNonNull(factory, "factory == null"));
return this;
}
public final Builder addCallAdapterFactory(CallAdapter.Factory factory) {
this.callAdapterFactories.add((CallAdapter.Factory) Objects.requireNonNull(factory, "factory == null"));
return this;
}
public final Builder callbackExecutor(Executor executor) {
this.callbackExecutor = (Executor) Objects.requireNonNull(executor, "executor == null");
return this;
}
public final Retrofit build() {
if (this.baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
Call.Factory factory = this.callFactory;
if (factory == null) {
factory = new OkHttpClient();
}
Call.Factory factory2 = factory;
Executor executor = this.callbackExecutor;
if (executor == null) {
executor = this.platform.defaultCallbackExecutor();
}
Executor executor2 = executor;
ArrayList arrayList = new ArrayList(this.callAdapterFactories);
arrayList.addAll(this.platform.defaultCallAdapterFactories(executor2));
ArrayList arrayList2 = new ArrayList(this.converterFactories.size() + 1 + this.platform.defaultConverterFactoriesSize());
arrayList2.add(new BuiltInConverters());
arrayList2.addAll(this.converterFactories);
arrayList2.addAll(this.platform.defaultConverterFactories());
return new Retrofit(factory2, this.baseUrl, Collections.unmodifiableList(arrayList2), Collections.unmodifiableList(arrayList), executor2, this.validateEagerly);
}
public final Builder validateEagerly(boolean z) {
this.validateEagerly = z;
return this;
}
public final List<Converter.Factory> converterFactories() {
return this.converterFactories;
}
public final List<CallAdapter.Factory> callAdapterFactories() {
return this.callAdapterFactories;
}
}
public final List<Converter.Factory> converterFactories() {
return this.converterFactories;
}
public final Executor callbackExecutor() {
return this.callbackExecutor;
}
public final Call.Factory callFactory() {
return this.callFactory;
}
public final List<CallAdapter.Factory> callAdapterFactories() {
return this.callAdapterFactories;
}
public final HttpUrl baseUrl() {
return this.baseUrl;
}
}