/** * A visitor to visit a Java class. The methods of this class must be called in the following order: * {@code visit} [ {@code visitSource} ] [ {@code visitModule} ][ {@code visitNestHost} ][ {@code * visitOuterClass} ] ( {@code visitAnnotation} | {@code visitTypeAnnotation} | {@code * visitAttribute} )* ( {@code visitNestMember} | {@code visitInnerClass} | {@code visitField} | * {@code visitMethod} )* {@code visitEnd}. * * @author Eric Bruneton */
直接看注释,很明显这个类的作用是负责访问ClassReader解析的class文件,需要注意的是它有两个构造方法,一个是public ClassVisitor(final int api),还有一个是public ClassVisitor(final int api, final ClassVisitor classVisitor),第二个构造函数表明,ClassVisitor自身可以形成一条链路,这样设计的原因就是为了可以分层处理class文件,让结构更加清晰。
ClassWriter:
1 2 3 4 5 6 7 8 9
/** * A {@link ClassVisitor} that generates a corresponding ClassFile structure, as defined in the Java * Virtual Machine Specification (JVMS). It can be used alone, to generate a Java class "from * scratch", or with one or more {@link ClassReader} and adapter {@link ClassVisitor} to generate a * modified class from one or more existing Java classes. * * @see <a href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html">JVMS 4</a> * @author Eric Bruneton */
/** * A flag to automatically compute the maximum stack size and the maximum number of local * variables of methods. If this flag is set, then the arguments of the {@link * MethodVisitor#visitMaxs} method of the {@link MethodVisitor} returned by the {@link * #visitMethod} method will be ignored, and computed automatically from the signature and the * bytecode of each method. * * <p><b>Note:</b> for classes whose version is {@link Opcodes#V1_7} of more, this option requires * valid stack map frames. The maximum stack size is then computed from these frames, and from the * bytecode instructions in between. If stack map frames are not present or must be recomputed, * used {@link #COMPUTE_FRAMES} instead. * * @see #ClassWriter(int) */
/** * A flag to automatically compute the stack map frames of methods from scratch. If this flag is * set, then the calls to the {@link MethodVisitor#visitFrame} method are ignored, and the stack * map frames are recomputed from the methods bytecode. The arguments of the {@link * MethodVisitor#visitMaxs} method are also ignored and recomputed from the bytecode. In other * words, {@link #COMPUTE_FRAMES} implies {@link #COMPUTE_MAXS}. * * @see #ClassWriter(int) */