users@jersey.java.net

Possible bug injecting (trying to) custom _at_Context object, using jersey 1.0.

From: Samuel Le Berrigaud <samuel.lb_at_gmail.com>
Date: Tue, 10 Mar 2009 10:58:13 +1100

So I want to inject my own @Context object.

I have the the interface of the type to be injected and the associated
resolver. Something like:

public interface MyContext
{
}

@Provider
public class MyContextResolver implements ContextResolver<MyContext>
{
  public MyContext getContext(Class type)
   {
       return MyContext.class.equals(type) ? new MyContext() {} : null;
   }
}

Unfortunately "somewhere" this doesn't work for me. And I think I have
located the somewhere. The ContextResolver does get picked up
correctly and gets registered as an InjectableProvider<Context, Type>.
I believe there could be an issue in the following piece of code,
found at lines 134 to 151 of the
com.sun.jersey.core.spi.factory.ContextResolverFactory:

public Injectable getInjectable(ComponentContext ic, Context ac, Type c) {
               if (!(c instanceof ParameterizedType))
                   return null;
               ParameterizedType pType = (ParameterizedType)c;
               if (pType.getRawType() != ContextResolver.class)
                   return null;
               Type type = pType.getActualTypeArguments()[0];
               // TODO check if concrete type

               final ContextResolver cr = getResolver(ic, type);
               if (cr == null) return null;

               return new Injectable() {
                   public Object getValue() {
                       return cr;
                   }
               };
           }

Here the "Type c" parameter is assumed to be of type ContextResolver.
But what I get when debugging is different. The Type is the actual
type of object to be injected, so in my case not MyContextResolver but
MyContext. I believe this is correct according to the javadocs. This
would mean that this implementation doesn't do at all what it is
supposed to be doing. Moreover the getInjectable should return
MyContext not a ContextResolver AFAIK.

So currently the code doesn't get passed the first if statement for
me, as MyContext is not a ParameterizedType.
Later in this method, if I change the type in this method call
"getResolver(ic, type)" to MyContextResolver I can access the
correctly registered MyContextResolver and from there access
MyContext.

So is that a bug? Am I missing something obvious? Anyone has had the
same issue? I could find anything in bugzilla…

Hope this all makes sense,
Any help appreciated,
SaM