Maybe this has been answered before, but I wasn't able to find it in
the archives. Or, maybe it is in the Guice book, but mine is still in
the ordering process...

Here's the class I want to inject dependencies into:

public class ProcessorImpl<T> implements Processor<T>
{
    private final Subprocessor<T> subprocessor;

    @Inject
    public ProcessorImpl(Subprocessor<T> subprocessor)
    {
        super();
        this.subprocessor = subprocessor;
    }

    public T process(List<T> list)
    {
...
    }
}

The constructor wants a Subprocessor<T>. However, I cannot figure out
how (or if it is possible) to bind a parameterized type. The best I've
come up with is this module:

    public static class MyModule implements Module
    {
        public void configure(final Binder binder)
        {
            binder.bind(new TypeLiteral<Processor<String>>(){}).to(new
TypeLiteral<ProcessorImpl<String>>(){});
            binder.bind(new TypeLiteral<Subprocessor<String>>()
{}).to(new TypeLiteral<StringConcater>(){});
        }
    }

and then this main() method:

    public static void main(final String[] argv)
    {
        final Injector injector = Guice.createInjector(new
MyModule());
        injector.getInstance(Key.get(new
TypeLiteral<Processor<String>>(){}));
    }

My thoughts here were that, by asking guice for a Processor<String>
(which is bound to ProcessorImpl<String>), the T is now the concrete
type String, so guice should be looking for a binding for
Subprocessor<String>. But, I get this error:

 1) Error at
example07_generics.ProcessorImpl.<init>(ProcessorImpl.java:15):
  Binding to example07_generics.Subprocessor<T> not found. No bindings
to that type were found.

And now I'm stuck, because I can't create a TypeLiteral with
parameterized types, only concrete ones. I can work around the problem
with another Gafter-ish trick and make a little shim class of concrete
type:

public class ProcessorStringImpl extends ProcessorImpl<String>
{
    @Inject
    public ProcessorStringImpl(final Subprocessor<String>
subprocessor)
    {
        super(subprocessor);
    }

}

and then tweaking the module a bit:

    public static class MyModule implements Module
    {
        public void configure(final Binder binder)
        {
            binder.bind(new TypeLiteral<Processor<String>>(){}).to(new
TypeLiteral<ProcessorStringImpl>(){});
            binder.bind(new TypeLiteral<Subprocessor<String>>()
{}).to(new TypeLiteral<StringConcater>(){});
        }
    }

Is this the best way to work around the issue? If so, can it be added
to the recipes page?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to