I am having an issue initializing a class with type parameter. It seems to be a shortcoming of Java's type inference and I would like to know if there's a way around this or a better way of achieving this.
public class ParentModel {}
public class ChildModel extends ParentModel {}
public class Service<E extends ParentModel, T extends Collection<E>> {
private Class<T> classOfT;
private Class<E> classOfE;
public Service(Class<E> classOfE, Class<T> classOfT) {
this.classOfE = classOfE;
this.classOfT = classOfT;
}
}
public class BusinessLogic {
public void someLogic() {
Service<ChildModel, ArrayList<ChildModel>> service = new
Service<ChildModel, ArrayList<ChildModel>>(ChildModel.class, ArrayList.class);
}
}
Compile-time error is in BusinessLogic::someLogic()
:
The constructor Service<ChildModel, ArrayList<ChildModel>>(Class<ChildModel>, Class<ArrayList>) is undefined
Compiled to Java 7.
Class
fields or fields of whatever typeT
andE
are? – Troll