Skip to content

Commit

Permalink
Apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Aug 9, 2021
1 parent cc8d73a commit 0ec733c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Expand Up @@ -24,7 +24,6 @@

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;

/**
Expand Down Expand Up @@ -64,13 +63,12 @@ public Multimap<Class<?>, EventHandler> findAllSubscribers(Object listener) {
Class<?> eventType = parameterTypes[0];
MethodHandle handle;
try {
handle = MethodHandles.lookup().unreflect(method);
handle = handle.asType(handle.type().generic().changeReturnType(void.class));
handle = MethodHandles.publicLookup().unreflect(method);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Method " + method + " failed to unreflect.", e);
}

EventHandler handler = new MethodHandleEventHandler(annotation.priority(), listener, handle);
EventHandler handler = new MethodHandleEventHandler(annotation.priority(), listener, handle, method.getName());
methodsInListener.put(eventType, handler);
}
}
Expand Down
Expand Up @@ -20,38 +20,46 @@
package com.sk89q.worldedit.util.eventbus;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.util.Objects;

public class MethodHandleEventHandler extends EventHandler {

private final MethodHandle methodHandle;
private final String methodName;
private final Object object;

/**
* Create a new event handler that uses MethodHandles to dispatch.
*
* @param priority the priority
* @param object The object to invoke it on
* @param methodHandle The handle to invoke
* @param methodName The name of the method (for equality checks)
*/
protected MethodHandleEventHandler(Priority priority, Object object, MethodHandle methodHandle) {
protected MethodHandleEventHandler(Priority priority, Object object, MethodHandle methodHandle, String methodName) {
super(priority);

this.object = object;
this.methodHandle = methodHandle;
this.methodHandle = methodHandle.asType(MethodType.methodType(void.class, Object.class, Object.class));
this.methodName = methodName;
}

@Override
public void dispatch(Object event) throws Exception {
try {
this.methodHandle.invokeExact(object, event);
} catch (Throwable e) {
// ew
throw new Exception(e);
} catch (Exception | Error e) {
throw e;
} catch (Throwable t) {
// If it's not an Exception or Error, throw it wrapped.
throw new Exception(t);
}
}

@Override
public int hashCode() {
return Objects.hash(methodHandle, object);
return Objects.hash(methodName, object);
}

@Override
Expand All @@ -65,7 +73,7 @@ public boolean equals(Object o) {

MethodHandleEventHandler that = (MethodHandleEventHandler) o;

if (!methodHandle.equals(that.methodHandle)) {
if (!methodName.equals(that.methodName)) {
return false;
}

Expand Down

0 comments on commit 0ec733c

Please sign in to comment.