Is it possible to automate bindings of generic classes? Consider this:
Generic interface:
public interface IAction<T> {
T echo(T inst);
}
Long subtype:
public class LongAction implements IAction<Long> {
@Override
public Long echo(Long inst) {return inst;}
}
String subtype:
public class StringAction implements IAction<String> {
@Override
public String echo(String inst) {return inst;}
}
Custom module: public class CustomModule extends AbstractModule {
@Override
protected void configure() {
//do this automagically?
bind(new TypeLiteral<IAction<String>>() {}).to(StringAction.class);
bind(new TypeLiteral<IAction<Long>>() {}).to(LongAction.class);
//
}
}
Main
// i know i can obtain the correct instance
IAction<String> is = injector.getInstance(new Key<IAction<String>>(){});
Is it possible to auto bind, in some way(eg: base abstract class, reflection or whatever) the binding of the StringAction and LongAction classes? I've tried using reflection to no avail.
LongAction,StringAction, etc? Are they in a particular package or something? What if two classes implementIAction<String>? - Tavian BarnesTfor now, so there won't be twoIAction<String>- Miguel Ping