Guice 2.0 has some improvements in this area, for example TypeResolver:
http://publicobject.com/2008/07/typeresolver-tells-you-what-listget.html
It seems to be possible to solve this problem properly, so I suggest you
open an issue in the tracker for your problem to make sure it gets
addressed.
Now, for a temporary solution, your intermediate class could actually be a
ProcessorProvider, which feels a little bit more natural. Or if you're
willing to use a Guice 2.0 snapshot, I highly recommend provider methods:
static class MyModule implements Module {
public void configure(final Binder binder) {
binder.bind(new
TypeLiteral<Subprocessor<String>>(){}).to(StringConcater.class);
}
@Provides
public Processor<String> getProcessor(Subprocessor<String> subProcessor)
{
return new ProcessorImpl<String>(subProcessor);
}
}
Hope this helps,
Robbie
On Mon, Oct 20, 2008 at 10:51 PM, Trent Bowman <[EMAIL PROTECTED]>wrote:
>
> 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
-~----------~----~----~----~------~----~------~--~---