javaeventssystem

Implementing an event system (Java)


Ok, so I am basically trying to create my own events/listener system for a project I'm working on. I'm trying to have a system where I create an event with an abstract class and I can then create a listener that every time that event is called, the code within the listener is also called. Here's what I've got so far:

Event.java (Abstract class where I will be able to create new events)

private String name;
private boolean cancelled;

public String getEventName() {
    if (this.name == null) {
        this.name = this.getClass().getSimpleName();
    }
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public boolean isCancelled() {
    return cancelled;
}

public void setCancelled(boolean cancelled) {
    this.cancelled = cancelled;
}

EventHandler.java (Interface for being able to recognise when a method is a listener)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler {}

Listener.java (Interface for being able to recognise when a class contains a listener)

public interface Listener {}

EventExecutor.java (A class that will execute the listener code, I've started but this is where I need help to call the method to execute the code)

public void execute(Class clazz){
    if(!clazz.isAssignableFrom(Listener.class)) return;
    for(Method method : clazz.getMethods()){
        if(!method.isAnnotationPresent(EventHandler.class)) continue;
    }
}

So what I am asking is how (in EventExecutor) could I call (invoke) the method when the annotation is present. The method for example could be public void onUpdate(ProgramUpdateEvent event) or be onClose(ProgramCloseEvent event). Thanks any help is appreciated.


Solution

  • You need to actually have an instance of the listener. Your execute method should look like this:

    public void execute(Listener listener) {
    

    To invoke, just put

    method.invoke(listener, event);
    

    The first argument is the object to actually invoke the method on, and the second one is the parameter for the method. Because the methods aren't static, you need a reference to the object. (if you make them static, just put null instead of the listener)