You'll need to create the RepositoryFactory
instance by passing in the Class
object for the generic type.
public class RepositoryFactory<T> {
private Class<T> type;
public RepositoryFactory(Class<T> type) {
this.type = type;
}
public T getRepository(){
if(type.isAssignableFrom(IQuestionRepository.class)){ //or type.equals(...) for more restrictive
return new QuestionRepository();
}
...
}
Otherwise, at runtime, you cannot know the value of the type variable T
.