Skip to content

Commit e0e273a

Browse files
committed
Fix generated event handlers for events on interfaces.
1 parent 913fb17 commit e0e273a

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
package net.minecraftforge.eventbus.test.general;
7+
8+
import java.util.function.Consumer;
9+
import java.util.function.Supplier;
10+
11+
import net.minecraftforge.eventbus.api.BusBuilder;
12+
import net.minecraftforge.eventbus.api.Event;
13+
import net.minecraftforge.eventbus.api.IEventBus;
14+
import net.minecraftforge.eventbus.api.SubscribeEvent;
15+
import net.minecraftforge.eventbus.test.ITestHandler;
16+
17+
import static org.junit.jupiter.api.Assertions.*;
18+
19+
public class InterfaceEventHandler implements ITestHandler {
20+
private static boolean hit = false;
21+
22+
public InterfaceEventHandler(boolean hasTransformer) {
23+
}
24+
25+
@Override
26+
public void test(Consumer<Class<?>> validator, Supplier<BusBuilder> builder) {
27+
var bus = builder.get().build();
28+
assertDoesNotThrow(() -> bus.register(STATIC.class));
29+
testCall(bus, true, "STATIC");
30+
assertDoesNotThrow(() -> bus.register(new INSTANCE() {}));
31+
testCall(bus, true, "STATIC");
32+
}
33+
34+
private void testCall(IEventBus bus, boolean expected, String name) {
35+
hit = false;
36+
bus.post(new Event());
37+
assertEquals(expected, hit, name + " did not behave correctly");
38+
}
39+
40+
public interface STATIC {
41+
@SubscribeEvent
42+
static void handler(Event e) {
43+
hit = true;
44+
}
45+
}
46+
47+
public interface INSTANCE {
48+
@SubscribeEvent
49+
default void handler(Event e) {
50+
hit = true;
51+
}
52+
}
53+
}

src/main/java/net/minecraftforge/eventbus/ClassLoaderFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected static void transformNode(String name, Method callback, ClassNode targ
5252
MethodVisitor mv;
5353

5454
boolean isStatic = Modifier.isStatic(callback.getModifiers());
55+
boolean isInterface = Modifier.isInterface(callback.getDeclaringClass().getModifiers());
5556
String desc = name.replace('.', '/');
5657
String instType = Type.getInternalName(callback.getDeclaringClass());
5758
String eventType = Type.getInternalName(callback.getParameterTypes()[0]);
@@ -95,7 +96,14 @@ protected static void transformNode(String name, Method callback, ClassNode targ
9596
}
9697
mv.visitVarInsn(ALOAD, 1);
9798
mv.visitTypeInsn(CHECKCAST, eventType);
98-
mv.visitMethodInsn(isStatic ? INVOKESTATIC : INVOKEVIRTUAL, instType, callback.getName(), Type.getMethodDescriptor(callback), false);
99+
100+
var opcode = INVOKEVIRTUAL;
101+
if (isStatic)
102+
opcode = INVOKESTATIC;
103+
else if (isInterface)
104+
opcode = INVOKEINTERFACE;
105+
106+
mv.visitMethodInsn(opcode, instType, callback.getName(), Type.getMethodDescriptor(callback), isInterface);
99107
mv.visitInsn(RETURN);
100108
mv.visitMaxs(2, 2);
101109
mv.visitEnd();

0 commit comments

Comments
 (0)