maven 中 javac 编译顺序错误的解决方法 [英] Workaround for javac compilation order bug in maven

查看:26
本文介绍了maven 中 javac 编译顺序错误的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 编译器中遇到了一个错误,提交编译的文件顺序可能会导致代码无法编译.我已经深入研究了代码以隔离我可以重现问题的最少量代码,从而产生 三个源文件(每个班级).

I'm encountering a bug in the Java compiler where the order of files submitted for compilation can cause code not to compile. I've drilled down the code to isolate the smallest amount of code I could to reproduce the problem, resulting in three source files (1 class each).

public interface ActionSpec {
    public abstract int run(String param);
}


public enum Actions implements ActionSpec {
    SKIP {
        public int run(String d) {
            return 0;
        }
    };
}

public class Program {

    public static void main(String[] args) {
        Actions.SKIP.run("hello");
    }
}

该问题可以通过以特定顺序使用 javac 参数来重现.简而言之,为了成功,Actions 类必须始终在使用它的 Program 类之前编译,否则 javac 无法以理智的方式处理它:

The problem is reproducible by having javac arguments in a particular order. In short, in order to succeed, the Actions class must always be compiled before the Program class which uses it, otherwise javac just fails to deal with it in a sane way:

# this case fails
echo "Trying order: javac Program.java Actions.java ActionSpec.java"
rm *class
javac -verbose Program.java Actions.java ActionSpec.java

# this case fails
#rm *class
#javac Program.java Actions.java ActionSpec.java

# this case fails
#rm *class
#javac ActionSpec.java Program.java Actions.java

# this case succeeds
#rm *class
#javac ActionSpec.java Actions.java Program.java

# this case succeeds
#rm *class
#javac Actions.java ActionSpec.java Program.java

# this case succeeds
#rm *class
#javac Actions.java Program.java ActionSpec.java

编译错误,当它发生时,总是相同的 - 无法找到 Actions 枚举实例上的 run 方法,即使它们都实现了一个具有该 run 方法的接口.

The compilation error, when it occurs, is always the same - the run method on the Actions enum instances cannot be found, even though they all implement an interface that has that run method.

Program.java:6: cannot find symbol
symbol  : method run(java.lang.String)
location: class problem.Actions
        Actions.SKIP.run("hello");

该错误似乎与 Oracle 网站上报告的这一错误有关.我在 mac os x 10.7.2 x86_64 上使用 javac 1.6.0_29,但也在 Linux 上复制了它.

The bug seems related to this one reported on Oracle's site. I'm using javac 1.6.0_29, on mac os x 10.7.2 x86_64, but have also reproduced it on Linux.

这个问题在我使用 Maven 构建时变得很明显,并且似乎对编译顺序没有任何控制.所以我正在寻找一种解决方法,要么强制 maven 以这样的顺序编译文件,以避免这个编译器错误,要么摆弄编译器标志(或类似的东西)来避免它.问题出现在工作站和持续集成环境中,因此它必须全面工作.有什么建议吗?

This problem became apparent as I am using Maven to build, and don't appear to have any control over the order of compilation. So I am looking for a workaround to either force maven to compile files in such an order as to avoid this compiler bug, or fiddle with the compiler flags (or something like it) to avoid it. The problem crops up on workstations and in continuous integration environments alike, so it would have to work across the board. Any suggestions?

刚刚尝试了以下解决方法,尽管只是将有问题的枚举分配给具有它实现的接口类型的变量,但令人惊讶的是会导致错误消失.

Just tried the following workaround, which despite merely assigning the enum in question to a variable with the type of the interface it implements, surprisingly causes the error to disappear.

public class Program {

    public static void main(String[] args) {
        ActionSpec a = Actions.SKIP;
        a.run("hello");
    }
}

仍然对别人的意见感兴趣.

Still interested in others opinions.

推荐答案

我玩了一圈,发现添加了简单的演员表:

I played around, and found that adding simple cast:

public static void main(String[] args) {
    ((ActionSpec)Actions.SKIP).run("hello");
}

解决了这个问题.将此枚举作为方法参数作为接口传递也可以解决问题

solves this problem. Passing this enum as method parameter as interface would also do the trick

这篇关于maven 中 javac 编译顺序错误的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆