private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter( Retrofit retrofit, Method method) { Type returnType = method.getGenericReturnType(); Annotation[] annotations = method.getAnnotations(); try { //noinspection unchecked return (CallAdapter<ResponseT, ReturnT>) retrofit.callAdapter(returnType, annotations); } catch (RuntimeException e) { // Wide exception range because factories are user code. throw methodError(method, e, "Unable to create call adapter for %s", returnType); } }
@Override public T invoke(Object proxy, Method method, Object[] args) throws Throwable { Type returnType = method.getGenericReturnType(); Annotation[] methodAnnotations = method.getAnnotations(); CallAdapter<R, T> callAdapter = (CallAdapter<R, T>) retrofit.callAdapter(returnType, methodAnnotations); return callAdapter.adapt(behaviorCall); } });
private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter( Retrofit retrofit, Method method) { Type returnType = method.getGenericReturnType(); Annotation[] annotations = method.getAnnotations(); try { //noinspection unchecked return (CallAdapter<ResponseT, ReturnT>) retrofit.callAdapter(returnType, annotations); } catch (RuntimeException e) { // Wide exception range because factories are user code. throw methodError(method, e, "Unable to create call adapter for %s", returnType); } }
<T> T adapt(Method method, NetworkBehavior networkBehavior, Call<?> call) { try { Class<?> behaviorCallClass = Class.forName("retrofit2.mock.BehaviorCall"); Constructor<?> constructor = behaviorCallClass.getDeclaredConstructor(NetworkBehavior.class, ExecutorService.class, Call.class); constructor.setAccessible(true); Call<?> behaviorCall = (Call<?>) constructor.newInstance(networkBehavior, Executors.newCachedThreadPool(), call); return (T) retrofit .callAdapter(method.getGenericReturnType(), method.getAnnotations()) .adapt(behaviorCall); } catch (Exception e) { throw new RuntimeException(e); } }