Using Scala class defined in package object from Java

夙愿已清 提交于 2019-12-19 19:48:16

问题


The following Scala example defines a class inside a package object:

package com.mycompany

package object test {
  class MyTest {
    def foo(): Int = {
      42
    }
  }
}

The following three classes are generated:

com/mycompany/test/package.class
com/mycompany/test/package$.class
com/mycompany/test/package$MyTest.class

The problem arises when trying to use the MyTest class from Java. I think that since package$MyTest contains a $ in the name, Java is not acknowledging its existence. Nevertheless, the package$ class is accessible.

Running javap on package$MyTest.class returns:

Compiled from "Test.scala"
public class com.mycompany.test.package$MyTest {
  public int foo();
  public com.mycompany.test.package$MyTest();
}

I've tried accessing the class using Eclipse, Intellij and Netbeans, without success. Is it possible to use Scala classes defined in package objects from Java?


回答1:


Defining a class inside the package object test is currently not implemented the same way as defining it inside the package test, although it probably should (this is being tracked as SI-4344).

Because of that, it is usually good practice to place into the package object only definitions that cannot be top level, like vals, defs, etc. (this is the whole point of package objects after all), and leave class/object definitions into a normal package definition:

package com.mycompany

package object test {
  // vals, defs, vars, etc.
  val foobar = 42
}

package test {
  // classes, objects, traits, etc.
  class MyTest {
    def foo(): Int = {
      42
    }
  }
}

This will produce a normal MyTest class, easily accessible from Java:

com/mycompany/test/MyTest.class
com/mycompany/test/package.class
com/mycompany/test/package$.class



回答2:


package$MyTest follows JVM convention for nested classes, so it would be visible as package.MyTest... except this isn't a legal name in Java. Which means it can't be accessed.



来源:https://stackoverflow.com/questions/23170058/using-scala-class-defined-in-package-object-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!