<commit_before>package nl.dykam.dev.reutil;

import nl.dykam.dev.reutil.events.AutoEventHandler;
import nl.dykam.dev.reutil.events.Bind;
import org.bukkit.Bukkit;
import org.bukkit.event.*;
import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin;

import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.util.*;

public class ReUtil {
    public static void registerEvents(Listener listener, Plugin plugin) {
        Set<Method> methods = new HashSet<>();
        methods.addAll(Arrays.asList(listener.getClass().getDeclaredMethods()));
        for (Method method : methods) {
            tryRegisterEvent(listener, method, plugin);
        }
    }

    private static void tryRegisterEvent(Listener listener, Method method, Plugin plugin) {
        AutoEventHandler eventHandler = method.getAnnotation(AutoEventHandler.class);
        if (eventHandler == null) return;

        if(method.isSynthetic()) {
            return;
        }
        if(!method.isAccessible()) {
            method.setAccessible(true);
        }

        final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        final Class<?>[] parameterTypes = method.getParameterTypes();
        if(parameterTypes.length == 0 || !Event.class.isAssignableFrom(parameterTypes[0])) {
            Bukkit.getLogger().severe(plugin.getDescription().getFullName() + " attempted to register an invalid AutoEventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass());
            return;
        } else if(parameterTypes.length == 1) {
            Bukkit.getLogger().warning(plugin.getDescription().getFullName() + " attempted to register an EventHandler method signature \"" + method.toGenericString() + "\" as AutoEventHandler in " + listener.getClass());
        }

        final Class<? extends Event> eventType = parameterTypes[0].asSubclass(Event.class);
        final Class<? extends Listener> listenerType = listener.getClass().asSubclass(Listener.class);

        final MethodHandle mainHandle;
        try {
            mainHandle = MethodHandles.lookup().unreflect(method);
        } catch (IllegalAccessException e) {
            return;
        }

        List<MethodHandle> handleList = new ArrayList<>();
        for(int i = 1; i < parameterTypes.length; i++) {
            Class<?> type = parameterTypes[i];
            Bind bind = null;
            for (Annotation annotation : parameterAnnotations[i]) {
                if(annotation instanceof Bind) {
                    bind = (Bind) annotation;
                    break;
                }
            }
            if(bind == null) {
                Bukkit.getLogger().severe(plugin.getDescription().getFullName() + " attempted to register an invalid AutoEventHandler method signature \"" + method.toGenericString() + "\" missing in " + listener.getClass()
                        + ": missing @Bind on parameter " + i);
                return;
            }

            MethodHandle handle = getMethod(eventType, type, bind.value());
            if(handle == null) {
                Bukkit.getLogger().severe(plugin.getDescription().getFullName() + " attempted to register an invalid AutoEventHandler method signature \"" + method.toGenericString() + "\" missing in " + listener.getClass()
                        + ": no equivalent getter found for @Bind " + bind.value());
                return;

            }
            handleList.add(handle);
        }
        final MethodHandle[] handles = handleList.toArray(new MethodHandle[handleList.size()]);

        EventExecutor executor = new EventExecutor() {
            @Override
            public void execute(Listener listener, Event event) throws EventException {
                Object[] params = new Object[handles.length + 2];
                params[0] = listener;
                params[1] = event;
                for(int i = 0; i < handles.length; i++) {
                    try {
                        Object param = handles[i].invokeWithArguments(event);
                        if(!parameterTypes[i + 1].isInstance(param))
                            return; // Incorrect type, ignore event
                        params[i + 2] = param;
                    } catch (Throwable throwable) {
                        throw new EventException(throwable);
                    }
                }
                try {<commit_msg>Fix an Event inheritance bug on invocation<commit_after>package nl.dykam.dev.reutil;

import nl.dykam.dev.reutil.events.AutoEventHandler;
import nl.dykam.dev.reutil.events.Bind;
import org.bukkit.Bukkit;
import org.bukkit.event.*;
import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin;

import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.util.*;

public class ReUtil {
    public static void registerEvents(Listener listener, Plugin plugin) {
        Set<Method> methods = new HashSet<>();
        methods.addAll(Arrays.asList(listener.getClass().getDeclaredMethods()));
        for (Method method : methods) {
            tryRegisterEvent(listener, method, plugin);
        }
    }

    private static void tryRegisterEvent(Listener listener, Method method, Plugin plugin) {
        AutoEventHandler eventHandler = method.getAnnotation(AutoEventHandler.class);
        if (eventHandler == null) return;

        if(method.isSynthetic()) {
            return;
        }
        if(!method.isAccessible()) {
            method.setAccessible(true);
        }

        final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        final Class<?>[] parameterTypes = method.getParameterTypes();
        if(parameterTypes.length == 0 || !Event.class.isAssignableFrom(parameterTypes[0])) {
            Bukkit.getLogger().severe(plugin.getDescription().getFullName() + " attempted to register an invalid AutoEventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass());
            return;
        } else if(parameterTypes.length == 1) {
            Bukkit.getLogger().warning(plugin.getDescription().getFullName() + " attempted to register an EventHandler method signature \"" + method.toGenericString() + "\" as AutoEventHandler in " + listener.getClass());
        }

        final Class<? extends Event> eventType = parameterTypes[0].asSubclass(Event.class);
        final Class<? extends Listener> listenerType = listener.getClass().asSubclass(Listener.class);

        final MethodHandle mainHandle;
        try {
            mainHandle = MethodHandles.lookup().unreflect(method);
        } catch (IllegalAccessException e) {
            return;
        }

        List<MethodHandle> handleList = new ArrayList<>();
        for(int i = 1; i < parameterTypes.length; i++) {
            Class<?> type = parameterTypes[i];
            Bind bind = null;
            for (Annotation annotation : parameterAnnotations[i]) {
                if(annotation instanceof Bind) {
                    bind = (Bind) annotation;
                    break;
                }
            }
            if(bind == null) {
                Bukkit.getLogger().severe(plugin.getDescription().getFullName() + " attempted to register an invalid AutoEventHandler method signature \"" + method.toGenericString() + "\" missing in " + listener.getClass()
                        + ": missing @Bind on parameter " + i);
                return;
            }

            MethodHandle handle = getMethod(eventType, type, bind.value());
            if(handle == null) {
                Bukkit.getLogger().severe(plugin.getDescription().getFullName() + " attempted to register an invalid AutoEventHandler method signature \"" + method.toGenericString() + "\" missing in " + listener.getClass()
                        + ": no equivalent getter found for @Bind " + bind.value());
                return;

            }
            handleList.add(handle);
        }
        final MethodHandle[] handles = handleList.toArray(new MethodHandle[handleList.size()]);

        EventExecutor executor = new EventExecutor() {
            @Override
            public void execute(Listener listener, Event event) throws EventException {
                if (!eventType.isAssignableFrom(event.getClass())) {
                    return;
                }
                Object[] params = new Object[handles.length + 2];
                params[0] = listener;
                params[1] = event;
                for(int i = 0; i < handles.length; i++) {
                    try {
                        Object param = handles[i].invokeWithArguments(event);
                        if(!parameterTypes[i + 1].isInstance(param))
                            return; // Incorrect type, ignore event
                        params[i + 2] = param;
                    } catch (Throwable throwable) {
                        throw new EventException(throwable);
                    }
                }
                try {
{'fraction_non_alphanumeric': 0.11113537117903931, 'fraction_numerical': 0.00240174672489083, 'mean_word_length': 2.1742896742896742, 'pattern_counts': {'":': 4, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4617871', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2239, 'n_words': 527}