Is it possible to wrap generic injections in some way?
Given the interface:
interface IFoo<T>
In my guice module:
bind((Key<IFoo<SomeType>) Key.get(Types.newParameterizedType(IFoo.class, SomeType.class))).to(Foo.class);
But before I return the Foo instance I want to wrap it with this:
class FooWrapper<T> implements IFoo<T> { public FooWrapper(Foo<T> foo) { ... } }
Somewhere like this:
return new FooWrapper<T>(foo);
Here’s one way you can do this:
Another thing that might work well would be to add a general annotation like
@Wrappedand then to declareFooWrapper‘s constructor like this:Then in your private module you could bind
Fooannotated with@Wrappedand bind and exposeFooWrappernormally, without needing to use an@Providesmethod.There may well be better ways of doing this I haven’t thought of yet. Do also keep in mind that method interception is often a good way of decorating interface implementations with other behavior as well.