Class Subscriber
EventBus
subscriber. This class wraps an event bus subscriber class and instance.- Since:
- 2.1
-
Constructor Summary
ConstructorsConstructorDescriptionSubscriber
(Class<?> type) Creates anSubscriber
instance for use with theEventBus
.Subscriber
(Class<?> type, Object instance) Creates anSubscriber
instance for use with theEventBus
.Subscriber
(Object instance) Alias forSubscriber(Class, Object)
. -
Method Summary
Modifier and TypeMethodDescriptionString[]
Returns the array of event method names the subscriber has declared itself as listening to.protected Object
Returns the event bus subscriber instance thisSubscriber
wraps.getListenerMethod
(String name, Class<?>[] parameterTypes, Class<?> returnType) Searches the valid event listener method with the given name and parameterTypes.getListenerMethodForEvent
(Event event) Searches the valid event listener method suitable for the given event.Class<?>
getType()
Returns the class of the event bus subscriber instance thisSubscriber
wraps.boolean
isForcedEvent
(Event event) Checks if thisSubscriber
has explicitly declared itself as listening to the given event usingsetForcedEvents(String[])
.boolean
Checks if there is a valid listener method available at this event bus subscriber.boolean
Returnstrue
if the given method m is a valid event listener method.Gets the event handler method for the passed event and invokes it with the given arguments.runEventMethod
(Method eventListener, Object... args) Validates and invokes the method eventListener with the given arguments.void
setForcedEvents
(String[] forcedEvents) Sets the array of event method names the subscriber declares itself as listening to.
-
Constructor Details
-
Subscriber
Alias forSubscriber(Class, Object)
.The type parameter is derived from the given instance parameter.
- Parameters:
instance
- The subscriber instance
-
Subscriber
Creates anSubscriber
instance for use with theEventBus
.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)
orSubscriber(Object)
.- Parameters:
type
- The class of the subscriber
-
Subscriber
Creates anSubscriber
instance for use with theEventBus
.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 subscriberinstance
- The subscriber instance
-
-
Method Details
-
isValidListenerMethod
Returnstrue
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
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
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
Searches the valid event listener method with the given name and parameterTypes. Returnsnull
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 searchedparameterTypes
- An array of parameter types the method should havereturnType
- 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
Returns the class of the event bus subscriber instance thisSubscriber
wraps.- Returns:
- The subscriber type
-
getInstance
Returns the event bus subscriber instance thisSubscriber
wraps.- Returns:
- The subscriber instance
-
getForcedEvents
Returns the array of event method names the subscriber has declared itself as listening to. SeesetForcedEvents(String[])
.- Returns:
- The array of event method names
-
setForcedEvents
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 anEventBusException
.- Parameters:
forcedEvents
- The array of event method names
-
isForcedEvent
Checks if thisSubscriber
has explicitly declared itself as listening to the given event usingsetForcedEvents(String[])
.- Parameters:
event
- The event- Returns:
true
if this subscriber has declared itself as listening to the given event
-
runEvent
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 optionalSubscribeEvent.priority()
argument.- Parameters:
event
- The event to be dispatched to the event bus subscriberargs
- 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 methodargs
- Arguments to be passed to the event method- Returns:
- The value returned by the event method
- Throws:
ReflectiveOperationException
- If a reflection operation fails
-