我有一个方法:
public Object instantiateAlmostAnyType(String stringToParse, Class<?> targetType) {
...
if (targetType.isEnum())
return Enum.valueOf((Class)targetType, stringToParse);
}
它工作,但我有编译器警告 . 全部为 return
行:
Class is a raw type. References to generic type Class should be parameterized
Enum is a raw type. References to generic type Enum should be parameterized
Type safety: The expression of type Class needs unchecked conversion to conform to Class
Type safety: Unchecked invocation valueOf(Class, String) of the generic method valueOf(Class, String) of type Enum
是否有任何方法(@suppressWarnings除外)以避免警告?任何演员?
2 回答
现在,您在编译时检查
Class
实例必须是enum
类型,并且不必执行任何不安全的转换 .到目前为止我还没有找到解决方案所以我认为这是不可能的 . 如果有人给我答案,我会接受