tags:

views:

268

answers:

2

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.

+1  A: 

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

oxbow_lakes
+3  A: 

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

Lukas Rytz