Getting parameter name from scala function
trait PublicApi{
def sayHi(from:String,content:String)
}
I know that it is impossible in java to get "from" and "content" in runtime but can scala manifest help me out here?? like defining
trait PublicApi{
def sayHi(from:String,conten开发者_如何学JAVAt:String)(m:Manifest)
}
invoking m.methodErasure.getArgumentName(0) would return "from"
In its current implementation, ClassManifest
is all about type reification, and would know nothing about parameter names.
Unless you:
- add meta data for parameter names at compile time
- or use a cut down version of ASM (Java bytecode manipulation and analysis framework) to extract debug information from a class at runtime
, as paranamer does (like Monkey mentions in the comments), you won't have any information on the parameter name (at least not before Java 8.0, even though it was initially mentioned for Java6!: the proposal is still in progress)
The Scala "signature" DOES contain these parameter names, as you rightly surmise. After all, they have to be stored somewhere for named/default parameters to work. You can also use scalap to see these names.
Feel free to take a look at my preliminary work on a reflection library.
It's a work in progress, and I make absolutely no guarantees as to the current feature set or correctness, but may give you some idea as to what's involved - you might even be able to use it for extracting parameter names in its current state. This is also something I'm actively working on, so you can expect it to improve with the passage of time...
精彩评论