Add InsnNodeUtils.toString() method

Useful for debugging.
bzip2
Graham 5 years ago
parent 4cfb9d9f69
commit 1216812c8a
  1. 23
      asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java

@ -1,5 +1,9 @@
package dev.openrs2.asm; package dev.openrs2.asm;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.util.ArrayList; import java.util.ArrayList;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
@ -8,6 +12,8 @@ import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode; import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.IntInsnNode; import org.objectweb.asm.tree.IntInsnNode;
import org.objectweb.asm.tree.LdcInsnNode; import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.util.TraceMethodVisitor;
public final class InsnNodeUtils { public final class InsnNodeUtils {
public static AbstractInsnNode nextReal(AbstractInsnNode insn) { public static AbstractInsnNode nextReal(AbstractInsnNode insn) {
@ -343,6 +349,23 @@ public final class InsnNodeUtils {
return replaceSimpleExpression(list, last, null); return replaceSimpleExpression(list, last, null);
} }
public static String toString(AbstractInsnNode insn) {
var printer = new Textifier();
var visitor = new TraceMethodVisitor(printer);
insn.accept(visitor);
try (
var stringWriter = new StringWriter();
var printWriter = new PrintWriter(stringWriter)
) {
printer.print(printWriter);
return stringWriter.toString().trim();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
private InsnNodeUtils() { private InsnNodeUtils() {
/* empty */ /* empty */
} }

Loading…
Cancel
Save