我在这样的类中有一个方法:
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<RuleEvent>
的类对象呢? ????
我已经知道((List<RuleEvent>) new ArrayList<RuleEvent>()).getClass()
的方法了但应该有更直接的方法......
最佳答案
你只需要以下内容
cls.getMethod("postEvents", List.class).invoke(null, eventList);
运行时不需要通用类型。
关于java - 在 java 反射中发送到 getDeclaredMethod(String, Class[]) 的参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8701742/