Search in sources :

Example 1 with CallAdapter

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);
        }
    });
}
Also used : CallAdapter(retrofit2.CallAdapter) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type)

Example 2 with CallAdapter

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Example 3 with CallAdapter

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!");
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 4 with CallAdapter

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);
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Aggregations

Type (java.lang.reflect.Type)4 ParameterizedType (java.lang.reflect.ParameterizedType)3 MediaType (okhttp3.MediaType)3 Test (org.junit.Test)3 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)3 NonMatchingCallAdapterFactory (retrofit2.helpers.NonMatchingCallAdapterFactory)3 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)3 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)3 Annotation (java.lang.annotation.Annotation)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CallAdapter (retrofit2.CallAdapter)1