asmhelper stuff
@@ -2,15 +2,21 @@ package com.matt.forgehax.asm.utils;
import com.matt.forgehax.asm.utils.asmtype.ASMField;
import com.matt.forgehax.asm.utils.asmtype.ASMMethod;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
+import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.objectweb.asm.tree.*;
+import static org.objectweb.asm.Opcodes.*;
+
public class ASMHelper {
public static final Map<Class<?>, Class<?>> primitiveToWrapper =
new HashMap<Class<?>, Class<?>>();
@@ -194,6 +200,35 @@ public class ASMHelper {
field.getRuntimeDescriptor());
}
+ public static int addNewLocalVariable(MethodNode method, String name, String desc, LabelNode start, LabelNode end) {
+ Optional<LocalVariableNode> lastVar = method.localVariables.stream()
+ .max(Comparator.comparingInt(var -> var.index));
+ final int newIndex = lastVar
+ .map(var -> var.desc.matches("[JD]") ? var.index + 2 : var.index + 1)
+ .orElse(0);
+
+ LocalVariableNode variable = new LocalVariableNode(name, desc, null, start, end, newIndex);
+ method.localVariables.add(variable);
+
+ return newIndex;
+ }
+
+ // args should be type descriptors
+ public static InsnList newInstance(String name, String[] argTypes, @Nullable InsnList args) {
+ final String desc = Stream.of(argTypes)
+ .collect(Collectors.joining("", "(", ")V"));
+ return newInstance(name, desc, args);
+ }
+
+ public static InsnList newInstance(String name, String desc, @Nullable InsnList args) {
+ InsnList list = new InsnList();
+ list.add(new TypeInsnNode(NEW, name));
+ list.add(new InsnNode(DUP));
+ if (args != null) list.add(args);
+ list.add(new MethodInsnNode(INVOKESPECIAL, name, "<init>", desc, false));
+ return list;
+ }
+
public interface MagicOpcodes {
int NONE = -666;
}
