I have generic abstract superclass GenericModel with method that I would like to call in children classes as follow:
public abstract class GenericModel<T extends GenericModel> {
@Transient protected Class<T> entityClass;
public GenericModel() {
Class obtainedClass = getClass();
Type genericSuperclass = null;
for(;;) {
genericSuperclass = obtainedClass.getGenericSuperclass();
if(genericSuperclass instanceof ParameterizedType) {
break;
}
obtainedClass = obtainedClass.getSuperclass();
}
ParameterizedType genericSuperclass_ = (ParameterizedType) genericSuperclass;
entityClass = ((Class) ((Class) genericSuperclass_.getActualTypeArguments()[0]));
// some code
public List<T> getByCreationUser_Id(Long id) {
// method body
return entities;
}
}
I am extending class this class with:
public class Insurance extends GenericModel<Insurance> {
// some code
}
And I wanna extend class: Insurance with this class as follow:
public class Liability extends Insurance {
}
and object of Liability class I would like call method:
getByCreationUser_Id() and instead of list of T I would like to get List<Liability> as follow:
List<Liability> currentUserInsurances = new Liability().getByCreationUser_Id(1L);
Unfortunatelly I am getting an error:
Type mismatch: cannot convert from List<Insurance> to List<Liability>
When I will call same method on Insurance class it works, I am getting an:
List<Insurance>
I've already tried declare classes as follow:
public class Insurance<T> extends GenericModel<Insurance<T>> {}
public class Liability extends Insurance<Liability> {}
But id doesn't work. Compile errors occur.
Please help.