Skip to content

Commit feb72b8

Browse files
Update ASM EventExecutor generator patch to respect event handler return types. Fixes #7311 (#7317)
Co-authored-by: Jan Boerman <janboerman95@gmail.com>
1 parent 2a306f5 commit feb72b8

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

patches/api/0026-Use-ASM-for-event-executors.patch

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Subject: [PATCH] Use ASM for event executors.
66
Uses method handles for private or static methods.
77

88
diff --git a/build.gradle.kts b/build.gradle.kts
9-
index 0e8e827b5a05ac9423386eaea4188d132cc7f954..70d6503745942ea8637146369aeab8f9357c42d5 100644
9+
index 80fdd05dd593455ca89b66636ed30f1d9facf4ed..d8d459561cc75935136f8f9888ff27b45ad98f9e 100644
1010
--- a/build.gradle.kts
1111
+++ b/build.gradle.kts
1212
@@ -38,6 +38,9 @@ dependencies {
@@ -118,10 +118,10 @@ index 0000000000000000000000000000000000000000..c83672427324bd068ed52916f700b684
118118
+}
119119
diff --git a/src/main/java/com/destroystokyo/paper/event/executor/asm/ASMEventExecutorGenerator.java b/src/main/java/com/destroystokyo/paper/event/executor/asm/ASMEventExecutorGenerator.java
120120
new file mode 100644
121-
index 0000000000000000000000000000000000000000..b6e7d8ee8d903ebf975d60bec0e08603d9a49fdb
121+
index 0000000000000000000000000000000000000000..b8d5c13980858dc27fb5383726b7ebcaf14adcb8
122122
--- /dev/null
123123
+++ b/src/main/java/com/destroystokyo/paper/event/executor/asm/ASMEventExecutorGenerator.java
124-
@@ -0,0 +1,47 @@
124+
@@ -0,0 +1,51 @@
125125
+package com.destroystokyo.paper.event.executor.asm;
126126
+
127127
+import java.lang.reflect.Method;
@@ -147,14 +147,18 @@ index 0000000000000000000000000000000000000000..b6e7d8ee8d903ebf975d60bec0e08603
147147
+ methodGenerator.returnValue();
148148
+ methodGenerator.endMethod();
149149
+ // Generate the execute method
150-
+ methodGenerator = new GeneratorAdapter(writer.visitMethod(ACC_PUBLIC, "execute", "(Lorg/bukkit/event/Listener;Lorg/bukkit/event/Event;)V", null, null), ACC_PUBLIC, "execute", "(Lorg/bukkit/event/Listener;Lorg/bukkit/event/Listener;)V");;
150+
+ methodGenerator = new GeneratorAdapter(writer.visitMethod(ACC_PUBLIC, "execute", "(Lorg/bukkit/event/Listener;Lorg/bukkit/event/Event;)V", null, null), ACC_PUBLIC, "execute", "(Lorg/bukkit/event/Listener;Lorg/bukkit/event/Listener;)V");
151151
+ methodGenerator.loadArg(0);
152152
+ methodGenerator.checkCast(Type.getType(m.getDeclaringClass()));
153153
+ methodGenerator.loadArg(1);
154154
+ methodGenerator.checkCast(Type.getType(m.getParameterTypes()[0]));
155155
+ methodGenerator.visitMethodInsn(m.getDeclaringClass().isInterface() ? INVOKEINTERFACE : INVOKEVIRTUAL, Type.getInternalName(m.getDeclaringClass()), m.getName(), Type.getMethodDescriptor(m), m.getDeclaringClass().isInterface());
156-
+ if (m.getReturnType() != void.class) {
157-
+ methodGenerator.pop();
156+
+ // The only purpose of this switch statement is to generate the correct pop instruction, should the event handler method return something other than void.
157+
+ // Non-void event handlers will be unsupported in a future release.
158+
+ switch (Type.getType(m.getReturnType()).getSize()) {
159+
+ // case 0 is omitted because the only type that has size 0 is void - no pop instruction needed.
160+
+ case 1 -> methodGenerator.pop(); // handles reference types and most primitives
161+
+ case 2 -> methodGenerator.pop2(); // handles long and double
158162
+ }
159163
+ methodGenerator.returnValue();
160164
+ methodGenerator.endMethod();

patches/api/0357-Warn-on-strange-EventHandler-return-types.patch

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Subject: [PATCH] Warn on strange @EventHandler return types
55

66

77
diff --git a/src/main/java/org/bukkit/plugin/EventExecutor.java b/src/main/java/org/bukkit/plugin/EventExecutor.java
8-
index 9026e108ccd3a88aee1267ee275137befa646455..e5f10ed4312529c082acf7de44af3639f77c8b8e 100644
8+
index 9026e108ccd3a88aee1267ee275137befa646455..e1860322ae0f3c35097d16767628744034941749 100644
99
--- a/src/main/java/org/bukkit/plugin/EventExecutor.java
1010
+++ b/src/main/java/org/bukkit/plugin/EventExecutor.java
1111
@@ -51,6 +51,12 @@ public interface EventExecutor {
@@ -15,8 +15,8 @@ index 9026e108ccd3a88aee1267ee275137befa646455..e5f10ed4312529c082acf7de44af3639
1515
+ if (m.getReturnType() != Void.TYPE) {
1616
+ final org.bukkit.plugin.java.JavaPlugin plugin = org.bukkit.plugin.java.JavaPlugin.getProvidingPlugin(m.getDeclaringClass());
1717
+ org.bukkit.Bukkit.getLogger().warning("@EventHandler method " + m.getDeclaringClass().getName() + (Modifier.isStatic(m.getModifiers()) ? '.' : '#') + m.getName()
18-
+ + " returns non-void type " + m.getReturnType().getName() + ". This is an unsupported behavior which will be removed in a future version of Paper."
19-
+ + "This should be reported to the developers of " + plugin.getDescription().getFullName() + ", (" + String.join(",", plugin.getDescription().getAuthors()) + ')');
18+
+ + " returns non-void type " + m.getReturnType().getName() + ". This is unsupported behavior and will no longer work in a future version of Paper."
19+
+ + " This should be reported to the developers of " + plugin.getDescription().getFullName() + " (" + String.join(",", plugin.getDescription().getAuthors()) + ')');
2020
+ }
2121
if (Modifier.isStatic(m.getModifiers())) {
2222
return new StaticMethodHandleEventExecutor(eventClass, m);

0 commit comments

Comments
 (0)