Java Reflection - 将形式参数列表与实际参数列表匹配

发布于 2024-12-20 17:12:32 字数 610 浏览 1 评论 0原文

假设我有一个像这样的类 Foo

public class Foo {
    public void setParameter(String name, Object value) {
       // ...
    }
}

我想通过反射获取 setParameter 方法。

问题是,在我的代码中,当我需要获取此方法时,我只知道 ACTUAL 参数。我构建了一个小方法,可以为我提供实际参数的 Class 数组。不幸的是,从实际参数返回的Class数组([String.class, String.class])不匹配 形式参数Class 数组 ([String.class, Object.class])getDeclaredMethod 引发异常NoSuchMethodException

您知道任何可以帮助匹配该方法的库或代码片段吗?

Let say I have a class Foo like this

public class Foo {
    public void setParameter(String name, Object value) {
       // ...
    }
}

I would like to get the setParameter method through reflection.

The problem is that in my code, I only know the ACTUAL parameters when I need to get this method. I have build a little method that can give me the Class array of actual parameters. Unfortunately the returned Class array from actual parameters ([String.class, String.class]) doesn't match the Class array of formal parameters ([String.class, Object.class]) and getDeclaredMethod raises exception NoSuchMethodException.

Do you know of any library or code snippet that can help match the method ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

醉生梦死 2024-12-27 17:12:32

您可能需要更聪明地寻找要调用的方法。一种选择是迭代 getDeclaredMethods(),首先检查方法的名称是否是您要调用的方法,然后检查实际参数的数量与形式参数的数量是否匹配参数,最后,如果这两个条件都为 true,则迭代其实际参数并检查参数类是否可从形式参数类分配。

然而,一个问题是,如果您有这些方法,会发生什么:

public void setParameter(String name, Object value);
public void setParameter(String name, String value);

因为两者都与该测试相匹配。

我不知道有哪个库可以以轻量级的方式处理这个问题。尽管大多数依赖注入框架(Spring、Guice)都会根据自己的需求提供一些支持,但可能值得研究一下它们是如何做事的。

Jon Skeet 的回答此处有一个我正在解释的示例。

You'll probably need to be a little smarter about finding which method to call. One option is to iterate through each method from getDeclaredMethods(), first checking the name of the method to see if that's the one you want to call, then checking if the number of actual parameters matches the number of formal parameters, and lastly, if both of those are true, iterating through its actual parameters and checking if the parameter class is assignable from your formal parameter class.

One problem, however, is what happens if you have the methods:

public void setParameter(String name, Object value);
public void setParameter(String name, String value);

as both will match that test.

I don't know of any library of the top of my head that can handle that in a lightweight way. Though most dependency-injection frameworks (Spring, Guice) would have some support for it for their needs, and it may be worth looking into how they do things.

Jon Skeet's answer here has an example of what I'm explaining.

蘑菇王子 2024-12-27 17:12:32

我只需使用 getDeclaredMethods() 来获取所有方法,然后根据方法名称、参数计数以及最后的 Class.isAssignableFrom() 在代码中进行匹配检查参数类型是否与签名匹配。这就留下了重载方法的签名在可以与相同具体类型(接口或超类/子类)兼容的类型上不同的情况。您必须决定您的代码是否必须支持此类极端情况。

I'd simply use getDeclaredMethods() to get all maethods and then do the matching in your code, based on method name, parameter count and finally Class.isAssignableFrom() to check whether the parameters' types match the signature. That leaves cases where overloaded methods' signatures differ in types that can be compatible with the same concrete type (interfaces or super/subclasses). You have to decide whether your code has to support such corner cases.

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