Reputation: 65
Context : java using guice (last version)
Hi everybody, is it possible to inject some TypeLiteral with Guice by this way :
public MyClass<?,?> getMyClass(Injector injector, Class<?> a, Class<?> b)
{
//how to Inject MyClass with type a & b ?
//e.g : injector.getInstance(MyClass<"a class","b class">)
}
public interface MyClass<S,T>
{
public T do(S s);
}
public class ClassOne implements MyClass<String,Integer>
{
public Integer do(String s)
{
//do something
}
}
Module :
bind.(new TypeLiteral<MyClass<String,Integer>(){}).to(ClassOne.class);
bind.(new TypeLiteral<MyClass<String,Double>(){}).to(ClassTwo.class);
...
What is the best way to handle this problem (with Guice)?
Thank you !
Upvotes: 6
Views: 677
Reputation: 9315
Create a ParameterizeType for your type :
// It's 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 a TypeLiteral from it:
TypeLiteral typeLiteral = TypeLiteral.get(type);
Now create injected instance:
return (MyClass<A,B>) injector.getInstance(Key.get(typeLiteral))
In practice you want to implement the ParameteriedType yourself:
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
Upvotes: 3
Reputation: 68847
In fact, I think it isn't possible. Generics are not reified at run-time. This means the information is not present at run-time. So it doesn't care what generic type you give it.
So, I think this method is not needed. Simply something like this:
public MyClass<?,?> getMyClass(Injector injector, Class<?> a, Class<?> b)
{
return new MyClass();
// Or maybe something like this, if you use custom constructors
// return injector.getInstance();
}
Since MyClass
is an interface, what you want to achieve is totally useless and impossible. How do you want to instantiate an object with a specific behavior when you only specify the param type and return type.
I think you should try to find a different way of working.
Upvotes: 0