是否可以获取scala中方法的参数名称

发布于 2024-10-20 00:52:38 字数 970 浏览 2 评论 0原文

据我所知,我们在Java中无法做到这一点。我们可以在scala中做到这一点吗?

假设有一个方法如下:

def insert(name:String, age:Int) {
    // insert new user
}

scala中是否可以获取参数名称nameage


更新

我想这样做,因为我想自动绑定方法的参数。

例如,这是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独木成林 2024-10-27 00:52:38

它仍然非常正在进行中,但你应该能够to:

import scalaj.reflect._
for {
  clazz <- Mirror.ofClass[UsersController].toSeq
  method <- clazz.allDefs.find(_.name == "insert").toSeq
  params <- method.flatParams
} yield params

toSeqs 感到抱歉,它们是为了在 for-compression 中使用 Option 解决已知问题。

请不要把它逼得太紧,它还很年轻而且很脆弱。我当然还没有到接受错误报告的阶段:)

更新

嗯...我知道我说过我不接受错误报告,但是这个似乎非常有用,以至于反正我已经把它推到github上了。

要动态地完成这项工作(从 String 类名):

import scalaj.reflect._
for {
  clazz <- Option(Class forName "x.y.UsersController")
  mirror <- Mirror.ofClass(clazz).toSeq
  method <- mirror.allDefs.find(_.name == "insert").toSeq
  params <- method.flatParams
} yield params

It's still very much a work in progress, but you should be able to:

import scalaj.reflect._
for {
  clazz <- Mirror.ofClass[UsersController].toSeq
  method <- clazz.allDefs.find(_.name == "insert").toSeq
  params <- method.flatParams
} yield params

Sorry about the toSeqs, they're to work around a known issue using Options 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):

import scalaj.reflect._
for {
  clazz <- Option(Class forName "x.y.UsersController")
  mirror <- Mirror.ofClass(clazz).toSeq
  method <- mirror.allDefs.find(_.name == "insert").toSeq
  params <- method.flatParams
} yield params
謌踐踏愛綪 2024-10-27 00:52:38

这个问题对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?

半衾梦 2024-10-27 00:52:38

为什么不只是使用不同的方法名称(以反映可能传递的参数)?就像,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文