javascalaantcyclic-reference

Building a project with mixed Scala and Java source files using Ant - illegal cyclic reference error


At the present time I have an existing Java project with Ant build that I am trying to set up to build with mixed source (Java and Scala).

I hava a Java interface defined as - public interface One<T extends Two<? super T>>

that when I run scalac from Ant build, I get the following error at :

Error: Illegal cyclic reference involving type T.

Since I want the Scala and Java class to be inter-dependent in my project, I first run the scalac task before running javac in the Ant build. My scalac block looks as follows:

<scalac srcdir="${src.path}" destdir="${build.classes}" encoding="UTF-8" >
        <classpath refid="compile.classpath" />
        <include name="**/*.scala"/>
        <include name="**/*.java"/>
</scalac> 

Is there any way getting around this? I can't modify the Java code since there is tons of other code that uses it.

I am using Java 7 and Scala 2.11.5


Here’s what I see at my end:

com.arif.One.java

package com.arif.java;
public interface One<T extends One<? super T>> {
    void test1();
}

com.arif.Two.java

package com.arif.java;
public class Two implements One<Two> {
    @Override
    public void test1() {
    } 
}

com.arif.scala.Main.scala

package com.arif.scala
import com.arif.java.Two

object Main {
  def main(args:Array[String]) {
    var b:Two = null
  } 
}

build.xml

<property name="scala.home" value="/Users/arif/software/scala/scala-2.11.5"/>
<property name="build.classes" value="./build/classes"/>

<property name="src.path" value="src"/>
<property name="javac.target" value="1.7"/>
<property name="compile.debug" value="true" />
<property name="compile.deprecation" value="false" />
<property name="compile.optimize" value="false" />

<path id="compile.classpath">
    <fileset dir="${scala.home}/lib" includes="*.jar" />
</path>

<path id="scala.classpath">
    <pathelement location="${scala.home}/lib/scala-compiler.jar" />
    <pathelement location="${scala.home}/lib/scala-library.jar" />
    <pathelement location="${scala.home}/lib/scala-reflect.jar" />
</path>

<target name="init">
    <taskdef resource="scala/tools/ant/antlib.xml">
        <classpath refid="scala.classpath" />
    </taskdef>
</target>

<target name="build" description="build" depends="init">
    <scalac srcdir="${src.path}" destdir="${build.classes}" encoding="UTF-8" >
        <classpath refid="compile.classpath" />
        <include name="**/*.scala"/>
        <include name="**/*.java"/>
    </scalac>
    <javac srcdir="${src.path}" destdir="${build.classes}" memoryinitialsize="512m" target="${javac.target}" source="${javac.target}" 
        memorymaximumsize="1024m" nowarn="true" debug="${compile.debug}" deprecation="${compile.deprecation}" 
        optimize="${compile.optimize}" fork="true" encoding="iso-8859-1" includeantruntime="false">
        <classpath refid="compile.classpath" />
    </javac>
</target>

From command line in the project root - doing an "ant build" gives the following -

[scalac] Compiling 1 scala and 2 java source files to /Users/arif/rbc/scala-java/build/classes
[scalac] /Users/arif/rbc/scala-java/src/com/arif/java/One.java:3: error: illegal cyclic reference involving type T
[scalac] public interface One<T extends One<? super T>> {
[scalac]                                    ^
[scalac] one error found

Solution

  • The PR for SI-4744 mentions an experimental flag -Ybreak-cycles that might help with this issue.