diff --git a/src/main/java/net/lenni0451/reflect/accessor/MethodAccessor.java b/src/main/java/net/lenni0451/reflect/accessor/MethodAccessor.java index 7dcb25f..017b63d 100644 --- a/src/main/java/net/lenni0451/reflect/accessor/MethodAccessor.java +++ b/src/main/java/net/lenni0451/reflect/accessor/MethodAccessor.java @@ -91,7 +91,7 @@ public static I makeInvoker(@Nonnull final Class invokerClass, final Obje * @param The return type * @return The invoker instance implementation */ - public static Function makeArrayInvoker(@Nonnull final Object instance, @Nonnull final Method method) { + public static Function makeArrayInvoker(final Object instance, @Nonnull final Method method) { boolean staticMethod = Modifier.isStatic(method.getModifiers()); BuiltClass builtClass = BUILDER.class_(BUILDER.opcode("ACC_SUPER", "ACC_FINAL", "ACC_SYNTHETIC"), slash(method.getDeclaringClass()) + "$ArrayMethodInvoker", null, slash(Object.class), new String[]{slash(Function.class)}, cb -> { //Disable the inspection because the instance parameter can be null. Just invoking getClass() here would throw an exception @@ -107,19 +107,7 @@ public static Function makeArrayInvoker(@Nonnull final Object i .var(BUILDER.opcode("ALOAD"), 0) .field(BUILDER.opcode("GETFIELD"), cb.getName(), "instance", desc(instance.getClass())); } - mb - .var(BUILDER.opcode("ALOAD"), 1) - .type(BUILDER.opcode("CHECKCAST"), desc(Object[].class)) - .var(BUILDER.opcode("ASTORE"), 1); - for (int i = 0; i < method.getParameterCount(); i++) { - Class parameter = method.getParameterTypes()[i]; - mb - .var(BUILDER.opcode("ALOAD"), 1) - .intPush(BUILDER, i) - .insn(BUILDER.opcode("AALOAD")) - .type(BUILDER.opcode("CHECKCAST"), slash(boxed(parameter))) - .unbox(BUILDER, parameter); - } + pushArrayArgs(mb, method, 1); if (staticMethod) { mb.method(BUILDER.opcode("INVOKESTATIC"), methodClass, method.getName(), methodDesc, interfaceMethod); } else { @@ -200,19 +188,7 @@ public static BiFunction makeDynamicArrayInvoker(@Nonnull mb .var(BUILDER.opcode("ALOAD"), 1) .type(BUILDER.opcode("CHECKCAST"), slash(method.getDeclaringClass())); - mb - .var(BUILDER.opcode("ALOAD"), 2) - .type(BUILDER.opcode("CHECKCAST"), desc(Object[].class)) - .var(BUILDER.opcode("ASTORE"), 2); - for (int i = 0; i < method.getParameterCount(); i++) { - Class parameter = method.getParameterTypes()[i]; - mb - .var(BUILDER.opcode("ALOAD"), 2) - .intPush(BUILDER, i) - .insn(BUILDER.opcode("AALOAD")) - .type(BUILDER.opcode("CHECKCAST"), slash(boxed(parameter))) - .unbox(BUILDER, parameter); - } + pushArrayArgs(mb, method, 2); if (Modifier.isInterface(method.getDeclaringClass().getModifiers())) { mb.method(BUILDER.opcode("INVOKEINTERFACE"), slash(method.getDeclaringClass()), method.getName(), desc(method), true); } else { @@ -270,6 +246,22 @@ private static void pushArgs(final MethodBuilder mb, final Class[] supplied, } } + private static void pushArrayArgs(final MethodBuilder mb, final Method method, final int arrayIndex) { + mb + .var(BUILDER.opcode("ALOAD"), arrayIndex) + .type(BUILDER.opcode("CHECKCAST"), desc(Object[].class)) + .var(BUILDER.opcode("ASTORE"), arrayIndex); + for (int i = 0; i < method.getParameterCount(); i++) { + Class parameter = method.getParameterTypes()[i]; + mb + .var(BUILDER.opcode("ALOAD"), arrayIndex) + .intPush(BUILDER, i) + .insn(BUILDER.opcode("AALOAD")) + .type(BUILDER.opcode("CHECKCAST"), slash(boxed(parameter))) + .unbox(BUILDER, parameter); + } + } + private static Class[] prepend(final Class[] classes, final Class other) { Class[] newClasses = new Class[classes.length + 1]; newClasses[0] = other;