com.github.dxee.dject.event.guava.GuavaApplicationEventDispatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dject-event Show documentation
Show all versions of dject-event Show documentation
A collection of guice extensions, help to improve the developer experience of guice.
package com.github.dxee.dject.event.guava;
import com.github.dxee.dject.event.ApplicationEvent;
import com.github.dxee.dject.event.ApplicationEventDispatcher;
import com.github.dxee.dject.event.ApplicationEventListener;
import com.github.dxee.dject.event.ApplicationEventRegistration;
import com.google.common.eventbus.EventBus;
import com.google.common.reflect.TypeToken;
import javax.inject.Inject;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
final class GuavaApplicationEventDispatcher implements ApplicationEventDispatcher {
private final EventBus eventBus;
private final Method eventListenerMethod;
@Inject
public GuavaApplicationEventDispatcher(EventBus eventBus) {
this.eventBus = eventBus;
try {
this.eventListenerMethod = ApplicationEventListener.class
.getDeclaredMethod("onEvent", ApplicationEvent.class);
} catch (Exception e) {
throw new RuntimeException("Failed to cache ApplicationEventListener method", e);
}
}
public ApplicationEventRegistration registerListener(Object instance, Method method,
Class extends ApplicationEvent> eventType) {
GuavaSubscriberProxy proxy = new GuavaSubscriberProxy(instance, method, eventType);
eventBus.register(proxy);
return new GuavaEventRegistration(eventBus, proxy);
}
public ApplicationEventRegistration registerListener(
Class eventType, ApplicationEventListener eventListener) {
GuavaSubscriberProxy proxy = new GuavaSubscriberProxy(eventListener, eventListenerMethod, eventType);
eventBus.register(proxy);
return new GuavaEventRegistration(eventBus, proxy);
}
public ApplicationEventRegistration registerListener(
ApplicationEventListener extends ApplicationEvent> eventListener) {
Type[] genericInterfaces = eventListener.getClass().getGenericInterfaces();
for (Type type : genericInterfaces) {
if (ApplicationEventListener.class.isAssignableFrom(TypeToken.of(type).getRawType())) {
ParameterizedType ptype = (ParameterizedType) type;
Class> rawType = TypeToken.of(ptype.getActualTypeArguments()[0]).getRawType();
GuavaSubscriberProxy proxy = new GuavaSubscriberProxy(eventListener, eventListenerMethod, rawType);
eventBus.register(proxy);
return new GuavaEventRegistration(eventBus, proxy);
}
}
return new ApplicationEventRegistration() {
public void unregister() {} //no-op. Could not find anything to register.
};
}
@Override
public void publishEvent(ApplicationEvent event) {
this.eventBus.post(event);
}
}