Argument types sent to getDeclaredMethod (String, Class []) in java reflection

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 this reflection:

 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 an object of class List<RuleEvent> ????

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

+4
source share
1 answer

All you need is

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

Generic type is not required at runtime.

+9
source

Source: https://habr.com/ru/post/1388938/


All Articles