简体   繁体   English

为什么Eclipse编译这个,但javac不编译?

[英]Why does Eclipse compile this, but javac doesn't?

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. 我们有一些单元测试可以在Eclipse 3.4中编译和运行,但是当我们尝试使用javac编译它们时,它会失败。 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: 代码本身没有多大意义,因为它完全脱离了上下文,但这并不重要 - 我只需要找出为什么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;
    }
}

I've tried with JDK 1.5.0_10 and 1.6.0_13 , with the same result: 我试过JDK 1.5.0_101.6.0_13 ,结果相同:

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. 我认为这是完全有效的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. SubMatcher.with()方法返回一个比BaseMatcher.with()更具体的类型,但编译器似乎认为返回类型是BaseMatcher。 However, it's possible that the Eclipse compiler is incorrectly allowing something it shouldn't be. 但是,Eclipse编译器可能错误地允许它不应该存在的东西。

Any ideas? 有任何想法吗?

in BaseMatcher you need to specify type parameters: 在BaseMatcher中,您需要指定类型参数:

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

in order to allow javac to match your with method 为了让javac与你with方法相匹配

PS PS

imho is a bug of eclipse compiler imho是eclipse编译器的一个bug

I made it build successfully by adding <?,?> to Matcher in SubMatcher.with : 我通过在SubMatcher.withMatcher添加<?,?>来使其成功构建:

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. 我想知道@Override检查中是否有一个错误没有注意到这一点。

Check which jre or jdk you are compiling with on both Eclipse and terminal. 检查您在Eclipse和终端上编译的jre或jdk。 Maybe it might be the version issue. 也许它可能是版本问题。

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. 我依稀记得这样的bug报道,但现在无法给你一个链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM