I am trying to do something like this:
1. Approach
Type ty = entityType.getEntityClass(); // could be e.g. String.class
List<ty> list;
2. Approach
Class cl = entityType.getEntityClass(); // could be e.g. String.class
List<cl> list;
However, compiler simply says "no".
Is there any way to set the generic type <T> of e.g. a java.util.List dynamic only by having e.g. String.class or foo.getClass()?
...
What I am actually trying to do is initializing a List by a given parameter of type EntityTypeEnum or it's Class property value.
public enum EntityTypeEnum {
ARTICLE(Article.class),
ORDER(OrderHeader.class),
CUSTOMER(Customer.class),
CUSTOMER_SESSION(CustomerSession.class);
private final Class entityClass;
private EntityTypeEnum(final Class entityClass) {
this.entityClass = entityClass;
}
public Class getEntityClass() {
return entityClass;
}
}
Constructive criticism is welcome. Thank you!
Add: (as a response of Andy's comment)
Class cl = String.class;
List<cl> list;
..is not working either!
List<String> list;
..works, of course.
So what is this difference between String and String.class?
Or is there a way to set the value like:
public enum EntityTypeEnum {
ARTICLE(Article)
..