|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | +package net.minecraftforge.eventbus.internal; |
| 6 | + |
| 7 | +import net.minecraftforge.eventbus.api.EventListenerHelper; |
| 8 | +import net.minecraftforge.eventbus.api.IGenericEvent; |
| 9 | + |
| 10 | +import java.lang.invoke.MethodHandle; |
| 11 | +import java.lang.invoke.MethodHandles; |
| 12 | +import java.lang.invoke.MethodType; |
| 13 | +import java.lang.reflect.Modifier; |
| 14 | + |
| 15 | +public final class InternalUtils { |
| 16 | + private InternalUtils() {} |
| 17 | + |
| 18 | + private static final MethodHandle GET_PERMITTED_SUBCLASSES; |
| 19 | + static { |
| 20 | + var lookup = MethodHandles.lookup(); |
| 21 | + var methodType = MethodType.methodType(Class[].class); |
| 22 | + if (Runtime.version().feature() >= 17) { |
| 23 | + try { |
| 24 | + GET_PERMITTED_SUBCLASSES = lookup |
| 25 | + .findVirtual(Class.class, "getPermittedSubclasses", methodType) |
| 26 | + .asType(methodType.insertParameterTypes(0, Class.class)); |
| 27 | + } catch (Exception e) { |
| 28 | + throw new ExceptionInInitializerError("Failed to find Class#getPermittedSubclasses(Class): " + e); |
| 29 | + } |
| 30 | + } else { |
| 31 | + GET_PERMITTED_SUBCLASSES = MethodHandles.empty(methodType.insertParameterTypes(0, Class.class)); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static Class<?>[] getPermittedSubclasses(Class<?> clazz) { |
| 36 | + try { |
| 37 | + return (Class<?>[]) GET_PERMITTED_SUBCLASSES.invokeExact(clazz); |
| 38 | + } catch (Throwable e) { |
| 39 | + throw new RuntimeException(e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param eventType the event type to check |
| 45 | + * @return true if the given event type could be cancelled by a listener for a subclass of the given event type |
| 46 | + */ |
| 47 | + public static boolean couldBeCancelled(Class<?> eventType) { |
| 48 | + return couldBeCancelled(eventType, IGenericEvent.class.isAssignableFrom(eventType)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param eventType the event type to check |
| 53 | + * @param isGenericEvent true if the given event type is a generic event type |
| 54 | + * @return true if the given event type could be cancelled by a listener for a subclass of the given event type |
| 55 | + */ |
| 56 | + public static boolean couldBeCancelled(Class<?> eventType, boolean isGenericEvent) { |
| 57 | + if (isGenericEvent || EventListenerHelper.isCancelable(eventType)) return true; |
| 58 | + if (Modifier.isFinal(eventType.getModifiers())) return false; |
| 59 | + |
| 60 | + var permittedSubclasses = getPermittedSubclasses(eventType); |
| 61 | + if (permittedSubclasses == null || permittedSubclasses.length == 0) return true; |
| 62 | + |
| 63 | + for (var subclass : permittedSubclasses) |
| 64 | + if (couldBeCancelled(subclass)) |
| 65 | + return true; |
| 66 | + |
| 67 | + return false; |
| 68 | + } |
| 69 | +} |
0 commit comments