是否可以获取scala中方法的参数名称
据我所知,我们在Java中无法做到这一点。我们可以在scala中做到这一点吗?
假设有一个方法如下:
def insert(name:String, age:Int) {
// insert new user
}
scala中是否可以获取参数名称name
和age
?
更新
我想这样做,因为我想自动绑定方法的参数。
例如,这是一个 Web 应用程序,它的一些操作定义为:
class UsersController extends Controller {
def create(name: String, age: Int) {
// insert the user
}
}
客户端单击创建用户表单的 submit
按钮。 url 将为 /users/create
并发送一些参数。
在服务器端,当我们得到一个名为/users/create
的url时,我们会在控制器UsersController
中找到一个方法create
,找到一个现在。然后我必须获取该方法的参数名称,然后我们才能获取它们的值:
val params = getParamsFromRequest()
val method = findMethodFromUrl("/users/create")
val paramNames = getParamNamesOfMethod(method)
val paramValues = getValues(params, paramNames)
// then invoke
method.invoke(controller, paramValues)
现在,关键是如何获取方法的参数名称?
As far as I know, we can't do this in Java. Can we do this in scala?
Suppose there is a method as following:
def insert(name:String, age:Int) {
// insert new user
}
Is it possible to get the parameter names name
and age
in scala?
UPDATE
I want to do this, because I want to bind the parameters of methods automaticlly.
For example, this is a web app, it has some actions defined as:
class UsersController extends Controller {
def create(name: String, age: Int) {
// insert the user
}
}
A client clicked the submit
button of a creating-user form. The url will be /users/create
and with some parameters sending.
On the server side, when we get a url named /users/create
, we will find a method create
in the controller UsersController
, found one now. Then I have to get the parameter names of that method, then we can get the values of them:
val params = getParamsFromRequest()
val method = findMethodFromUrl("/users/create")
val paramNames = getParamNamesOfMethod(method)
val paramValues = getValues(params, paramNames)
// then invoke
method.invoke(controller, paramValues)
Now, the key is how to get the parameter names of a method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它仍然非常正在进行中,但你应该能够to:
对
toSeqs
感到抱歉,它们是为了在 for-compression 中使用Option
解决已知问题。请不要把它逼得太紧,它还很年轻而且很脆弱。我当然还没有到接受错误报告的阶段:)
更新
嗯...我知道我说过我不接受错误报告,但是这个似乎非常有用,以至于反正我已经把它推到github上了。
要动态地完成这项工作(从 String 类名):
It's still very much a work in progress, but you should be able to:
Sorry about the
toSeqs
, they're to work around a known issue usingOption
s in a for-comprehension.Please don't push it too hard, it's still very young and fragile. I'm certainly not at the stage yet where I'd accept bug reports :)
UPDATE
Well... I know that I said I'm not accepting bug reports, but this one seemed so useful that I've pushed it up to github anyway.
To do the job dynamically (from a String class name):
这个问题对Java中的实现方式有一些了解: Java中有没有办法获取方法参数的名称?
This question has some insight on how it is done in Java: Is there a way to obtain names of method parameters in Java?
为什么不只是使用不同的方法名称(以反映可能传递的参数)?就像,
craeteWithNameAndAgeAndGender
(相当标准的方法)。无论如何,您将无法拥有具有相同名称(/arity/参数类型和顺序)但参数名称不同的多个方法 - 方法重载无法以这种方式工作。Why not just using different method names (to reflect the parameters that may be passed)? Like,
craeteWithNameAndAgeAndGender
(pretty standard approach). You won't anyways be able to have multiple methods with the same names(/arity/parameter types and order) and just different parameter names - method overloading doesn't work this way.