title: Implementing an event system (Java)
body: <p>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:</p>
  
  <p>Event.java (Abstract class where I will be able to create new events)</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>EventHandler.java (Interface for being able to recognise when a method is a listener)</p>
  
  <pre><code>@Target(ElementType.METHOD)
  @Retention(RetentionPolicy.RUNTIME)
  public @interface EventHandler {}
  </code></pre>
  
  <p>Listener.java (Interface for being able to recognise when a class contains a listener)</p>
  
  <pre><code>public interface Listener {}
  </code></pre>
  
  <p>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)</p>
  
  <pre><code>public void execute(Class clazz){
      if(!clazz.isAssignableFrom(Listener.class)) return;
      for(Method method : clazz.getMethods()){
          if(!method.isAnnotationPresent(EventHandler.class)) continue;
      }
  }
  </code></pre>
  
  <p>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.</p>