0

Here the problem: I have jar containing compiled scala code. In jar I see

Step.class Step$class.class

which both are legal java classes.

However when I try to use Step$class in Intellij Idea java project it says: "Cannot resolve symbol Step$class". But code gets compiled with maven so I assume problem is in IDE.

Vladimir
  • 12,753
  • 19
  • 62
  • 77

2 Answers2

1

Scala traits can contain implementations, unlike Java interfaces. scalac compiles this into an interface and an implementation class:

  ~/code/scratch/20120505 cat trait.scala 
package test

trait T {
    def a = println("foo")
}
  ~/code/scratch/20120505 scalac210 -d . trait.scala 

  ~/code/scratch/20120505 javap -classpath . test/T
Compiled from "trait.scala"
public interface test.T extends scala.ScalaObject{
    public abstract void a();
}

  ~/code/scratch/20120505 javap -classpath . test/T\$class
Compiled from "trait.scala"
public abstract class test.T$class extends java.lang.Object{
    public static void a(test.T);
    public static void $init$(test.T);
}

The IntelliJ Scala plugin does not expose the implementation class to Java code. It's not really a good idea to use this directly, as you are relying on an implementation detail of the Scala compiler.

You should invoke the corresponding methods on a subclass of the trait. You'll have to write that in Scala, though.

More info: How are Scala traits compiled into Java bytecode?

Community
  • 1
  • 1
retronym
  • 54,768
  • 12
  • 155
  • 168
0

Ups, silly me, I've just invalidated all caches and re-imported jars, now it get resolved.

Vladimir
  • 12,753
  • 19
  • 62
  • 77