I have a class
with a generic parameter T
say Action
. Every Action
has an ActionType
, given in the constructor. For example, it could be GET_STRING
. Depending on the ActionType
, I would like Java to detect the Action
's generic type T
.
Example:
public enum ActionType {
GET_STRING(String.class), GET_BOOLEAN(Boolean.class);
private ActionType(Class clazz) { this.clazz = clazz}
private Class clazz;
Class getType() { return clazz; }
}
Now I want Action
to automatically have the type its ActionType#getType()
returns.
public class Action<T> {
public Action(ActionType type) {
// magic
}
public T doAction() { ... }
}
String s = new Action(ActionType.GET_STRING).doAction();
Is that possible? Maybe in a way that the enum
implements an interface
or something similar?
Thanks for your help in advance!!