为什么 Eclipse 可以编译这个,而 javac 却不能?

发布于 2024-07-29 04:27:32 字数 1201 浏览 2 评论 0原文

我们有一些单元测试可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朱染 2024-08-05 04:27:33

在 BaseMatcher 中,您需要指定类型参数:

public SubMatcher with(Matcher<?, ?> matcher) {

为了允许 javac 匹配您的 with 方法

PS

恕我直言,这是 eclipse 编译器的一个错误

in BaseMatcher you need to specify type parameters:

public SubMatcher with(Matcher<?, ?> matcher) {

in order to allow javac to match your with method

PS

imho is a bug of eclipse compiler

撩起发的微风 2024-08-05 04:27:33

我通过将 添加到 SubMatcher.with 中的 Matcher 成功构建了它:

class SubMatcher extends BaseMatcher {
    @Override
    public SubMatcher with(Matcher<?,?> matcher) {
        return this;
    }
}

没有这个,方法签名是不同的从基地。 我想知道 @Override 检查中是否存在错误而未能注意到这一点。

I made it build successfully by adding <?,?> to Matcher in SubMatcher.with:

class SubMatcher extends BaseMatcher {
    @Override
    public SubMatcher with(Matcher<?,?> matcher) {
        return this;
    }
}

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.

痴梦一场 2024-08-05 04:27:33

检查您在 Eclipse 和终端上编译的 jre 或 jdk。 也许这可能是版本问题。

Check which jre or jdk you are compiling with on both Eclipse and terminal. Maybe it might be the version issue.

我的鱼塘能养鲲 2024-08-05 04:27:33

对我有用:

$ java -version
openjdk version "1.7.0-internal"
OpenJDK Runtime Environment (build 1.7.0-internal-****-2009_07_23_10_21-b00)
OpenJDK 64-Bit Server VM (build 16.0-b06, mixed mode)
$ javac -XDrawDiagnostics Test.java 
$

我隐约记得这样一个错误报告,但现在无法给您链接。

Works for me:

$ java -version
openjdk version "1.7.0-internal"
OpenJDK Runtime Environment (build 1.7.0-internal-****-2009_07_23_10_21-b00)
OpenJDK 64-Bit Server VM (build 16.0-b06, mixed mode)
$ javac -XDrawDiagnostics Test.java 
$

I vaguely remember such a bugreport, but cannot give you a link to it right now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文