Class Subscriber

java.lang.Object
org.omegazero.common.eventbus.Subscriber

public class Subscriber extends Object
Represents an EventBus subscriber. This class wraps an event bus subscriber class and instance.
Since:
2.1
  • Constructor Details

    • Subscriber

      public Subscriber(Object instance)
      Alias for Subscriber(Class, Object).

      The type parameter is derived from the given instance parameter.

      Parameters:
      instance - The subscriber instance
    • Subscriber

      public Subscriber(Class<?> type)
      Creates an Subscriber instance for use with the EventBus.

      The class type must have the annotation EventBusSubscriber set.

      Only static event listener methods can be called when using this constructor. To be able to use non-static methods, pass an instance with Subscriber(Class, Object) or Subscriber(Object).

      Parameters:
      type - The class of the subscriber
    • Subscriber

      public Subscriber(Class<?> type, Object instance)
      Creates an Subscriber instance for use with the EventBus.

      The class type must have the annotation EventBusSubscriber set.

      The object given in instance will be the instance the method is called with.

      Parameters:
      type - The class of the subscriber
      instance - The subscriber instance
  • Method Details

    • isValidListenerMethod

      public boolean isValidListenerMethod(Method m)
      Returns true if the given method m is a valid event listener method.
      Parameters:
      m - The method to validate
      Returns:
      true if m is a valid event listener method
    • getListenerMethodForEvent

      public Method getListenerMethodForEvent(Event event)
      Searches the valid event listener method suitable for the given event.

      The result of this method will be cached, including when a method was not found.

      Parameters:
      event - The event to find a suitable method for
      Returns:
      The valid event listener method, or null if the method was not found
    • isListenerMethodForEventAvailable

      public boolean isListenerMethodForEventAvailable(Event event)
      Checks if there is a valid listener method available at this event bus subscriber.
      Parameters:
      event - The event to check
      Returns:
      true if a valid listener method for this event was found, false otherwise
    • getListenerMethod

      public Method getListenerMethod(String name, Class<?>[] parameterTypes, Class<?> returnType)
      Searches the valid event listener method with the given name and parameterTypes. Returns null if a suitable method was not found.

      The result of this method will be cached, including when a method was not found.

      Parameters:
      name - The name of the method to be searched
      parameterTypes - An array of parameter types the method should have
      returnType - The return type of the method. Void methods always match
      Returns:
      The valid event listener method, or null if the method was not found
    • getType

      public Class<?> getType()
      Returns the class of the event bus subscriber instance this Subscriber wraps.
      Returns:
      The subscriber type
    • getInstance

      protected Object getInstance()
      Returns the event bus subscriber instance this Subscriber wraps.
      Returns:
      The subscriber instance
    • getForcedEvents

      public String[] getForcedEvents()
      Returns the array of event method names the subscriber has declared itself as listening to. See setForcedEvents(String[]).
      Returns:
      The array of event method names
    • setForcedEvents

      public void setForcedEvents(String[] forcedEvents)
      Sets the array of event method names the subscriber declares itself as listening to. If no suitable handler method is found when attempting to execute an event with a method name in this array, event execution will fail with an EventBusException.
      Parameters:
      forcedEvents - The array of event method names
    • isForcedEvent

      public boolean isForcedEvent(Event event)
      Checks if this Subscriber has explicitly declared itself as listening to the given event using setForcedEvents(String[]).
      Parameters:
      event - The event
      Returns:
      true if this subscriber has declared itself as listening to the given event
    • runEvent

      public Object runEvent(Event event, Object... args) throws ReflectiveOperationException
      Gets the event handler method for the passed event and invokes it with the given arguments.

      A handler method must have the annotation SubscribeEvent with the optional SubscribeEvent.priority() argument.

      Parameters:
      event - The event to be dispatched to the event bus subscriber
      args - Arguments to be passed to the event method
      Returns:
      The value returned by the event method
      Throws:
      ReflectiveOperationException - If a reflection operation fails
      See Also:
    • runEventMethod

      public Object runEventMethod(Method eventListener, Object... args) throws ReflectiveOperationException
      Validates and invokes the method eventListener with the given arguments.

      eventListener must be a valid event listener. See runEvent(Event, Object...).

      Parameters:
      eventListener - A valid event listener method
      args - Arguments to be passed to the event method
      Returns:
      The value returned by the event method
      Throws:
      ReflectiveOperationException - If a reflection operation fails