I have an abstract class called Data, with a getInstance() method which should return instances of concrete subclasses of Data.
I want to pass a String to the getInstance method, and this string will define exactly what class will be returned.
So far I have:
public abstract class Data {
private Map<String, Data> instances;
public <T extends Data> T getInstance(Class<T> type, String dataName) {
return type.cast(instances.get(dataName));
}
}
Where the getInstance() method looks up the correct instance in the instances map. I think this should be OK provided the map is populated (by Spring), but the caller must match the Class<T> type parameter with the String dataName parameter.
Is there any way I can remove the Class<T> type parameter and not have generics warnings?