/
ReflectionEventHandler.java
40 lines (34 loc) · 1.12 KB
/
ReflectionEventHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package de.cubeside.globalserver.event;
import java.lang.reflect.Method;
class ReflectionEventHandler<E extends Event> extends AbstractEventHandler<E> {
private final Method method;
private final Object instance;
public ReflectionEventHandler(Class<E> eventClass, int priority, Object instance, Method method) {
super(eventClass, priority);
this.instance = instance;
this.method = method;
}
@Override
public void handle(E event) {
try {
method.invoke(instance, event);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@Override
public int hashCode() {
return method.hashCode() ^ (instance == null ? 0 : instance.hashCode());
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ReflectionEventHandler)) {
return false;
}
ReflectionEventHandler<?> other = (ReflectionEventHandler<?>) obj;
return other.instance == instance && other.method.equals(method);
}
}