Get class <T> at compile time?
In Java you can write:
Class<SomeClass> foo = SomeClass.class; I play with Scala and want a little more: I want to get a method (and its generic type) at compile time, for example:
val foo : Method[...] = SomeClass.class.someMethod or even:
val foo : Method[...] = someObject.class.someMethod Is there a way to do something like this in Scala?
If possible, the only exception to interacting with the Java library that requires Method instances should never be the need to do this in Scala.
If you need to pass a method for a subsequent call; then partially apply it to create an instance of FunctionN , pass it using the by-name parameter, or create it directly as a first-class function in the first case.
If you need to think about a method so that you can bypass confidential visibility, perhaps for testing, then you really need to reorganize your design to avoid this limitation of visibility in the first place.
If you just study Scala and see how concepts compare with java. Or, if you are unsuccessful enough to get stuck with a bad case of interaction, then the equivalent SomeClass.class construct is equal to classOf[SomeClass] . I hope you do not need it :)
For class:
val classOfClass = classOf[SomeClass] For the object (it is important to distinguish between the use of getClass, since otherwise the companion class would not be available):
val classOfObject = SomeClass.getClass Assuming the code is as follows:
class SomeClass { def someMethod() {} } object SomeClass After that, you can use Java reflection operations such as Class#getMethod(String,Class<?>...) (assuming someMethod has no parameters):
val method = classOfClass.getMethod("someMethod")