Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to protected inner class in Scala when inheriting from Java (with byte code only)

Tags:

scala

I am writing a Scala class to inherit from a Java class, and I must override a method that takes a protected Java inner class as a parameter. The Java dependency comes as a jar without source code.

I have the exact same setup as found in https://issues.scala-lang.org/browse/SI-3120 except that I do not have the Java source code available, so scalac only knows about the Java dependency by looking at the byte code (in jar or class files).

This is basically what I'm trying to do:

// javapkg/JavaSuperClass.java
package javapkg;
public class JavaSuperClass {
    protected class JavaInnerClass {
    }
    public void method(JavaInnerClass javaInnerclass) {
        System.out.println("hello");
    }
}

// scalapkg/ScalaSubClass.scala
package scalapkg
import javapkg.JavaSuperClass
class ScalaSubClass extends JavaSuperClass {
  override def method(javaInnerClass: JavaSuperClass#JavaInnerClass) {
    println("world")
  }
}

I have Java Sun JDK Hotspot 1.6.0_24 and Scala 2.9.0.1 on Linux. This is what happens:

$ cd javapkg
$ javac JavaSuperClass.java
$ cd ../scalapkg
$ scalac -cp .. ScalaSubClass.scala
ScalaSubClass.scala:6: error: class JavaInnerClass in class JavaSuperClass cannot be accessed in javapkg.JavaSuperClass
 Access to protected class JavaInnerClass not permitted because
 prefix type javapkg.JavaSuperClass does not conform to
 class ScalaSubClass in package scalapkg where the access take place
  override def method(javaInnerclass: JavaSuperClass#JavaInnerClass) {
                                                     ^
one error found

Note, if I change JavaSuperClass#JavaInnerClass to simply JavaInnerClass, I get this:

ScalaSubClass.scala:6: error: method method overrides nothing
  override def method(javaInnerClass: JavaInnerClass) {
               ^
one error found

Note: I know this sounds very similar to the common "protected static inner class" Java-compatibility issue in Scala, but I believe this is unrelated because there are no statics anywhere in my example.

I feel like something is wrong, because when I put the same code into a mixed java/scala project in Eclipse, it seemed to compile fine (with the latter JavaInnerClass syntax); it's only when I compile the Scala code with only the Java byte code (and no Java source code) that I cannot get it to work. Am I just completely missing the correct syntax to refer to a Java inner class, is this a known defect, or should I file a compiler bug? I couldn't find anything about this exact use case in my searching.

like image 439
Mike Avatar asked Jun 25 '11 08:06

Mike


1 Answers

This is an excellent article that discuss the topic.

EDIT-1

My bad, I answered to quickly. This actually may be a bug Mike, I'm trying to see if I can find a hack around. I'll let you know if I find one.

EDIT-2

I've tried different things but I can't find a way to make it work. Mike I'd suggest you to file a bug report.

like image 150
Mirco Dotta Avatar answered Oct 21 '22 00:10

Mirco Dotta