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

Commit fbcba92

Browse files
committed
Fix default methods
1 parent 76508c3 commit fbcba92

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import me.theminecoder.minecraft.nmsproxy.annotations.NMSMethod;
66
import me.theminecoder.minecraft.nmsproxy.annotations.NMSStatic;
77

8+
import java.lang.invoke.MethodHandles;
9+
import java.lang.reflect.Constructor;
810
import java.lang.reflect.Field;
911
import java.lang.reflect.InvocationHandler;
1012
import java.lang.reflect.Method;
@@ -15,6 +17,8 @@
1517
*/
1618
public class NMSProxyInvocationHandler implements InvocationHandler {
1719

20+
private static Constructor<MethodHandles.Lookup> methodLookupConstructor;
21+
1822
private final Object handle;
1923
private final NMSProxyInvocationMapper invocationMapper;
2024
private final NMSProxyProvider proxyProvider;
@@ -39,8 +43,20 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
3943
return proxyProvider.getStaticNMSObject((Class<? extends NMSProxy>) proxy.getClass().getInterfaces()[0]);
4044
}
4145

46+
if (method.isDefault()) {
47+
final Class<?> declaringClass = method.getDeclaringClass();
48+
if (methodLookupConstructor == null) {
49+
methodLookupConstructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
50+
methodLookupConstructor.setAccessible(true);
51+
}
52+
return methodLookupConstructor.newInstance(declaringClass, -1) //Trusted Flag
53+
.unreflectSpecial(method, declaringClass)
54+
.bindTo(proxy)
55+
.invokeWithArguments(args);
56+
}
57+
4258
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");
59+
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");
4460
}
4561

4662
if (method.getAnnotation(NMSMethod.class) != null || method.getDeclaringClass() == Object.class) {

0 commit comments

Comments
 (0)