registerListeners(BusGroup group, Object instance) {
final class DodgyLookup {
private DodgyLookup() {}
private static final Lookup INSTANCE;
static {
try {
var lookupField = Lookup.class.getDeclaredField("IMPL_LOOKUP");
UnsafeHacks.setAccessible(lookupField);
INSTANCE = (Lookup) lookupField.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
if (instance.getClass() == Class.class) {
Class> clazz = (Class>) instance;
return group.register(DodgyLookup.INSTANCE.in(clazz), clazz);
} else {
return group.register(DodgyLookup.INSTANCE.in(instance.getClass()), instance);
}
}
/**
* The unique name of this BusGroup.
*/
public String name() {
return group.name();
}
/**
* Registers all static methods annotated with {@link SubscribeEvent} in the given class.
*
* @param callerLookup {@code MethodHandles.lookup()} from the class containing listeners
* @param utilityClassWithStaticListeners the class containing the static listeners
* @return A collection of the registered listeners, which can be used to optionally unregister them later
*
* @apiNote This method only registers static listeners.
* If you want to register both instance and static methods, use
* {@link BusGroup#register(MethodHandles.Lookup, Object)} instead.
*/
public Collection register(MethodHandles.Lookup callerLookup, Class> utilityClassWithStaticListeners) {
return group.register(callerLookup, utilityClassWithStaticListeners);
}
/**
* Registers all methods annotated with {@link SubscribeEvent} in the given object.
*
* @param callerLookup {@code MethodHandles.lookup()} from the class containing the listeners
* @param listener the object containing the static and/or instance listeners
* @return A collection of the registered listeners, which can be used to optionally unregister them later
*
* @apiNote If you know all the listeners are static methods, use
* {@link BusGroup#register(MethodHandles.Lookup, Class)} instead for better registration performance.
*/
public Collection register(MethodHandles.Lookup callerLookup, Object listener) {
return group.register(callerLookup, listener);
}
/**
* Unregisters the given listeners from this BusGroup.
* @param listeners A collection of listeners to unregister, obtained from
* {@link #register(Class)} or {@link #register(Object)}
* {@link #register(MethodHandles.Lookup, Class)} or {@link #register(MethodHandles.Lookup, Object)}
*/
public void unregister(Collection listeners) {
group.unregister(listeners);
}
}