0

现在有一个主要的大脑放屁。

我有这个:

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;

但这是不允许的。有什么想法/建议吗?谢谢。

4

2 回答 2

1

我认为你应该声明

Map<WorkType, Class<? extends JobHandler>> handler

从方法内部的代码instantiateHandler看来,您期望的Class是任何子类型的对象,JobHandler并且您正在尝试实例化JobHandler对象

于 2013-05-28T15:36:48.353 回答
1

我想你想要:

Map<WorkType, Class<? extends JobHandler>> handler
于 2013-05-28T15:37:25.503 回答