I have two methods:
public <T extends Component> void addComponent(BluePrint bluePrint, Class<T> type) throws InstantiationException, IllegalAccessException {
AddComponent addComponent = addComponentMap.get(type);
if (addComponent == null) {
addScriptable(bluePrint, type); <--- fails here
}
}
if addComponentMap.get(type); returns null, i know implicitely that T is of type Scriptabe and need to call:
private <T extends Scriptable> void addScriptable(BluePrint bluePrint, Class<T> type) throws InstantiationException, IllegalAccessException {
scriptableSystems.add(new ScriptableSystem<T>());
}
The issue is that the upper bound for T in the second method is Scriptable and in the first method its Component, therefore type "could" potentially be any component when addComponent is null.
Can i somehow narrow the constraint to Scritpable when addComponent is null? Or somehow explicitly say that when addComponent is null T will extend Scriptable, before calling addScriptable?
Worth mentioning perhaps is that Scriptable inherits from component.
Scriptable? In that case, makeT extends Scriptablein the first one, too. Otherwise, you could just cast if you definitely know. If you cannot guarantee that all components in the map areScriptable, I'd say you are out of luck using only generics.Class.isAssignableFrom()could help