users@jersey.java.net

[Jersey] Re: Injection of generics

From: Fabrizio Cucci <fabrizio.cucci_at_gmail.com>
Date: Thu, 17 Mar 2016 10:43:25 +0000

Hi Trenton,

please find below an example of Factory for a generic type:

package hk2;

import java.util.ArrayList;
import java.util.List;

import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.glassfish.hk2.utilities.binding.AbstractBinder;

public class Test {

public static class ListOfStringFactory implements Factory<List<String>> {

@Override
public List<String> provide() {
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("Factory");
return list;
}

@Override
public void dispose(List<String> arg0) {

}

}

public static void main(String[] args) {

AbstractBinder binder = new AbstractBinder() {
@Override
protected void configure() {
bindFactory(ListOfStringFactory.class).to(new TypeLiteral<List<String>>()
{});
}
};

ServiceLocator serviceLocator = ServiceLocatorUtilities.bind(binder);

List<String> list = serviceLocator.getService(new
TypeLiteral<List<String>>(){}.getType());
System.out.println(list);

}

}

Here I'm using HK2 outside Jersey but I think the important part for you is
in the binder.

I hope that helped,
Fabrizio


On 17 March 2016 at 06:10, Trenton D. Adams <trenton.d.adams_at_gmail.com>
wrote:

> Good day,
>
> I'd like to inject his...
>
> @Inject
> protected ParameterHandler<SampleParameters> pageParameters;
>
> With a factory like so...
>
> public class ParameterHandlerFactory
> implements Factory<ParameterHandler>
> {
>
> private final HttpServletRequest request;
>
> @Inject
> public ParameterHandlerFactory(final HttpServletRequest request)
> {
> this.request = request;
> }
>
> @Override
> public ParameterHandler provide()
> {
> return new ParameterHandler(request,
> SampleParameters.class);
> }
>
> @Override
> public void dispose(
> final ParameterHandler parameterEnumParameterHandler)
> {
>
> }
>
> }
>
> And it's registered like so...
>
> register(new AbstractBinder()
> {
> @Override
> protected void configure()
> {
> bindFactory(ParameterHandlerFactory.class).to(
> ParameterHandler.class);
> }
> });
>
>
> And I did in fact originally have the Factory taking the same generic, but
> that didn't work either. The errors are below, with most of the actual
> stack trace removed for brevity.
>
> But it's not working....
> MultiException stack 1 of 3
> org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object
> available for injection at
> SystemInjecteeImpl(requiredType=ParameterHandler<SampleParameters>,parent=ParametersSample,qualifiers={},position=-1,optional=false,self
> =false,unqualified=null,2004350963)
>
> MultiException stack 2 of 3
> java.lang.IllegalArgumentException: While attempting to resolve the
> dependencies of com.example.ParametersSample errors were found
>
> MultiException stack 3 of 3
> java.lang.IllegalStateException: Unable to perform operation: resolve on
> com.example.ParametersSample
>
> As soon as I remove the generic part of it, it works great.
>
> I'm new to generics, so this exercise may be pointless, in that I'm not
> sure how to get the class of a generic so that I don't have to hardcode
> that "SampleParameters" class into the factory. I'm mainly curious if
> there's something obvious to someone that has been using generics for
> awhile; or maybe HK2 doesn't support this?
>