简体   繁体   English

如何为Java提供Scala随播对象的类?

[英]How can I provide a scala companion object's class to Java?

I have a Java code that looks for annotations in static methods of a class. 我有一个Java代码,用于在类的静态方法中查找注释。

processor.readStatics( MyClass.class );  // Takes Class<?>

How can I provide the methods of a scala companion object to this function from within scala? 如何从scala中向此函数提供scala伴侣对象的方法?

class MyClass {
}
object MyClass {
  def hello() { println("Hello (object)") } 
}

I seems that: 我似乎是:

MyClass$.MODULE$.getClass()

should be the answer . 应该是答案 However, MyClass$ seems to be missing from scala (in 2.10, at least) and only visible to Java. 但是,scala中似乎缺少MyClass $(至少在2.10中),并且仅Java可见。

println( Class.forName("MyClass$.MODULE$") )

also fails. 也失败了。

Class name is MyClass$ (with the appropriate package name prepended). 类名是MyClass$ (带有适当的包名)。

println(Class.forName("MyClass$")) will print out "class MyClass$". println(Class.forName("MyClass$"))将打印出“ class MyClass $”。

MyClass$.MODULE$ is the instance of the class, referencing the singleton object. MyClass$.MODULE$是类的实例 ,引用单例对象。

println(MyClass$.MODULE$ == MyClass) will print out "true" even though, when compiling, you will get a warning that this comparison always yields false :) 即使在编译时, println(MyClass$.MODULE$ == MyClass)也会打印出“ true”,尽管您会收到一条警告,指出该比较总是产生false :)

Note, that none of this works in repl for some reason. 请注意,由于某种原因,这些都不能在repl中起作用。 You need to actually create a .scala file, compile it with scalac, and run. 您实际上需要创建一个.scala文件,使用scalac进行编译,然后运行。

So, in java, use MyClass$ to reference the class of MyClass object statically, use MyClass$.MODULE$ to reference the singleton instance of MyClass object, use MyClass$.class or MyClass$.MODULE$.getClass() to reference the class of the singleton object dynamically, use Class.forName("MyClass$") to access it at runtime by name. 因此,在Java中,使用MyClass$静态引用MyClass对象的类,使用MyClass$.MODULE$引用MyClass对象的单例实例,使用MyClass$.classMyClass$.MODULE$.getClass()引用单例对象的动态类,使用Class.forName("MyClass$")在运行时按名称访问它。

The shortest and type-safest solution is to simply use 最短和最安全的解决方案是简单地使用

MyClass.getClass

I would have hoped the following to work, but apparently scalac is not happy with it: 我本来希望以下方法能起作用,但显然scalac对此并不满意:

classOf[MyClass.type]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Scala中,如何为Java中定义的类定义伴随对象? - In Scala, how can I define a companion object for a class defined in Java? 如何在泛型类中引用伴随对象的方法? - How can I reference a companion object's method in a generic class? 为什么我不能在Scala中的类的伴随对象中访问私有类方法? - Why can't I access private class methods in the class's companion object in Scala? 我可以在Scala中声明其伴随对象中的类吗? - Can I in Scala declare the class inside its companion object? 在Scala中,如何确定类+随播对象与仅对象之间的关系 - In scala, how I can decide between class + companion object vs just the object 在Scala中,如何从其伴随对象访问案例类的私有构造函数 - In Scala, how do I access a case class's private constructor from its companion object 类和伴随对象如何在Scala中看到私有val? - How can a class and companion object see private vals in Scala? 如何访问作为类型参数传递的Scala类的伴随对象? - How can I access the companion object of a Scala class passed as a type param? 如何满足IntelliJ IDEA对我的Scala类的伴随对象的声明进行测试的期望? - How do I satisfy IntelliJ IDEA's expectation that my scala class's companion object's declaration be tested? Scala:在伴侣对象中获取伴侣类 - Scala: Get companion class in companion object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM