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");
}
}
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
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");
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.
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?
EDIT: 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
It's the bug reported in http://bugs.sun.com/view_bug.do?bug_id=6724345
The workaround suggested should work if you are still using Java 6 compiler. The bug is fixed in Java 7.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With