There are two entities classes:
public class Base {
private String provider;
}
public class SuperClass extends Base {
private String trunk;
}
and main class:
public class Test {
public static <T extends Base> List<T> getDetailedReport(Class<T> clazz, String providerName) {
List<T> list = new ArrayList<>();
T entity = new Base(); // this is wrong, of course.
return list;
}
public static void main(String[] args) {
Base base = new Base();
SuperClass superClass = new SuperClass();
List<Base> baseList = getDetailedReport(Base.class, "Telia");
List<SuperClass> superClassList = getDetailedReport(SuperClass.class, "Globalcom");
}
}
I'm trying to create generic method getDetailedReport which returns list of entities. Type of entity is passed through clazz variable.
My question is: How to create entity of necessary type?
newInstance()method onClassor one of the constructors. However, you seem to need parameters (like provider for both and trunk forSuperClass) so some kind of factory might be better suited here.