如何在java中使用反引用的scala标识符?

时间:2014-05-06 12:50:42

标签: java scala interop

我正在研究Java / Scala中用于分析Java代码的项目。在我的scala代码中,我有:

package p{
   object NodeKind{
      // In real code they are not Int but this is good enough for the example
      val `class` = 0 
      val interface = 0
   }
}

我需要从Java访问它

class MyJavaClass{
     void m(){
        /* ... */
        int value = p.NodeKind.//how can I access to `class` or interface ?
        /* ... */
     }
}

现在我已经解决了Brian Agnew解决方案,我添加了一个字段

  val classKind = `class`

在我的scala对象中,我可以在我的java代码中使用p.nodeKind.classKind()访问它,但我真的更喜欢知道`class`的编译(mangling?)规则和直接访问它的接口。

1 个答案:

答案 0 :(得分:0)

我怀疑你的两个选择可能是:

  1. 在Scala类中创建一个访问器方法
  2. 使用反射在Java类中创建一个访问器方法来查找该字段并将其读取
  3. e.g。

    class A {
       val class = 1
       def clazz = class
    }
    
相关问题