为什么 Eclipse 可以编译这个,而 javac 却不能?
我们有一些单元测试可以在 Eclipse 3.4 中编译并运行良好,但是当我们尝试使用 javac 编译它们时,它失败了。 我设法将代码缩减为小而独立的代码,因此它没有外部依赖项。 代码本身没有多大意义,因为它完全脱离了上下文,但这并不重要 - 我只需要找出为什么 javac 不喜欢这样:
public class Test {
public void test() {
matchOn(someMatcher().with(anotherMatcher()));
}
void matchOn(SubMatcher matcher) {}
SubMatcher someMatcher() {
return new SubMatcher();
}
Matcher anotherMatcher() {
return null;
}
}
interface Matcher <U, T> {}
class BaseMatcher implements Matcher {
public BaseMatcher with(Matcher<?,?> matcher) {
return this;
}
}
class SubMatcher extends BaseMatcher {
@Override
public SubMatcher with(Matcher matcher) {
return this;
}
}
我尝试过 JDK 1.5.0_10< /code> 和
1.6.0_13
,具有相同的结果:
Test.java:6: matchOn(test.SubMatcher) in test.Test cannot be applied to (test.BaseMatcher)
matchOn(someMatcher().with(anotherMatcher()));
^
1 error
我认为这是完全有效的 Java。 SubMatcher.with() 方法返回比 BaseMatcher.with() 更具体的类型,但编译器似乎认为返回类型是 BaseMatcher。 但是,Eclipse 编译器可能错误地允许了不应该允许的操作。
有任何想法吗?
We have some unit tests which compile and run fine in Eclipse 3.4, but when we try to compile them using javac, it fails. I've managed to cut the code down to something small and self-contained, so it has no external dependencies. The code itself won't make much sense because it's all out of context, but that doesn't matter - I just need to find out why javac doesn't like this:
public class Test {
public void test() {
matchOn(someMatcher().with(anotherMatcher()));
}
void matchOn(SubMatcher matcher) {}
SubMatcher someMatcher() {
return new SubMatcher();
}
Matcher anotherMatcher() {
return null;
}
}
interface Matcher <U, T> {}
class BaseMatcher implements Matcher {
public BaseMatcher with(Matcher<?,?> matcher) {
return this;
}
}
class SubMatcher extends BaseMatcher {
@Override
public SubMatcher with(Matcher matcher) {
return this;
}
}
I've tried with JDK 1.5.0_10
and 1.6.0_13
, with the same result:
Test.java:6: matchOn(test.SubMatcher) in test.Test cannot be applied to (test.BaseMatcher)
matchOn(someMatcher().with(anotherMatcher()));
^
1 error
I think this is perfectly valid Java. The SubMatcher.with() method returns a more specific type than BaseMatcher.with(), but the compiler seems to think that the return type is BaseMatcher. However, it's possible that the Eclipse compiler is incorrectly allowing something it shouldn't be.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 BaseMatcher 中,您需要指定类型参数:
为了允许 javac 匹配您的
with
方法PS
恕我直言,这是 eclipse 编译器的一个错误
in BaseMatcher you need to specify type parameters:
in order to allow javac to match your
with
methodPS
imho is a bug of eclipse compiler
我通过将
添加到
SubMatcher.with
中的Matcher
成功构建了它:没有这个,方法签名是不同的从基地。 我想知道
@Override
检查中是否存在错误而未能注意到这一点。I made it build successfully by adding
<?,?>
toMatcher
inSubMatcher.with
:Without this, the method signature is different from the base. I wonder whether there's a bug in the
@Override
checking that fails to notice this.检查您在 Eclipse 和终端上编译的 jre 或 jdk。 也许这可能是版本问题。
Check which jre or jdk you are compiling with on both Eclipse and terminal. Maybe it might be the version issue.
对我有用:
我隐约记得这样一个错误报告,但现在无法给您链接。
Works for me:
I vaguely remember such a bugreport, but cannot give you a link to it right now.