我仍然遇到java泛型系统中的一些极端情况的问题.
我有这种方法(我只对签名感兴趣):
interface Extractor {
public > RETURN_TYPE extractEnum(final Class enumType);
}
(想想一个接口,其实现有时会提取一个EnumSet,有时一个实现提取一个JComboBox等)
我想用在运行时获得的类调用它,所以我只是这样调用它:
public static RETURN_TYPE extractField(final Extractor extractor, final Field field) {
final Class> type = field.getType();
if (type.isEnum())
return extractor.extractEnum(/* error here*/type.asSubclass(Enum.class));
throw new RuntimeException("the rest of the visitor is not necessary here");
}
我收到一条奇怪的错误信息:
????不兼容的类型
????发现:java.lang.Object
????必需:RETURN_TYPE
消息的位置,如果恰好在调用的开头braket之后,在类型的“t”之前.
如果我从非通用上下文中调用它,它可以工作:
Integer extractField(final Extractor extractor, final Field field) {
final Class> type = field.getType();
if (type.isEnum())
return extractor.extractEnum(type.asSubclass(Enum.class));
throw new RuntimeException("the rest of the visitor is not necessary here");
}
有没有人对这个问题有解释和解决方案好吗?
这是一个完整的文件,供想要玩它的人使用:
public class Blah {
interface Extractor {
public > RETURN_TYPE extractEnum(final Class enumType);
}
public static RETURN_TYPE extractField(final Extractor extractor, final Field field) {
final Class> type = field.getType();
if (type.isEnum())
return extractor.extractEnum(/* error here*/type.asSubclass(Enum.class));
throw new RuntimeException("the rest of the visitor is not necessary here");
}
public static Integer extractField(final Extractor extractor, final Field field) {
final Class> type = field.getType();
if (type.isEnum())
return extractor.extractEnum(type.asSubclass(Enum.class));
throw new RuntimeException("the rest of the visitor is not necessary here");
}
}
提前致谢,
尼科