I have a few types that are like this
// a value that is aware of its key type (K)
Bar
// something that deals with such values and keys
Foo
From the JavaDoc for Binder:
Guice cannot currently bind or inject a generic type, such as
Setall type parameters must be fully specified.
You can create bindings for Foo when K and V are bound.
If you need to make bindings for Foo for more than one type of key, you can make a method that makes it easier to do these bindings. One way to do that is to create a method like this in your module:
> AnnotatedBindingBuilder> bind(Class keyType,
Class barType) {
ParameterizedType bType = Types.newParameterizedType(Bar.class, keyType);
ParameterizedType fType = Types.newParameterizedType(Foo.class, barType,
keyType);
@SuppressWarnings("unchecked")
TypeLiteral> typeLiteral =
(TypeLiteral>) TypeLiteral.get(fType);
return bind(typeLiteral);
}
Then if you have these classes:
class StringValue implements Bar {
...
}
class StringValueProcessor implements Foo {
...
}
You can create a binding like this:
bind(String.class, StringValue.class).to(StringValueProcessor.class);
...so that Guice could inject into a class like this:
static class Target {
private final Foo foo;
@Inject
public Target(Foo foo) {
this.foo = foo;
}
}