Support asynchronous events; Addresses BUKKIT-1212

By: Wesley Wolfe <weswolf@aol.com>
This commit is contained in:
Bukkit/Spigot
2012-06-13 21:28:13 -05:00
parent ca71171e8c
commit ad7045596d
10 changed files with 300 additions and 27 deletions

View File

@@ -0,0 +1,48 @@
package org.bukkit;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
public class TestServer implements InvocationHandler {
private static interface MethodHandler {
Object handle(TestServer server, Object[] args);
}
private static final Constructor<? extends Server> constructor;
private static final HashMap<Method, MethodHandler> methods = new HashMap<Method, MethodHandler>();
static {
try {
methods.put(Server.class.getMethod("isPrimaryThread"),
new MethodHandler() {
public Object handle(TestServer server, Object[] args) {
return Thread.currentThread().equals(server.creatingThread);
}
});
constructor = Proxy.getProxyClass(Server.class.getClassLoader(), Server.class).asSubclass(Server.class).getConstructor(InvocationHandler.class);
} catch (Throwable t) {
throw new Error(t);
}
}
private Thread creatingThread = Thread.currentThread();
private TestServer() {};
public static Server getInstance() {
try {
return constructor.newInstance(new TestServer());
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public Object invoke(Object proxy, Method method, Object[] args) {
MethodHandler handler = methods.get(method);
if (handler != null) {
return handler.handle(this, args);
}
throw new UnsupportedOperationException(String.valueOf(method));
}
}