在我的IF中,所有3行都有警告。如何在没有压力的情况下将其移除?有更好的解决方案吗?
public void template() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
if(templateResult == null){
Class templateClass = getGenericTypeArgument(this.getClass(), 0);
Constructor<? extends Template> constructor = templateClass.getConstructor(new Class[]{List.class, List.class});
templateResult = (T) constructor.newInstance(listData, listaDataRes);
}
}
答案 0 :(得分:2)
您可以不使用raw types
删除部分内容Class<?> templateClass = getGenericTypeArgument(this.getClass(), 0);
// ^
Constructor<? extends Template> constructor =
templateClass.getConstructor(new Class<?>[]{List.class, List.class});
// ^
我不认为你可以避免未经检查的强制转换为T
。
有更好的解决方案吗?
在我看来,您可能正在使用getGenericSuperclass().getActualTypeArguments()
内容,并且可以通过将Class<T>
作为参数传递给对象(shown here)来删除所有警告。