【发布时间】:2011-12-13 04:19:42
【问题描述】:
我在包 A 中有一个枚举,它有一个静态转换器方法,我用它来为另一个包 B 中的一组特定类获取适当的枚举类型(另一个包中的每个类都有一个对应的枚举)。
现在,从第三个包 C,它不导入包 A,但导入包 B,我正在尝试打开枚举。
现在,当我使用 switch 检查枚举时,它会抱怨:
nz.ac.waikato.jstar.jStar.OrderOperator 类型无法解析。它是从必需的 .class 中间接引用的 文件
但是,当我使用 if 语句时,它不会。
谁能解释为什么switch 会导致错误而if 不会?
代码:
//Doesn't work
switch (connection.getType()) {
case LESS_THAN:
break;
case LESS_THAN_EQUALS:
break;
default:
break;
}
//Works Fine
if(connection.getType() == OrderingConnectionType.LESS_THAN) {
return -1;
} else if(connection.getType() == OrderingConnectionType.LESS_THAN_EQUALS) {
return 2;
}
引用bundle A的Enum的静态方法(OrderOperator来自Bundle A):
public static OrderingConnectionType getType(Class<? extends OrderOperator> operatorType) {
for (OrderingConnectionType type : OrderingConnectionType.values()) {
if(type.operatorType.isAssignableFrom(operatorType)) return type;
}
throw new IllegalArgumentException("Unknown Operator type: " + operatorType);
}
【问题讨论】:
标签: java eclipse if-statement osgi switch-statement