简体   繁体   English

Eclipse 编译器和 javac 命令之间的行为不一致

[英]Inconsistent behavior between Eclipse compiler and javac command

Recently I'm testing java generic feature.最近我正在测试 java 通用功能。 Here is the testing code:这是测试代码:

package test;

public class GenericAndMethodSignature {

    public static void main(String[] args) {
        (new ClazzAAA()).fooo();
    }

    public abstract static class ClazzAA<T> {

        public final void fooo() {
            System.out.println(this.foo((T) null));
        }

        public abstract String foo(T input);

        public final String foo(Integer input) {
            return "foo";
        }

    }

    public static class ClazzAAA extends ClazzAA<Integer> {
    }
}

If I compile and run it with Eclipse, console will show:如果我用 Eclipse 编译并运行它,控制台将显示:

Exception in thread "main" java.lang.AbstractMethodError: test.GenericAndMethodSignature$ClazzAA.foo(Ljava/lang/Object;)Ljava/lang/String;
    at test.GenericAndMethodSignature$ClazzAA.fooo(GenericAndMethodSignature.java:12)
    at test.GenericAndMethodSignature.main(GenericAndMethodSignature.java:6)

However, if I compile it with javac command:但是,如果我用 javac 命令编译它:

javac test/GenericAndMethodSignature.java

and run it with command并使用命令运行它

java test.GenericAndMethodSignature

The terminal will show "foo" successfully.终端将成功显示“foo”。

Also, an interesting thing, if I run the class compiled by eclipse with java command, I will get java.lang.AbstractMethodError too.另外,有趣的是,如果我用java命令运行由 eclipse 编译的 class,我也会得到 java.lang.AbstractMethodError。

I use java byte code editor to check those two class, and find ClazzAAA compiled by javac overrides the generic method while class compiled by eclipse not.我用java字节码编辑器查看了这两个class,发现javac编译的ClazzAAA覆盖了泛型方法,而eclipse编译的class没有。

Does anyone know why the behavior of these two compiler is different?有谁知道为什么这两个编译器的行为不同?

Not sure which result is correct.不确定哪个结果是正确的。

This is happening because Eclipse does not internally use javac: they have their own java compiler implementation built into Eclipse.发生这种情况是因为 Eclipse 没有在内部使用 javac:它们在 Eclipse 中内置了自己的 java 编译器实现。

You can read about it here: CodeJava - Why does Eclipse use its own Java compiler?您可以在这里阅读: CodeJava - 为什么 Eclipse 使用自己的 Java 编译器?

Obviously, as you have discovered by yourself, the Java compiler that is built into Eclipse does not work in exactly the same way as javac , hence, there will be some discrepancies.显然,正如您自己发现的那样,Eclipse 中内置的 Java 编译器与javac的工作方式并不完全相同,因此,会有一些差异。

That was the answer to your question.那就是你问题的答案。 What follows from here on is opinions.接下来是意见。

Perhaps it made sense for eclipse to contain its own Java compiler back in the days of Java 1.3, or at any rate back in the days before Java started to follow a regular two-releases-per-year schedule.也许 eclipse 在 Java 1.3 时代包含自己的 Java 编译器是有意义的,或者至少在 Java 开始遵循每年两次发布的常规时间表之前的日子。 Nowadays, following up with new developments of javac is an untenable proposition.现在,跟进javac的新发展是一个站不住脚的命题。

Not only Eclipse is 30 years old, but in many ways it is stuck 30 years in the past. Eclipse 不仅是 30 岁,而且在很多方面都停留在过去 30 年。

I know you did not ask for it, but here is my recommendation on how you could avoid this problem: Switch to a decent IDE. That would be IntelliJ IDEA.我知道你没有要求它,但这里是我关于如何避免这个问题的建议:切换到一个像样的 IDE。那将是 IntelliJ IDEA。 IntelliJ IDEA uses Javac. IntelliJ IDEA 使用 Javac。 They only use their own built-in parser for syntax highlighting and error underlining, so in the rare event that there is a discrepancy between their own parser and javac it will only affect some visual aspects of editing, it will never affect the actual code that gets generated.他们只使用他们自己的内置解析器来进行语法高亮和错误下划线,所以在极少数情况下,他们自己的解析器和javac之间存在差异,只会影响编辑的一些视觉方面,它永远不会影响实际代码生成。

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

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