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

Moz*_*ath 4 java generics reflection annotations

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

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

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

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);
Run Code Online (Sandbox Code Playgroud)

那么,我怎么得到List<RuleEvent>???? 的类对象

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

Pet*_*rey 9

您只需要以下内容

cls.getMethod("postEvents", List.class).invoke(null, eventList);
Run Code Online (Sandbox Code Playgroud)

运行时不需要泛型类型.