Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit 8bfe22b

Browse files
committed
Tweaks
- Actually commit toString this time - Remove proxy reuse until a non memory leak version of it can be created - Add a static only proxy getter so users don't have to provide an object to get to static methods
1 parent 3188c49 commit 8bfe22b

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

src/main/java/me/theminecoder/minecraft/nmsproxy/NMSProxy.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
*/
66
public interface NMSProxy {
77

8+
/**
9+
* @return An static only compatible version of the proxy.
10+
*/
11+
NMSProxy getStaticProxyObject();
12+
13+
/**
14+
* @return The original object used to create the proxy
15+
*/
816
Object getProxyHandle();
917

1018
}

src/main/java/me/theminecoder/minecraft/nmsproxy/NMSStaticProxy.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/me/theminecoder/minecraft/nmsproxy/proxy/NMSProxyInvocationHandler.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
3131
return handle;
3232
}
3333

34+
if (method.getName().equals("getStaticProxyObject")) {
35+
if (handle == null) {
36+
return this;
37+
}
38+
39+
return proxyProvider.getStaticNMSObject((Class<? extends NMSProxy>) proxy.getClass().getInterfaces()[0]);
40+
}
41+
42+
if (handle == null && method.getAnnotation(NMSStatic.class) == null) {
43+
throw new IllegalStateException("Proxy method \""+method+"\" is attempting to call to instance method/field on a static proxy. Please mark the proxy method with @NMSStatic");
44+
}
45+
3446
if (method.getAnnotation(NMSMethod.class) != null || method.getDeclaringClass() == Object.class) {
3547
NMSMethod nmsMethodAnnotation = method.getAnnotation(NMSMethod.class);
3648

@@ -46,6 +58,10 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
4658
return null;
4759
}
4860

61+
if (method.getName().equals("toString") && method.getParameterCount() == 0) {
62+
return "Proxy|" + proxy.getClass().getInterfaces()[0].getCanonicalName() + "(" + returnObject + ")";
63+
}
64+
4965
if (NMSProxy.class.isAssignableFrom(method.getReturnType())) {
5066
returnObject = proxyProvider.getNMSObject((Class<? extends NMSProxy>) method.getReturnType(), returnObject);
5167
}

src/main/java/me/theminecoder/minecraft/nmsproxy/proxy/NMSProxyProvider.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.reflect.Proxy;
2121
import java.util.Arrays;
2222
import java.util.Map;
23-
import java.util.WeakHashMap;
2423
import java.util.stream.Collectors;
2524

2625
/**
@@ -34,8 +33,6 @@ public final class NMSProxyProvider {
3433
private final Map<Class, DynamicType.Loaded> proxyToNMSSubclassMap = Maps.newHashMap();
3534
private NMSProxyInvocationMapper invocationMapper = new NMSProxyInvocationMapper(proxyToNMSClassMap);
3635

37-
private Map<Object, NMSProxy> proxyInstances = new WeakHashMap<>();
38-
3936
private NMSProxyProvider() {
4037
}
4138

@@ -68,20 +65,20 @@ private void registerNMSClasses(Class<? extends NMSProxy> clazz) {
6865
proxyToNMSClassMap.put(clazz, nmsClass);
6966
}
7067

71-
public <T extends NMSProxy> T getNMSObject(Class<T> clazz, Object object) {
68+
public <T extends NMSProxy> T getStaticNMSObject(Class<T> clazz) {
7269
registerNMSClasses(clazz);
7370

74-
T proxyObject = (T) proxyInstances.get(object);
71+
return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new NMSProxyInvocationHandler(null, invocationMapper, this));
72+
}
7573

76-
if (proxyObject == null) {
77-
if (!proxyToNMSClassMap.get(clazz).isAssignableFrom(object.getClass())) {
78-
throw new IllegalStateException("Object is not of type " + proxyToNMSClassMap.get(clazz).getCanonicalName() + "!");
79-
}
80-
proxyObject = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new NMSProxyInvocationHandler(object, invocationMapper, this));
81-
proxyInstances.put(object, proxyObject);
74+
public <T extends NMSProxy> T getNMSObject(Class<T> clazz, Object object) {
75+
registerNMSClasses(clazz);
76+
77+
if (!proxyToNMSClassMap.get(clazz).isAssignableFrom(object.getClass())) {
78+
throw new IllegalStateException("Object is not of type " + proxyToNMSClassMap.get(clazz).getCanonicalName() + "!");
8279
}
8380

84-
return proxyObject;
81+
return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new NMSProxyInvocationHandler(object, invocationMapper, this));
8582
}
8683

8784
public <T extends NMSProxy> T constructNMSObject(Class<T> clazz, Object... params) throws ReflectiveOperationException {

0 commit comments

Comments
 (0)