The problem is T. You add a constraint to your method, saying that T should extend HashMap<?, M> . However, the way you later reference T is similar to a generic parameter of type Class (Class<T>) . LinkedHashMap.class is of type Class<LinkedHashMap> not Class<LinkedHashmap<?, Entity>> (this is what you need)
A class object always refers to a non-parameterized type, and that makes sense. Since a common binding exists at compile time, and you are going to use this class to dynamically reflect the state and behavior of an instance at runtime. In short, you can use Class<HashMap> to create a new instance, not limited to any type.
So, I think what you need to do for your code is to change this restriction the way it looks:
public static <T extends HashMap, M extends Entity> EntityCollection<M> getInstance( Class<T> mapType, Class<M> entityType) throws ReflectiveOperationException { T map = mapType.getConstructor().newInstance(); return new EntityCollection<M>(map); }
Claudio
source share