如何获取注释参数的scala对象的java.lang.Class

mem*_*let 6 scala

我有一个对象,我需要将其类传递给一个带有java.lang.Class的注释,例如:

public @interface PrepareForTest {
   Class<?>[] value()
}

object MyObject

@PrepareForTest(Array(?????))
class MySpec ...
Run Code Online (Sandbox Code Playgroud)

我试过了:

@PrepareForTest(Array(classOf[MyObject]))
// error: not found: type MyObject

@PrepareForTest(Array(MyObject))
// error: type mismatch
// found: MyObject.type (with underlying type object MyObject
// required: java.lang.Class[_]

@PrepareForTest(Array(classOf[MyObject.type]))
// error: class type required by MyObject.type found
Run Code Online (Sandbox Code Playgroud)

不知道还有什么可以尝试.

Luk*_*ytz 4

classOf[MyObject$]不起作用,因为没有名为 的类型MyObject$

事实上,这个问题以前就曾出现过,而且没有简单的解决方案。请参阅https://lampsvn.epfl.ch/trac/scala/ticket/2453#comment:5上的讨论