I want to convert with the ModelMapper a List<> to a List<T> knowing the exact java.lang.Class of T. But the ModelMapper keeps only instantiating the common parent class of T...
for your context: I want to convert a list of DTO to a list of Domain, but as I do it in every Service class I extracted it in a generic abstract Service.
public class BaseClass {}
public class SubClass1 extends BaseClass {}
public <T extends BaseClass> List<T> getList(Class<T> specificClass) {
...
Type listType = new TypeToken<List<T>>(){}.getType(); // here problem
List<T> responseList = modelMapper.map(listIAlreadyHave, listType);
return responseList;
}
List<SubClass1> list = doIt(SubClass1.class);
I would like list to be a list of SubClass1 instances, but i get only BaseClass instances.
I don't blame Java for not understading my listType definition because T is unknown on runntime, but how can i use the specificClass parameter i generously provide?
Thank you dear community!