我有一个对象,我需要将它的类传递给一个带有java.lang.Class的注释,例如:
public @interface PrepareForTest {
Class<?>[] value()
}
object MyObject
@PrepareForTest(Array(?????))
class MySpec ...
我试过了:
@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
不确定还有什么可以尝试。
答案 0 :(得分:4)
classOf[MyObject$]
不起作用,因为没有名为MyObject$
的类型。
事实上,问题确实出现过,并没有简单的解决方案。请参阅https://lampsvn.epfl.ch/trac/scala/ticket/2453#comment:5
上的讨论答案 1 :(得分:1)
查看throws
注释:
@throws(classOf[IOException])
注释本身声明为:
class throws(clazz: Class[_]) extends StaticAnnotation
对于object
执行相同操作似乎不可能,因为scala似乎阻止对象被视为具有自己的类