现在有一个主要的大脑放屁。
我有这个:
public static final Map<WorkType, Class> handler;
然后
handler = new TreeMap<WorkType, Class>() {{
put(WorkType.SUBMIT, UtilityHandler.class);
}};
然后
JobHandler jobHandler = instantiateHandler(handler.get(work), work, buildingId);
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!! this shows a warning unchecked assignment
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Unchecked assignment: 'java.lang.Class' to 'java.lang.Class<? extends JobHandler>
然后
@SuppressWarnings(value="unchecked")
private static JobHandler instantiateHandler(Class<? extends JobHandler> ref, WorkType type, String id) {
if(ref == null) {
throw new UnsupportedOperationException("This action is not supposed!");
}
try {
Constructor<JobHandler> constructor = (Constructor<JobHandler>) ref.getConstructor(WorkType.class, String.class);
JobHandler handler = constructor.newInstance(type, id);
return handler;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
Utilities.writeLogException(null, e);
}
return null;
}
我想摆脱未经检查的分配。我以为我可以这样做:
public static final Map<WorkType, Class extends JobHandler> handler;
但这是不允许的。有什么想法/建议吗?谢谢。