lundi 29 novembre 2021

Java Reflect newInstance() causing a ConcurrentModificationException (CME) resulting in a crash

To sum up my project I'm trying to make an event processor that runs methods from a given list of methods. In order to invoke these methods I need to pass in the newInstance of the method. The method is correctly run, however, right after the whole program crashes with a "ConcurrentModificationException".

@EventHandler
private Listener<EventKeyInput> eventKeyInput = new Listener<>(event ->
{
    for(Method m : event.getMethods())
    {
        try {
            m.invoke(m.getDeclaringClass().newInstance(), event);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

});

One of the methods I wish to call

@EventAnnotation()
public void eventKeyInputeTest(EventKeyInput event)
{
    System.out.println("EventKeyInput test!");
}

Please let me know if I need to provide anymore code. Thank you for any help!





Aucun commentaire:

Enregistrer un commentaire