Java ASM系列:(035)TraceClassVisitor介绍
2021/7/4 11:24:27
本文主要是介绍Java ASM系列:(035)TraceClassVisitor介绍,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文属于[Java ASM系列一:Core API](https://blog.51cto.com/lsieun/2924583)当中的一篇。 `TraceClassVisitor` class extends the `ClassVisitor` class, and builds **a textual representation of the visited class**. ## 1. TraceClassVisitor类 ### 1.1 class info 第一个部分,`TraceClassVisitor`类继承自`ClassVisitor`类,而且有`final`修饰,因此不会存在子类。 ```java public final class TraceClassVisitor extends ClassVisitor { } ``` ### 1.2 fields 第二个部分,`TraceClassVisitor`类定义的字段有哪些。`TraceClassVisitor`类有两个重要的字段,一个是`PrintWriter printWriter`用于打印;另一个是`Printer p`将class转换成文字信息。 ```java public final class TraceClassVisitor extends ClassVisitor { private final PrintWriter printWriter; // 真正打印输出的类 public final Printer p; // 信息采集器 } ``` ### 1.3 constructors 第三个部分,`TraceClassVisitor`类定义的构造方法有哪些。 ```java public final class TraceClassVisitor extends ClassVisitor { public TraceClassVisitor(final PrintWriter printWriter) { this(null, printWriter); } public TraceClassVisitor(final ClassVisitor classVisitor, final PrintWriter printWriter) { this(classVisitor, new Textifier(), printWriter); } public TraceClassVisitor(final ClassVisitor classVisitor, final Printer printer, final PrintWriter printWriter) { super(Opcodes.ASM10_EXPERIMENTAL, classVisitor); this.printWriter = printWriter; this.p = printer; } } ``` ### 1.4 methods 第四个部分,`TraceClassVisitor`类定义的方法有哪些。对于`TraceClassVisitor`类的`visit()`、`visitField()`、`visitMethod()`和`visitEnd()`方法,会分别调用`Printer.visit()`、`Printer.visitField()`、`Printer.visitMethod()`和`Printer.visitClassEnd()`方法。 ```java public final class TraceClassVisitor extends ClassVisitor { @Override public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) { p.visit(version, access, name, signature, superName, interfaces); super.visit(version, access, name, signature, superName, interfaces); } @Override public FieldVisitor visitField(final int access, final String name, final String descriptor, final String signature, final Object value) { Printer fieldPrinter = p.visitField(access, name, descriptor, signature, value); return new TraceFieldVisitor(super.visitField(access, name, descriptor, signature, value), fieldPrinter); } @Override public MethodVisitor visitMethod(final int access, final String name, final String descriptor, final String signature, final String[] exceptions) { Printer methodPrinter = p.visitMethod(access, name, descriptor, signature, exceptions); return new TraceMethodVisitor(super.visitMethod(access, name, descriptor, signature, exceptions), methodPrinter); } @Override public void visitEnd() { p.visitClassEnd(); if (printWriter != null) { p.print(printWriter); // Printer和PrintWriter进行结合 printWriter.flush(); } super.visitEnd(); } } ``` ## 2. 如何使用TraceClassVisitor类 使用`TraceClassVisitor`类,很重点的一点就是选择`Printer`类的具体实现,可以选择`ASMifier`类,也可以选择`Textifier`类(默认): ```text boolean flag = true or false; Printer printer = flag ? new ASMifier() : new Textifier(); PrintWriter printWriter = new PrintWriter(System.out, true); TraceClassVisitor traceClassVisitor = new TraceClassVisitor(null, printer, printWriter); ``` ### 2.1 生成新的类 ```java import lsieun.utils.FileUtils; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.util.TraceClassVisitor; import java.io.PrintWriter; import static org.objectweb.asm.Opcodes.*; public class TraceClassVisitorExample01Generate { public static void main(String[] args) throws Exception { String relative_path = "sample/HelloWorld.class"; String filepath = FileUtils.getFilePath(relative_path); // (1) 生成byte[]内容 byte[] bytes = dump(); // (2) 保存byte[]到文件 FileUtils.writeBytes(filepath, bytes); } public static byte[] dump() throws Exception { // (1) 创建ClassWriter对象 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); PrintWriter printWriter = new PrintWriter(System.out); TraceClassVisitor cv = new TraceClassVisitor(cw, printWriter); // (2) 调用visitXxx()方法 cv.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "sample/HelloWorld", null, "java/lang/Object", null); { FieldVisitor fv1 = cv.visitField(ACC_PRIVATE, "intValue", "I", null, null); fv1.visitEnd(); } { FieldVisitor fv2 = cv.visitField(ACC_PRIVATE, "strValue", "Ljava/lang/String;", null, null); fv2.visitEnd(); } { MethodVisitor mv1 = cv.visitMethod(ACC_PUBLIC, "这篇关于Java ASM系列:(035)TraceClassVisitor介绍的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南