From 4ef7f45ae71bd03ff86aaf88da371e9e97d02f0b Mon Sep 17 00:00:00 2001 From: shedaniel Date: Fri, 6 Aug 2021 17:09:05 +0800 Subject: [PATCH] Switch to MethodHandlers to avoid wrapping exceptions with InvocationTargetException due to reflection --- .../dev/architectury/event/EventFactory.java | 16 +++++++++++----- .../particles/test_particle.json | 6 +++++- .../{particles => particle}/test_particle.png | Bin 3 files changed, 16 insertions(+), 6 deletions(-) rename testmod-common/src/main/resources/assets/architectury_test/textures/{particles => particle}/test_particle.png (100%) diff --git a/common/src/main/java/dev/architectury/event/EventFactory.java b/common/src/main/java/dev/architectury/event/EventFactory.java index b2fd0769..db79359f 100644 --- a/common/src/main/java/dev/architectury/event/EventFactory.java +++ b/common/src/main/java/dev/architectury/event/EventFactory.java @@ -26,6 +26,7 @@ import dev.architectury.injectables.annotations.ExpectPlatform; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; +import java.lang.invoke.MethodHandles; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; @@ -48,13 +49,18 @@ public final class EventFactory { return createLoop((Class) typeGetter.getClass().getComponentType()); } + private static R invokeMethod(T listener, Method method, Object[] args) throws Throwable { + return (R) MethodHandles.lookup().unreflect(method) + .bindTo(listener).invokeWithArguments(args); + } + @SuppressWarnings("UnstableApiUsage") public static Event createLoop(Class clazz) { return of(listeners -> (T) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{clazz}, new AbstractInvocationHandler() { @Override protected Object handleInvocation(@NotNull Object proxy, @NotNull Method method, Object @NotNull [] args) throws Throwable { for (var listener : listeners) { - method.invoke(listener, args); + invokeMethod(listener, method, args); } return null; } @@ -73,7 +79,7 @@ public final class EventFactory { @Override protected Object handleInvocation(@NotNull Object proxy, @NotNull Method method, Object @NotNull [] args) throws Throwable { for (var listener : listeners) { - var result = (EventResult) Objects.requireNonNull(method.invoke(listener, args)); + var result = (EventResult) Objects.requireNonNull(invokeMethod(listener, method, args)); if (result.interruptsFurtherEvaluation()) { return result; } @@ -95,7 +101,7 @@ public final class EventFactory { @Override protected Object handleInvocation(@NotNull Object proxy, @NotNull Method method, Object @NotNull [] args) throws Throwable { for (var listener : listeners) { - var result = (CompoundEventResult) Objects.requireNonNull(method.invoke(listener, args)); + var result = (CompoundEventResult) Objects.requireNonNull(invokeMethod(listener, method, args)); if (result.interruptsFurtherEvaluation()) { return result; } @@ -117,7 +123,7 @@ public final class EventFactory { @Override protected Object handleInvocation(@NotNull Object proxy, @NotNull Method method, Object @NotNull [] args) throws Throwable { for (var listener : listeners) { - method.invoke(listener, args); + invokeMethod(listener, method, args); } return null; } @@ -144,7 +150,7 @@ public final class EventFactory { @Override protected Object handleInvocation(@NotNull Object proxy, @NotNull Method method, Object @NotNull [] args) throws Throwable { for (var listener : listeners) { - var result = (EventResult) method.invoke(listener, args); + var result = (EventResult) invokeMethod(listener, method, args); if (result.interruptsFurtherEvaluation()) { return result; } diff --git a/testmod-common/src/main/resources/assets/architectury_test/particles/test_particle.json b/testmod-common/src/main/resources/assets/architectury_test/particles/test_particle.json index 9e26dfee..d02c5d9c 100644 --- a/testmod-common/src/main/resources/assets/architectury_test/particles/test_particle.json +++ b/testmod-common/src/main/resources/assets/architectury_test/particles/test_particle.json @@ -1 +1,5 @@ -{} \ No newline at end of file +{ + "textures": [ + "architectury_test:test_particle" + ] +} \ No newline at end of file diff --git a/testmod-common/src/main/resources/assets/architectury_test/textures/particles/test_particle.png b/testmod-common/src/main/resources/assets/architectury_test/textures/particle/test_particle.png similarity index 100% rename from testmod-common/src/main/resources/assets/architectury_test/textures/particles/test_particle.png rename to testmod-common/src/main/resources/assets/architectury_test/textures/particle/test_particle.png