users@jersey.java.net

[Jersey] misuse of generics with InvocationCallback

From: Andrea Ratto <andrearatto_liste_at_yahoo.it>
Date: Thu, 4 Sep 2014 13:56:11 +0100

Hello list!

In my opinion there is a problem in the JAX-RS spec and in the Jersey
implementation with the use of generic InvocationCallback<T> objects for
async methods in the javax.ws.rs.client.AsyncInvoker interface.

Due to type erasure, the jersey implementation is forced to use
reflection to understand what entity type is required and make the
conversion.

When trying to build a bridge from the callback API to rx.Observable I
really used a generic InvocationCallback, ending up with code that looks
like this:

    Observable<CustomerInfo> call = ObservableRequest.create(target.request().async()::get);
    call.subscribe(s -> {
      System.out.println(s.getId());
    });

This code type-checks and compiles but raises an error at runtime:
"Unable to resolve generic type value of
javax.ws.rs.client.InvocationCallback<T> for an instance of class
xxxxx.jersey.client.ObservableRequest$NotifySubscriberOnResponse."

I think that the correct signature for these methods should not avoid
the type parameter:

<T> Future<T> get(*Class<T> responseType*, InvocationCallback<T> callback)

It is a little more verbose, but this is how these things must be done
with today's Java.

Am I wrong?

Cheers,

Andrea R