From 1216812c8a78a423a29ecb941dc66be50eb33496 Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 27 Aug 2019 17:28:15 +0100 Subject: [PATCH] Add InsnNodeUtils.toString() method Useful for debugging. --- .../java/dev/openrs2/asm/InsnNodeUtils.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java b/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java index 5654f798..e6ddd1f5 100644 --- a/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java +++ b/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java @@ -1,5 +1,9 @@ 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 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.IntInsnNode; import org.objectweb.asm.tree.LdcInsnNode; +import org.objectweb.asm.util.Textifier; +import org.objectweb.asm.util.TraceMethodVisitor; public final class InsnNodeUtils { public static AbstractInsnNode nextReal(AbstractInsnNode insn) { @@ -343,6 +349,23 @@ public final class InsnNodeUtils { 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() { /* empty */ }