use of retrofit2.CallAdapter in project retrofit by square.
the class BehaviorDelegate method returning.
// Single-interface proxy creation guarded by parameter safety.
@SuppressWarnings("unchecked")
public <R> T returning(Call<R> call) {
final Call<R> behaviorCall = new BehaviorCall<>(behavior, executor, call);
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class[] { service }, new InvocationHandler() {
@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);
}
});
}
use of retrofit2.CallAdapter in project retrofit by square.
the class RetrofitTest method callCallCustomAdapter.
@Test
public void callCallCustomAdapter() {
final AtomicBoolean factoryCalled = new AtomicBoolean();
final AtomicBoolean adapterCalled = new AtomicBoolean();
class MyCallAdapterFactory extends CallAdapter.Factory {
@Override
public CallAdapter<?, ?> get(final Type returnType, Annotation[] annotations, Retrofit retrofit) {
factoryCalled.set(true);
if (getRawType(returnType) != Call.class) {
return null;
}
return new CallAdapter<Object, Call<?>>() {
@Override
public Type responseType() {
return getParameterUpperBound(0, (ParameterizedType) returnType);
}
@Override
public Call<Object> adapt(Call<Object> call) {
adapterCalled.set(true);
return call;
}
};
}
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addCallAdapterFactory(new MyCallAdapterFactory()).build();
CallMethod example = retrofit.create(CallMethod.class);
assertThat(example.getResponseBody()).isNotNull();
assertThat(factoryCalled.get()).isTrue();
assertThat(adapterCalled.get()).isTrue();
}
use of retrofit2.CallAdapter in project retrofit by square.
the class RetrofitTest method customCallAdapter.
@Test
public void customCallAdapter() {
class GreetingCallAdapterFactory extends CallAdapter.Factory {
@Override
public CallAdapter<Object, String> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
if (getRawType(returnType) != String.class) {
return null;
}
return new CallAdapter<Object, String>() {
@Override
public Type responseType() {
return String.class;
}
@Override
public String adapt(Call<Object> call) {
return "Hi!";
}
};
}
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).addCallAdapterFactory(new GreetingCallAdapterFactory()).build();
StringService example = retrofit.create(StringService.class);
assertThat(example.get()).isEqualTo("Hi!");
}
use of retrofit2.CallAdapter in project retrofit by square.
the class RetrofitTest method methodAnnotationsPassedToCallAdapter.
@Test
public void methodAnnotationsPassedToCallAdapter() {
final AtomicReference<Annotation[]> annotationsRef = new AtomicReference<>();
class MyCallAdapterFactory extends CallAdapter.Factory {
@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
annotationsRef.set(annotations);
return null;
}
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).addCallAdapterFactory(new MyCallAdapterFactory()).build();
Annotated annotated = retrofit.create(Annotated.class);
// Trigger internal setup.
annotated.method();
Annotation[] annotations = annotationsRef.get();
assertThat(annotations).hasAtLeastOneElementOfType(Annotated.Foo.class);
}
Aggregations