I have a few types that are like this
// a value that is aware of its key type (K)
Bar<K>
// something that deals with such values and keys
Foo<V extends Bar<K>, K>
How would one recreate Foo such that you could consume it in Guice? The bit I'm stuck on is how to cross reference the K from Bar to the 2nd parameterized type of Foo.
So for example,
WildcardType kType = Types.subtypeOf(Object.class);
WildcardType barType =
Types.subtypeOf(Types.newParameterizedType(Bar.class, pipeKey));
ParameterizedType fooType =
Types.newParameterizedType(Foo.class, pipelineableType, pipeKey);
Really this seems wrong as it's basically:
Foo<V extends Bar<? extends Object>, ? extends Object>
Which is not the same thing as:
Foo<V extends Bar<K>, K>
As in the latter case I know that K is a consistent type.
Any ideas?
Cheers
Matt
Guice's factory cannot build
TypeVariable
instances. You'll need to implement this interface directly as you need it.Note that Guice doesn't allow bindings for types that aren't fully-qualified. For example, you can bind a
Map<String, Integer>
but you can't bind aMap<K, V>
.From the JavaDoc for Binder:
You can create bindings for
Foo
whenK
andV
are bound. If you need to make bindings forFoo
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:Then if you have these classes:
You can create a binding like this:
...so that Guice could inject into a class like this: