1
Class<?> clazz = loadClass( "Test" );
Method printSomething = clazz.getDeclaredMethod( "printSomething" );
printSomething.invoke( clazz );

I'm trying to figure out how to do this in Scala. I'm guessing I'm missing something simple with the way Scala handles reflection.

val clazz = loadClass("Test")
val printSomething = clazz.getDeclaredMethod("printSomething")

printSomething.invoke(clazz)

My main question: Is the Any object the same as Class<?> in Java?

Drew H
  • 4,699
  • 9
  • 57
  • 90
  • Your code isn't complete enough to tell what's up here - where does "render" come from, for example. – Paul Butcher Dec 27 '11 at 21:09
  • 3
    What's wrong with the Scala code you posted, if anything? – Dan Burton Dec 27 '11 at 22:00
  • Fixed code. Well I'm more or less asking what does Class> transfer to in Scala? Thanks. – Drew H Dec 28 '11 at 03:53
  • 1
    Class remains a class. It would written as Class[_] in scala, but meaning would be the same. Everything in scala is Any. – aishwarya Dec 28 '11 at 07:49
  • What exactly is your question? You've posted some code, but what is that code doing (or not doing) that you don't expect? Are you just asking what type clazz is? In which case (as @aishwarya says, it's Class[_]), which you can trivially tell for yourself by typing this into the REPL. But I get the impression that there's something more to your question - but I have no idea what it is? – Paul Butcher Dec 28 '11 at 12:50
  • Thanks. Well IntelliJ isn't providing any sort of auto completion(not that it's the end of the world) on the reflection methods. Is there any other way I'm missing on how to do reflection in Scala? I'm not getting too many hits in Google on reflection, that's why I was asking here. I saw this question and it threw me off a bit. http://stackoverflow.com/questions/1469958/scala-how-do-i-dynamically-instantiate-an-object-and-invoke-a-method-using-refl But yes this code compiles and runs fine. Thanks again. – Drew H Dec 28 '11 at 13:55

1 Answers1

4

Any is not the same as Class<?> in Java.

The Any type is a Scala alternative to Java's Object with extensions: in difference to Java's Object it is a supertype to literally everything in Scala, including primitives.

The Class[_] (short for Class[Any]) is the same type as Java's Class<?>, namely it is the way Java's Class<?> is presented in Scala. Instances of type Class in Scala as much as in Java provide the basic reflection capabilities over the type provided as its generic parameter. I.e. an instance of Class[Something] provides the standard Java's reflection API over class Something. You can use it the same way you do in Java. To get that instance you call the standard method classOf[Something] or instanceOfSomething.getClass.

Extended reflection capabilities primarily targeted at the solution of the type erasure issue are coming with Scala 2.10, pretty stable snapshot versions of which you can always download. This extended API is provided thru the object scala.reflect.runtime.Mirror. There's not much documentation on it in the internet yet, but you can find some decent information here on StackOverflow.

Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169