com.slack.api.rtm.RTMEventHandler Maven / Gradle / Ivy
The newest version!
package com.slack.api.rtm;
import com.slack.api.model.event.Event;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* Real Time Messaging API event handler base class.
*
* @param The type of an events API Payload
*/
public abstract class RTMEventHandler {
private String cachedEventName;
private String cachedEventSubName;
private Class cachedClazz;
/**
* Returns the type value of the event (e.g., MessageEvent.TYPE_NAME)
*/
public String getEventType() {
if (cachedEventName != null) {
return cachedEventName;
}
Class clazz = getEventClass();
try {
Field field = clazz.getField("TYPE_NAME");
field.setAccessible(true);
cachedEventName = (String) field.get(null);
return cachedEventName;
} catch (Exception e) {
throw new IllegalStateException("A static field TYPE_NAME in " + clazz.getCanonicalName() + " is required");
}
}
/**
* Returns the subtype value of the event (e.g., MessageEvent.TYPE_NAME)
*/
public String getEventSubType() {
if (cachedEventSubName != null) {
return cachedEventSubName;
}
Class clazz = getEventClass();
try {
Field field = clazz.getField("SUBTYPE_NAME");
field.setAccessible(true);
cachedEventSubName = (String) field.get(null);
return cachedEventSubName;
} catch (Exception e) {
cachedEventSubName = "";
return cachedEventSubName;
}
}
/**
* Returns the Class object of the Event implementation.
*/
public Class getEventClass() {
if (cachedClazz != null) {
return cachedClazz;
}
Class> clazz = this.getClass();
while (clazz != Object.class) {
try {
Type mySuperclass = clazz.getGenericSuperclass();
Type tType = ((ParameterizedType) mySuperclass).getActualTypeArguments()[0];
cachedClazz = (Class) Class.forName(tType.getTypeName());
return cachedClazz;
} catch (Exception e) {
}
clazz = clazz.getSuperclass();
}
throw new IllegalStateException("Failed to load event class - " + this.getClass().getCanonicalName());
}
/**
* Implement your logic in this method.
*
* @param event event data
*/
public abstract void handle(E event);
/**
* Used only internally.
*
* @param event event data
*/
public void acceptUntypedObject(Object event) {
handle((E) event);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy