spring-framework / org.springframework.asm / ClassWriter

ClassWriter

open class ClassWriter : ClassVisitor

A ClassVisitor that generates classes in bytecode form. More precisely this visitor generates a byte array conforming to the Java class file format. It can be used alone, to generate a Java class "from scratch", or with one or more ClassReader and adapter class visitor to generate a modified class from one or more existing Java classes.

Author
Eric Bruneton

Constructors

<init>

ClassWriter(flags: Int)

Constructs a new ClassWriter object.

ClassWriter(classReader: ClassReader, flags: Int)

Constructs a new ClassWriter object and enables optimizations for "mostly add" bytecode transformations. These optimizations are the following:

  • The constant pool from the original class is copied as is in the new class, which saves time. New constant pool entries will be added at the end if necessary, but unused constant pool entries won't be removed.
  • Methods that are not transformed are copied as is in the new class, directly from the original class bytecode (i.e. without emitting visit events for all the method instructions), which saves a lot of time. Untransformed methods are detected by the fact that the ClassReader receives MethodVisitor objects that come from a ClassWriter (and not from any other ClassVisitor instance).

Properties

COMPUTE_FRAMES

static val COMPUTE_FRAMES: Int

Flag to automatically compute the stack map frames of methods from scratch. If this flag is set, then the calls to the MethodVisitor#visitFrame method are ignored, and the stack map frames are recomputed from the methods bytecode. The arguments of the visitMaxs method are also ignored and recomputed from the bytecode. In other words, COMPUTE_FRAMES implies COMPUTE_MAXS.

COMPUTE_MAXS

static val COMPUTE_MAXS: Int

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 visitMaxs method of the MethodVisitor returned by the visitMethod method will be ignored, and computed automatically from the signature and the bytecode of each method.

Functions

newClass

open fun newClass(value: String): Int

Adds a class reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newConst

open fun newConst(cst: Any): Int

Adds a number or string constant to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newField

open fun newField(owner: String, name: String, desc: String): Int

Adds a field reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newHandle

open fun newHandle(tag: Int, owner: String, name: String, desc: String): Int
open fun newHandle(tag: Int, owner: String, name: String, desc: String, itf: Boolean): Int

Adds a handle to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newInvokeDynamic

open fun newInvokeDynamic(name: String, desc: String, bsm: Handle, vararg bsmArgs: Any): Int

Adds an invokedynamic reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newMethod

open fun newMethod(owner: String, name: String, desc: String, itf: Boolean): Int

Adds a method reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newMethodType

open fun newMethodType(methodDesc: String): Int

Adds a method type reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newModule

open fun newModule(moduleName: String): Int

Adds a module reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newNameType

open fun newNameType(name: String, desc: String): Int

Adds a name and type to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newPackage

open fun newPackage(packageName: String): Int

Adds a package reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

newUTF8

open fun newUTF8(value: String): Int

Adds an UTF8 string to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

toByteArray

open fun toByteArray(): ByteArray

Returns the bytecode of the class that was build with this class writer.

visit

fun visit(version: Int, access: Int, name: String, signature: String, superName: String, interfaces: Array<String>): Unit

visitAnnotation

fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor

visitAttribute

fun visitAttribute(attr: Attribute): Unit

visitEnd

fun visitEnd(): Unit

visitField

fun visitField(access: Int, name: String, desc: String, signature: String, value: Any): FieldVisitor

visitInnerClass

fun visitInnerClass(name: String, outerName: String, innerName: String, access: Int): Unit

visitMethod

fun visitMethod(access: Int, name: String, desc: String, signature: String, exceptions: Array<String>): MethodVisitor

visitModule

fun visitModule(name: String, access: Int, version: String): ModuleVisitor

visitOuterClass

fun visitOuterClass(owner: String, name: String, desc: String): Unit

visitSource

fun visitSource(file: String, debug: String): Unit

visitTypeAnnotation

fun visitTypeAnnotation(typeRef: Int, typePath: TypePath, desc: String, visible: Boolean): AnnotationVisitor