Skip to content

Commit e19648f

Browse files
committed
fix: proper private interface event handler methods deduplication
1 parent 129f7c5 commit e19648f

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

server/src/main/java/org/allaymc/server/utils/ReflectionUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ private static void collectAllMethods(Class<?> type, Map<String, Method> methods
6161
}
6262

6363
public static String buildMethodSignature(Method method) {
64-
return method.getName() + Arrays.toString(method.getParameterTypes());
64+
var signature = method.getName() + Arrays.toString(method.getParameterTypes());
65+
66+
if (method.getDeclaringClass().isInterface() && Modifier.isPrivate(method.getModifiers())) {
67+
return method.getDeclaringClass().getName() + "#" + signature;
68+
}
69+
70+
return signature;
6571
}
6672

6773
public static List<Method> getAllStaticVoidParameterlessMethods(Class<?> clazz) {
@@ -115,4 +121,3 @@ public static <T, U> BiMap<T, U> mapStaticFields(Class<?> classA, Class<?> class
115121
return result;
116122
}
117123
}
118-

server/src/test/java/org/allaymc/server/utils/ReflectionUtilsTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ void testGetAllMethodsIncludesInterfaceDefaultMethods() {
3535
assertTrue(methods.stream().anyMatch(m -> m.getName().equals("defaultMethod")));
3636
}
3737

38+
@Test
39+
void testGetAllMethodsDoesNotDeduplicatePrivateInterfaceMethodsAcrossInterfaces() {
40+
var methods = ReflectionUtils.getAllMethods(F.class);
41+
assertEquals(2, methods.stream().filter(m -> m.getName().equals("helper")).count());
42+
}
43+
3844
@Test
3945
void testMapStaticFields() {
4046
var map = ReflectionUtils.mapStaticFields(EnumA.class, EnumB.class);
@@ -74,13 +80,23 @@ default String defaultMethod() {
7480
}
7581
}
7682

83+
interface PrivateMethodA {
84+
private void helper() {}
85+
}
86+
87+
interface PrivateMethodB {
88+
private void helper() {}
89+
}
90+
7791
static class C implements WithDefaultMethod {}
7892

93+
static class F implements PrivateMethodA, PrivateMethodB {}
94+
7995
enum EnumA {
8096
A, B, C
8197
}
8298

8399
enum EnumB {
84100
A, B
85101
}
86-
}
102+
}

0 commit comments

Comments
 (0)