1
1
package ftbsc .lll .utils .debug ;
2
2
3
- import org .apache .logging .log4j .Logger ;
4
3
import org .objectweb .asm .tree .AbstractInsnNode ;
5
4
import org .objectweb .asm .tree .MethodNode ;
6
5
import org .objectweb .asm .util .Printer ;
12
11
import java .io .StringWriter ;
13
12
import java .nio .file .Files ;
14
13
import java .nio .file .Paths ;
14
+ import java .util .function .Consumer ;
15
15
16
16
/**
17
17
* A collection of static methods for debugging by printing the ASM bytecode.
@@ -30,33 +30,33 @@ public class BytecodePrinter {
30
30
private static final TraceMethodVisitor MP = new TraceMethodVisitor (PRINTER );
31
31
32
32
/**
33
- * Prints the bytecode of a method using System.out.print() .
33
+ * Logs the bytecode of a method on System.out.
34
34
* @param main the method to print
35
35
*/
36
- public static void printMethod (final MethodNode main ) {
37
- for (AbstractInsnNode i : main .instructions .toArray ())
38
- System .out .print (insnToString (i ));
36
+ public static void logMethod (MethodNode main ) {
37
+ logMethod (main , System .out ::println );
39
38
}
40
39
41
40
/**
42
- * Logs the bytecode of a method using the ASM logger .
41
+ * Logs the bytecode of a method into a given sink .
43
42
* @param main the method to print
44
- * @param logger the Log4j {@link Logger} to print it with
43
+ * @param logFn a consumer for the string, typically a logging function
44
+ * @since 0.6.0
45
45
*/
46
- public static void logMethod (final MethodNode main , final Logger logger ) {
47
- for (AbstractInsnNode i : main .instructions .toArray ())
48
- logger .debug (insnToString (i ));
46
+ public static void logMethod (MethodNode main , Consumer <String > logFn ) {
47
+ for (AbstractInsnNode i : main .instructions .toArray ()) {
48
+ logFn .accept (insnToString (i ));
49
+ }
49
50
}
50
51
51
52
/**
52
53
* Logs the bytecode of a method to a file.
53
54
* @param main the method to print
54
55
* @param path the file to log it to
55
56
*/
56
- public static void logMethod (final MethodNode main , String path ) {
57
+ public static void logMethod (MethodNode main , String path ) {
57
58
StringBuilder out = new StringBuilder ();
58
- for (AbstractInsnNode i : main .instructions .toArray ())
59
- out .append (insnToString (i ));
59
+ logMethod (main , out ::append );
60
60
try {
61
61
Files .write (Paths .get (path ), out .toString ().getBytes ());
62
62
} catch (IOException e ) {
0 commit comments