|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Ampflower |
| 3 | + * |
| 4 | + * This software is subject to the terms of either the MIT or CC0-1.0 Licenses. |
| 5 | + * If a copy was not distributed with this file, you can obtain one at |
| 6 | + * https://github.com/Modflower/QuickBench/blob/trunk/LICENSE-MIT |
| 7 | + * https://github.com/Modflower/QuickBench/blob/trunk/LICENSE-CC0 |
| 8 | + * |
| 9 | + * Sources: |
| 10 | + * - https://github.com/Modflower/QuickBench |
| 11 | + * |
| 12 | + * SPDX-License-Identifier: MIT OR CC0-1.0 |
| 13 | + * |
| 14 | + * Additional details are outlined in LICENSE.md, which you can obtain at |
| 15 | + * https://github.com/Modflower/QuickBench/blob/trunk/LICENSE.md |
| 16 | + */ |
| 17 | + |
| 18 | +package tfar.fastbench.quickbench.internal; |
| 19 | + |
| 20 | +import net.fabricmc.loader.api.FabricLoader; |
| 21 | +import net.fabricmc.loader.api.MappingResolver; |
| 22 | + |
| 23 | +import java.lang.invoke.MethodHandle; |
| 24 | +import java.lang.invoke.MethodHandles; |
| 25 | +import java.lang.invoke.MethodType; |
| 26 | +import java.lang.reflect.Method; |
| 27 | +import java.lang.reflect.Modifier; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Ampflower |
| 31 | + * @since 4.3.2 |
| 32 | + **/ |
| 33 | +public final class Reflector { |
| 34 | + private static final String namespace = "intermediary"; |
| 35 | + |
| 36 | + private static final MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 37 | + private static final MappingResolver resolver = FabricLoader.getInstance().getMappingResolver(); |
| 38 | + private static final boolean production = namespace.equals(resolver.getCurrentRuntimeNamespace()); |
| 39 | + |
| 40 | + private static String unmap(Class<?> clazz) { |
| 41 | + if (clazz.isPrimitive()) { |
| 42 | + return clazz.descriptorString(); |
| 43 | + } |
| 44 | + return 'L' + unmapClassName(clazz).replace('.', '/') + ';'; |
| 45 | + } |
| 46 | + |
| 47 | + private static String unmapClassName(Class<?> clazz) { |
| 48 | + return resolver.unmapClassName(namespace, clazz.getName()); |
| 49 | + } |
| 50 | + |
| 51 | + private static boolean virtual(Method method, String reference, MethodType signature) { |
| 52 | + if (!signature(method, signature)) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + if (production) { |
| 56 | + return reference.equals(method.getName()); |
| 57 | + } |
| 58 | + |
| 59 | + final var parameters = method.getParameterTypes(); |
| 60 | + final var unmap = new String[method.getParameterCount()]; |
| 61 | + for (int i = 0; i < unmap.length; i++) { |
| 62 | + unmap[i] = unmap(parameters[i]); |
| 63 | + } |
| 64 | + |
| 65 | + return resolver.mapMethodName(namespace, |
| 66 | + unmapClassName(method.getDeclaringClass()), |
| 67 | + reference, |
| 68 | + '(' + String.join("", unmap) + ')' + unmap(method.getReturnType())) |
| 69 | + .equals(method.getName()); |
| 70 | + } |
| 71 | + |
| 72 | + private static boolean signature(Method method, MethodType signature) { |
| 73 | + if (signature.parameterCount() != method.getParameterCount()) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + if (!signature.returnType().equals(method.getReturnType())) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + final var parameters = method.getParameterTypes(); |
| 80 | + for (int i = 0; i < signature.parameterCount(); i++) { |
| 81 | + if (!parameters[i].isAssignableFrom(signature.parameterType(i))) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + public static MethodHandle virtual(Class<?> clazz, String reference, MethodType signature) { |
| 89 | + try { |
| 90 | + for (final var method : clazz.getMethods()) { |
| 91 | + if (Modifier.isStatic(method.getModifiers())) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + if (!virtual(method, reference, signature)) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + return lookup.unreflect(method); |
| 98 | + } |
| 99 | + } catch (IllegalAccessException e) { |
| 100 | + throw new AssertionError(e); |
| 101 | + } |
| 102 | + throw new AssertionError(clazz + " has no such method: " + reference + signature); |
| 103 | + } |
| 104 | +} |
0 commit comments