在 java 反射中发送到 getDeclaredMethod(String, Class[]) 的参数类型

发布于 2024-12-23 13:20:13 字数 640 浏览 3 评论 0原文

我在这样的类中有一个方法:

public static void postEvents(List<RuleEvent> eventList) {
    for(RuleEvent event:eventList)
        if(canProcess(event))
            findListenerAndPost(event);
    }

并且我想使用这样的反射来访问它:

Class partypes[] = new Class[1];
partypes[0] = List.class;    //does not find the method as it is of List<RuleEvent>
postMethod = cls.getMethod("postEvents", partypes);

那么,如何获取 List 的类对象???

我已经知道 ((List) new ArrayList()).getClass() 的方式,但应该有更直接的方式...

i have a method in a class like this:

public static void postEvents(List<RuleEvent> eventList) {
    for(RuleEvent event:eventList)
        if(canProcess(event))
            findListenerAndPost(event);
    }

and i want to access it using reflection like this:

Class partypes[] = new Class[1];
partypes[0] = List.class;    //does not find the method as it is of List<RuleEvent>
postMethod = cls.getMethod("postEvents", partypes);

so, how do I get the class object of List<RuleEvent>????

I already know the way of ((List<RuleEvent>) new ArrayList<RuleEvent>()).getClass() but there should be a more direct way...

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

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

发布评论

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

评论(1

不喜欢何必死缠烂打 2024-12-30 13:20:13

您所需要的只是以下

cls.getMethod("postEvents", List.class).invoke(null, eventList);

运行时不需要泛型类型。

All you need is the following

cls.getMethod("postEvents", List.class).invoke(null, eventList);

The generic type is not required at runtime.

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