Note that there are some explanatory texts on larger screens.

plurals
  1. POjava project won't compile class referencing enum depending on source code order
    primarykey
    data
    text
    <p>I have an situation where some Java 1.6 sources won't compile in Maven and only compile in javac when I specify sources in a certain order.</p> <p>pom.xml is bare-bones but specifies <code>-source</code> and <code>-target</code> of 1.6:</p> <pre><code> &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &lt;version&gt;3.1&lt;/version&gt; &lt;configuration&gt; &lt;source&gt;1.6&lt;/source&gt; &lt;target&gt;1.6&lt;/target&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; </code></pre> <p>ComputeOperation.java</p> <pre class="lang-java prettyprint-override"><code>package test.domain; public interface ComputeOperation&lt;T&gt; { public T compute(T arg1, T arg2); } </code></pre> <p>Computations.java</p> <pre class="lang-java prettyprint-override"><code> package test.domain; public enum Computations implements ComputeOperation&lt;Integer&gt; { ADD { @Override public Integer compute(Integer arg1, Integer arg2) { return arg1 + arg2; } }, SUBTRACT { @Override public Integer compute(Integer arg1, Integer arg2) { return arg1 - arg2; } } } </code></pre> <p>App.java</p> <pre class="lang-java prettyprint-override"><code>package test.app; import test.domain.*; public class App { public static void main( String[] args ) { System.out.println(Computations.ADD.compute(1, 1)); } } </code></pre> <p>Maven build fails as follows (<a href="https://gist.github.com/noahlz/5645995" rel="nofollow noreferrer">full debug log here</a>):</p> <pre><code>[ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /home/nzucker/projects/maven-test/src/main/java/test/app/App.java:[7,44] cannot find symbol symbol : method compute(int,int) location: class test.domain.Computations [INFO] 1 error </code></pre> <p>Plain old <code>javac</code> compilation fails succeeds if App.java comes before Computations.java:</p> <pre><code>[nzucker:maven-test]$ javac -source 1.6 -target 1.6 -g -d target/classes src/main/java/test/app/App.java src/main/java/test/domain/Computations.java src/main/java/test/domain/ComputeOperation.java src/main/java/test/app/App.java:7: cannot find symbol symbol : method compute(int,int) location: class test.domain.Computations System.out.println(Computations.ADD.compute(1, 1)); ^ 1 error </code></pre> <p>But the build <em>succeeds</em> if I put App.java last in the input sources:</p> <pre><code>[nzucker:maven-test]$ javac -source 1.6 -target 1.6 -g -d target/classes src/main/java/test/domain/Computations.java src/main/java/test/domain/ComputeOperation.java src/main/java/test/app/App.java [nzucker:maven-test]$ java -cp target/classes/ test.app.App 2 </code></pre> <p>Finally, if I change the <code>import</code> in App.java to a static import, compilation succeeds:</p> <pre><code>package test.app; import static test.domain.Computations.*; public class App { public static void main( String[] args ) { System.out.println(ADD.compute(1, 1)); } } </code></pre> <p>Questions:</p> <p>I am aware of "<a href="https://stackoverflow.com/questions/9505857/workaround-for-javac-compilation-order-bug-in-maven">Workaround for javac compilation order bug in maven</a>" - <strike>so my question is <em>why</em> is this occurring? Is this a true defect in Maven? If so, what is the JIRA issue if so?</p> <p>Also, why does compilation fail? I thought that javac resolve dependencies and compilation order between classes during a pre-processing phase? I guess I was wrong? Is there a specification that covers this?</strike></p> <p>Ok, the other question I linked to has a response that points to a bug in Java 1.6. The answer is to upgrade to Java 1.7 or implement a work-around.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload