Create a ParameterizeType parameter for your type:
// It supposed to be internal. // You could use sun.reflect.generics.reflectiveObjects but it is not portable. // Or you can implement it yourself (see below) ParameterizedType type = new com.google.inject.internal.MoreTypes.ParameterizedTypeImpl(null, MyClass.class, a, b);
Create TypeLiteral from it:
TypeLiteral typeLiteral = TypeLiteral.get(type);
Now create the entered instance:
return (MyClass<A,B>) injector.getInstance(Key.get(typeLiteral))
In practice, you yourself will want to implement ParameteriedType:
final Type[] types = {a, b}; ParameterizedType type = ParameterizedType() { @Override public Type[] getActualTypeArguments() { return types; } @Override public Type getOwnerType() { return null; } @Override public Type getRawType() { return MyClass.class; }; }
EDIT: In fact, you can use:
Types.newParameterizedType(MyClass.class,a,b)
see Guice module with type parameters
source share