I am using a Map
to store references to a class of a generic type parameter and an implementation object with the same generic type parameter, more specifically:
public class SomeImpl<T extends BaseClass> {
void execute(T instance);
}
Now, in another class, i want to store references to implementations based on their generic type parameter.
private Map<Class<BaseClass>, SomeImpl<BaseClass>> map;
However, i find it somewhat confusing to operate with the generics here, especially since a lot of unsafe cast warnings are shown.
private <T extends BaseClass> void register(SomeImpl<T> impl, Class<T> aClass) {
// unsafe
Class<BaseClass> configClass = (Class<BaseClass>) aClass;
// unsafe
map.put((SomeImpl<BaseClass>) impl, configClass);
}
and later when i want to retrieve some instance
private SomeImpl<BaseClass> get(Class<? extends BaseClass> aClass) {
// unsafe
return (SomeImpl<BaseClass>) map.get(aClass);
}
Could someone clarify if the above statements are safe, thus the warnings can safely be ignored?