We can use reflection to get method names as follows:
object Foo { def bar(name:String, age:Int) = {} }
val foo = Foo.getClass
val methods = foo.getMethods.filter(_.getName.startsWith("b"))
methods.foreach(m => println(m.getName))
I now need to get the parameter types and names.
String
and/or Array[String]
type parameters.[EDIT:] Java version of the solution also ok.
[EDIT:] Annotations seems to be one way to do it. However, Scala annotation support is not that good. Related SO question.
reflect package is used to fetch the parameter types using method parameter reflection. Reflection is a process of analyzing and modifying all capabilities of class at runtime.
Types obtained through reflection can be instantiated by invoking their constructor using an appropriate “invoker” mirror (mirrors are expanded upon below). Let's walk through an example using the REPL: scala> case class Person(name: String) defined class Person scala> val m = ru. runtimeMirror(getClass.
getName() returns the parameter names. If -parameters compiler argument has not been used, getName() method will return parameter name as arg0, arg1 etc.
I've not tried it, but http://paranamer.codehaus.org/ is designed for this task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With