users@jersey.java.net

Problem with ContextResolver, generics...

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Tue, 28 Apr 2009 14:25:14 -0700

I am encountering some problems when trying to inject @Context
injectable objects.
I am trying to reduce amount of boilerplate code, so I was trying 2 approaches:

(a) Create generic SimpleResolver<T> class (implements
ContextResolver<T>), to be instantiated for specific types (resolver =
new ContextResolver<ContextClass>(instance)), or
(b) Just include Context object itself in Set returned by
Application#getSingletons

Neither works, and I think it sort of makes sense, since:

(a) Type erasure causes effective type to be wiped out; and result
being same as if I just extended ContextResolver<Object>.
(b) There is nothing to indicate role for the type; but I often
(usually) can't add @Provider within that class either.

Ok; now, I was thinking that to make (a) work I have to use dummy
sub-class, like so:

static class FoobarResolver extends SimpleResolver<Foobar> ...

but this for some reason does not seem to work either. At least not if
super-class has @Provider annotation. In the end I ended up just
writing the full boiler-plate code.
Perhaps type-detection code just can not deal with multi-level
inheritance? That is, concrete class does not implement
ContextResolver<?>, but an intermediate base class that does.

But as to (b),
it seems like it'd be good to have a wrapper that allows using any
given object as a Context.
Something like:

ContextResolverWrapper(Class<T> contextType, T instance);

(for cases where instance is an instance of sub-class of T; or if it's
the exact type, just pass Object)

Also: JAX-RS specs suggest that there should be warnings given if
singletons/instances passed in are not actually valid (root) resource
or providers. But I don't see any warnings emitted by Jersey, even
though registration seems to fail.

-+ Tatu +-