我有一个方法:
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除外)以避免警告?任何演员?
答案 0 :(得分:5)
public <T extends Enum<T>> T instantiate(String stringToParse, Class<T> targetType) {
...
return Enum.valueOf(targetType, stringToParse);
}
现在,您在编译时检查Class
实例必须是enum
类型,并且不必进行任何不安全的转换。
答案 1 :(得分:0)