Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed Jun 20, 2024
1 parent 734277e commit fa5f4aa
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions src/main/java/net/lenni0451/reflect/accessor/MethodAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static <I> I makeInvoker(@Nonnull final Class<I> invokerClass, final Obje
* @param <R> The return type
* @return The invoker instance implementation
*/
public static <R> Function<Object[], R> makeArrayInvoker(@Nonnull final Object instance, @Nonnull final Method method) {
public static <R> Function<Object[], R> 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
Expand All @@ -107,19 +107,7 @@ public static <R> Function<Object[], R> 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 {
Expand Down Expand Up @@ -200,19 +188,7 @@ public static <I, R> BiFunction<I, Object[], R> 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 {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fa5f4aa

Please sign in to comment.