55
66package scala .tools .nsc .backend .jvm
77
8- import scala .tools .asm .tree .{AbstractInsnNode , ClassNode , MethodNode }
9- import java .io .PrintWriter
8+ import scala .tools .asm .tree .{InsnList , AbstractInsnNode , ClassNode , MethodNode }
9+ import java .io .{ StringWriter , PrintWriter }
1010import scala .tools .asm .util .{TraceClassVisitor , TraceMethodVisitor , Textifier }
1111import scala .tools .asm .ClassReader
12+ import scala .collection .convert .decorateAsScala ._
1213
1314object AsmUtils {
1415
@@ -36,19 +37,12 @@ object AsmUtils {
3637
3738 def traceMethod (mnode : MethodNode ): Unit = {
3839 println(s " Bytecode for method ${mnode.name}" )
39- val p = new Textifier
40- val tracer = new TraceMethodVisitor (p)
41- mnode.accept(tracer)
42- val w = new PrintWriter (System .out)
43- p.print(w)
44- w.flush()
40+ println(textify(mnode))
4541 }
4642
4743 def traceClass (cnode : ClassNode ): Unit = {
4844 println(s " Bytecode for class ${cnode.name}" )
49- val w = new PrintWriter (System .out)
50- cnode.accept(new TraceClassVisitor (w))
51- w.flush()
45+ println(textify(cnode))
5246 }
5347
5448 def traceClass (bytes : Array [Byte ]): Unit = traceClass(readClass(bytes))
@@ -59,8 +53,56 @@ object AsmUtils {
5953 node
6054 }
6155
62- def instructionString (instruction : AbstractInsnNode ): String = instruction.getOpcode match {
63- case - 1 => instruction.toString
64- case op => scala.tools.asm.util.Printer .OPCODES (op)
56+ /**
57+ * Returns a human-readable representation of the cnode ClassNode.
58+ */
59+ def textify (cnode : ClassNode ): String = {
60+ val trace = new TraceClassVisitor (new PrintWriter (new StringWriter ))
61+ cnode.accept(trace)
62+ val sw = new StringWriter
63+ val pw = new PrintWriter (sw)
64+ trace.p.print(pw)
65+ sw.toString
6566 }
67+
68+ /**
69+ * Returns a human-readable representation of the code in the mnode MethodNode.
70+ */
71+ def textify (mnode : MethodNode ): String = {
72+ val trace = new TraceClassVisitor (new PrintWriter (new StringWriter ))
73+ mnode.accept(trace)
74+ val sw = new StringWriter
75+ val pw = new PrintWriter (sw)
76+ trace.p.print(pw)
77+ sw.toString
78+ }
79+
80+ /**
81+ * Returns a human-readable representation of the given instruction.
82+ */
83+ def textify (insn : AbstractInsnNode ): String = {
84+ val trace = new TraceMethodVisitor (new Textifier )
85+ insn.accept(trace)
86+ val sw = new StringWriter
87+ val pw = new PrintWriter (sw)
88+ trace.p.print(pw)
89+ sw.toString.trim
90+ }
91+
92+ /**
93+ * Returns a human-readable representation of the given instruction sequence.
94+ */
95+ def textify (insns : Iterator [AbstractInsnNode ]): String = {
96+ val trace = new TraceMethodVisitor (new Textifier )
97+ insns.foreach(_.accept(trace))
98+ val sw : StringWriter = new StringWriter
99+ val pw : PrintWriter = new PrintWriter (sw)
100+ trace.p.print(pw)
101+ sw.toString.trim
102+ }
103+
104+ /**
105+ * Returns a human-readable representation of the given instruction sequence.
106+ */
107+ def textify (insns : InsnList ): String = textify(insns.iterator().asScala)
66108}
0 commit comments