blob: 63e8aeffbea5893745020b5596a5ed41025d5359 (
plain)
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
41
42
43
44
45
46
47
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));
}
}
|