Reputation: 107
I have an object and I need to pass its class to an annotation that takes java.lang.Class, eg:
public @interface PrepareForTest {
Class<?>[] value()
}
object MyObject
@PrepareForTest(Array(?????))
class MySpec ...
I've tried:
@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
Not sure what else to try.
Upvotes: 6
Views: 3632
Reputation: 1894
classOf[MyObject$]
does not work because there is no type called MyObject$
.
In fact, the issue did come up before and there is no easy solution. See the discussion on https://lampsvn.epfl.ch/trac/scala/ticket/2453#comment:5
Upvotes: 4
Reputation: 134270
Have a look at the throws
annotation:
@throws(classOf[IOException])
The annotation itself is declared as:
class throws(clazz: Class[_]) extends StaticAnnotation
Doing the same for an object
does not seem to be possible because scala seems to prevent an object from being viewed as having a class in its own right
Upvotes: 1