SPIGOT-893: Fix bridge method registering generic listeners twice

By: Jonas Konrad <me@yawk.at>
This commit is contained in:
Bukkit/Spigot 2015-05-18 11:14:47 +02:00
parent fa63b3d7f4
commit 745401c988
2 changed files with 54 additions and 0 deletions

View file

@ -250,6 +250,11 @@ public final class JavaPluginLoader implements PluginLoader {
for (final Method method : methods) {
final EventHandler eh = method.getAnnotation(EventHandler.class);
if (eh == null) continue;
// Do not register bridge or synthetic methods to avoid event duplication
// Fixes SPIGOT-893
if (method.isBridge() || method.isSynthetic()) {
continue;
}
final Class<?> checkClass;
if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) {
plugin.getLogger().severe(plugin.getDescription().getFullName() + " attempted to register an invalid EventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass());

View file

@ -0,0 +1,49 @@
package org.bukkit.event;
import org.bukkit.TestServer;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.SimplePluginManager;
import org.bukkit.plugin.TestPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.junit.Assert;
import org.junit.Test;
public class SyntheticEventTest {
@SuppressWarnings("deprecation")
@Test
public void test() {
final JavaPluginLoader loader = new JavaPluginLoader(TestServer.getInstance());
TestPlugin plugin = new TestPlugin(getClass().getName()) {
@Override
public PluginLoader getPluginLoader() {
return loader;
}
};
SimplePluginManager pluginManager = new SimplePluginManager(TestServer.getInstance(), null);
TestEvent event = new TestEvent(false);
Impl impl = new Impl();
pluginManager.registerEvents(impl, plugin);
pluginManager.callEvent(event);
Assert.assertEquals(1, impl.callCount);
}
public static abstract class Base<E extends Event> implements Listener {
int callCount = 0;
public void accept(E evt) {
System.out.println("Invk " + evt);
callCount++;
}
}
public static class Impl extends Base<TestEvent> {
@Override
@EventHandler
public void accept(TestEvent evt) {
super.accept(evt);
}
}
}