Top Description Fields Constructors Methods
org.easymock.asm

public Class ClassWriter

extends ClassVisitor
Class Inheritance

A 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 ClassReader and adapter ClassVisitor to generate a modified class from one or more existing Java classes.
Author
Eric Bruneton
See Also
JVMS 4

Field Summary

Modifier and TypeField and Description
private int
accessFlags

The access_flags field of the JVMS ClassFile structure.

private int
compute

Indicates what must be automatically computed in MethodWriter.

public static final int
COMPUTE_FRAMES

A flag to automatically compute the stack map frames of methods from scratch.

public static final int
COMPUTE_MAXS

A flag to automatically compute the maximum stack size and the maximum number of local variables of methods.

private ByteVector
debugExtension

The debug_extension field of the SourceDebugExtension attribute, or null.

private int
enclosingClassIndex

The class_index field of the EnclosingMethod attribute, or 0.

private int
enclosingMethodIndex

The method_index field of the EnclosingMethod attribute.

private Attribute
firstAttribute

The first non standard attribute of this class.

private FieldWriter
firstField

The fields of this class, stored in a linked list of FieldWriter linked via their FieldWriter#fv field.

private MethodWriter
firstMethod

The methods of this class, stored in a linked list of MethodWriter linked via their MethodWriter#mv field.

private RecordComponentWriter
firstRecordComponent

The record components of this class, stored in a linked list of RecordComponentWriter linked via their RecordComponentWriter#delegate field.

private final int
flags

The flags passed to the constructor.

private ByteVector
innerClasses

The 'classes' array of the InnerClasses attribute, or null.

private int
interfaceCount

The interface_count field of the JVMS ClassFile structure.

private int[]
interfaces

The 'interfaces' array of the JVMS ClassFile structure.

private FieldWriter
lastField

The fields of this class, stored in a linked list of FieldWriter linked via their FieldWriter#fv field.

private MethodWriter
lastMethod

The methods of this class, stored in a linked list of MethodWriter linked via their MethodWriter#mv field.

private RecordComponentWriter
lastRecordComponent

The record components of this class, stored in a linked list of RecordComponentWriter linked via their RecordComponentWriter#delegate field.

private AnnotationWriter
lastRuntimeInvisibleAnnotation

The last runtime invisible annotation of this class.

private AnnotationWriter
lastRuntimeInvisibleTypeAnnotation

The last runtime invisible type annotation of this class.

private AnnotationWriter
lastRuntimeVisibleAnnotation

The last runtime visible annotation of this class.

private AnnotationWriter
lastRuntimeVisibleTypeAnnotation

The last runtime visible type annotation of this class.

private ModuleWriter
moduleWriter

The Module attribute of this class, or null.

private int
nestHostClassIndex

The host_class_index field of the NestHost attribute, or 0.

private ByteVector
nestMemberClasses

The 'classes' array of the NestMembers attribute, or null.

private int
numberOfInnerClasses

The number_of_classes field of the InnerClasses attribute, or 0.

private int
numberOfNestMemberClasses

The number_of_classes field of the NestMembers attribute, or 0.

private int
numberOfPermittedSubclasses

The number_of_classes field of the PermittedSubclasses attribute, or 0.

private ByteVector
permittedSubclasses

The 'classes' array of the PermittedSubclasses attribute, or null.

private int
signatureIndex

The signature_index field of the Signature attribute, or 0.

private int
sourceFileIndex

The source_file_index field of the SourceFile attribute, or 0.

private int
superClass

The super_class field of the JVMS ClassFile structure.

private final SymbolTable
symbolTable

The symbol table for this class (contains the constant_pool and the BootstrapMethods).

private int
thisClass

The this_class field of the JVMS ClassFile structure.

private int
version

The minor_version and major_version fields of the JVMS ClassFile structure.

Inherited from org.easymock.asm.ClassVisitor:
apicv

Constructor Summary

AccessConstructor and Description
public
ClassWriter(final int
option flags that can be used to modify the default behavior of this class. Must be zero or more of COMPUTE_MAXS and COMPUTE_FRAMES.
flags
)

Constructs a new ClassWriter object.

public
ClassWriter(final ClassReader
the ClassReader used to read the original class. It will be used to copy the entire constant pool and bootstrap methods from the original class and also to copy other fragments of original bytecode where applicable.
classReader
,
final int
option flags that can be used to modify the default behavior of this class. Must be zero or more of COMPUTE_MAXS and COMPUTE_FRAMES. These option flags do not affect methods that are copied as is in the new class. This means that neither the maximum stack size nor the stack frames will be computed for these methods.
flags
)

Constructs a new ClassWriter object and enables optimizations for "mostly add" bytecode transformations.

Method Summary

Modifier and TypeMethod and Description
private Attribute[]

Returns:

the prototypes of the attributes used by this class, its fields and its methods.
getAttributePrototypes
()

Returns the prototypes of the attributes used by this class, its fields and its methods.

protected ClassLoader

Returns:

ClassLoader
getClassLoader
()

Returns the ClassLoader to be used by the default implementation of getCommonSuperClass(String, String), that of this ClassWriter's runtime type by default.

protected String

Returns:

the internal name of the common super class of the two given classes (see Type#getInternalName()).
getCommonSuperClass
(final String
the internal name of a class (see Type#getInternalName()).
type1
,
final String
the internal name of another class (see Type#getInternalName()).
type2
)

Returns the common super type of the two given types.

public boolean

Returns:

true if all the given flags, or more, were passed to the constructor.
hasFlags
(final int
some option flags. Must be zero or more of COMPUTE_MAXS and COMPUTE_FRAMES.
flags
)

Returns true if all the given flags were passed to the constructor.

public int

Returns:

the index of a new or already existing class reference item.
newClass
(final String
the internal name of the class (see Type#getInternalName()).
value
)

Adds a class reference to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing constant item with the given value.
newConst
(final Object
the value of the constant to be added to the constant pool. This parameter must be an Integer, a Float, a Long, a Double or a String.
value
)

Adds a number or string constant to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing dynamic constant reference item.
newConstantDynamic
(final String
name of the invoked method.
name
,
final String
field descriptor of the constant type.
descriptor
,
final Handle
the bootstrap method.
bootstrapMethodHandle
,
final Object...
the bootstrap method constant arguments.
bootstrapMethodArguments
)

Adds a dynamic constant reference to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing field reference item.
newField
(final String
the internal name of the field's owner class (see Type#getInternalName()).
owner
,
final String
the field's name.
name
,
final String
the field's descriptor.
descriptor
)

Adds a field reference to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing method type reference item.
newHandle
(final int tag, final String
the internal name of the field or method owner class (see Type#getInternalName()).
owner
,
final String
the name of the field or method.
name
,
final String
the descriptor of the field or method.
descriptor
)

Deprecated this method is superseded by newHandle(int, String, String, String, boolean).
Adds a handle to the constant pool of the class being build.
public int

Returns:

the index of a new or already existing method type reference item.
newHandle
(final int tag, final String
the internal name of the field or method owner class (see Type#getInternalName()).
owner
,
final String
the name of the field or method.
name
,
final String
the descriptor of the field or method.
descriptor
,
final boolean
true if the owner is an interface.
isInterface
)

Adds a handle to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing invokedynamic reference item.
newInvokeDynamic
(final String
name of the invoked method.
name
,
final String
descriptor of the invoke method.
descriptor
,
final Handle
the bootstrap method.
bootstrapMethodHandle
,
final Object...
the bootstrap method constant arguments.
bootstrapMethodArguments
)

Adds an invokedynamic reference to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing method reference item.
newMethod
(final String
the internal name of the method's owner class (see Type#getInternalName()).
owner
,
final String
the method's name.
name
,
final String
the method's descriptor.
descriptor
,
final boolean
true if owner is an interface.
isInterface
)

Adds a method reference to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing method type reference item.
newMethodType
(final String
method descriptor of the method type.
methodDescriptor
)

Adds a method type reference to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing module reference item.
newModule
(final String
name of the module.
moduleName
)

Adds a module reference to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing name and type item.
newNameType
(final String
a name.
name
,
final String
a type descriptor.
descriptor
)

Adds a name and type to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing module reference item.
newPackage
(final String
name of the package in its internal form.
packageName
)

Adds a package reference to the constant pool of the class being build.

public int

Returns:

the index of a new or already existing UTF8 item.
newUTF8
(final String
the String value.
value
)

Adds an UTF8 string to the constant pool of the class being build.

private byte[]

Returns:

an equivalent of 'classFile', with the ASM specific instructions replaced with standard ones.
replaceAsmInstructions
(final byte[]
a class file containing ASM specific instructions, generated by this ClassWriter.
classFile
,
final boolean
whether there is at least one stack map frames in 'classFile'.
hasFrames
)

Returns the equivalent of the given class file, with the ASM specific instructions replaced with standard ones.

public byte[]

Returns:

the binary content of the JVMS ClassFile structure that was built by this ClassWriter.
toByteArray
()

Returns the content of the class file that was built by this ClassWriter.

public final void
visit(final int
the class version. The minor version is stored in the 16 most significant bits, and the major version in the 16 least significant bits.
version
,
final int
the class's access flags (see Opcodes). This parameter also indicates if the class is deprecated Opcodes#ACC_DEPRECATED or a record Opcodes#ACC_RECORD.
access
,
final String
the internal name of the class (see Type#getInternalName()).
name
,
final String
the signature of this class. May be null if the class is not a generic one, and does not extend or implement generic classes or interfaces.
signature
,
final String
the internal of name of the super class (see Type#getInternalName()). For interfaces, the super class is Object. May be null, but only for the Object class.
superName
,
final String[]
the internal names of the class's interfaces (see Type#getInternalName()). May be null.
interfaces
)

Overrides org.easymock.asm.ClassVisitor.visit.

Visits the header of the class.
public final AnnotationVisitor
visitAnnotation(final String
the class descriptor of the annotation class.
descriptor
,
final boolean
true if the annotation is visible at runtime.
visible
)

Overrides org.easymock.asm.ClassVisitor.visitAnnotation.

Visits an annotation of the class.
public final void
visitAttribute(final Attribute
an attribute.
attribute
)

Overrides org.easymock.asm.ClassVisitor.visitAttribute.

Visits a non standard attribute of the class.
public final void
visitEnd()

Overrides org.easymock.asm.ClassVisitor.visitEnd.

Visits the end of the class.
public final FieldVisitor
visitField(final int
the field's access flags (see Opcodes). This parameter also indicates if the field is synthetic and/or deprecated.
access
,
final String
the field's name.
name
,
final String
the field's descriptor (see Type).
descriptor
,
final String
the field's signature. May be null if the field's type does not use generic types.
signature
,
final Object
the field's initial value. This parameter, which may be null if the field does not have an initial value, must be an Integer, a Float, a Long, a Double or a String (for int, float, long or String fields respectively). This parameter is only used for static fields. Its value is ignored for non static fields, which must be initialized through bytecode instructions in constructors or methods.
value
)

Overrides org.easymock.asm.ClassVisitor.visitField.

Visits a field of the class.
public final void
visitInnerClass(final String
the internal name of C (see Type#getInternalName()).
name
,
final String
the internal name of the class or interface C is a member of (see Type#getInternalName()). Must be null if C is not the member of a class or interface (e.g. for local or anonymous classes).
outerName
,
final String
the (simple) name of C. Must be null for anonymous inner classes.
innerName
,
final int
the access flags of C originally declared in the source code from which this class was compiled.
access
)

Overrides org.easymock.asm.ClassVisitor.visitInnerClass.

Visits information about an inner class.
public final MethodVisitor
visitMethod(final int
the method's access flags (see Opcodes). This parameter also indicates if the method is synthetic and/or deprecated.
access
,
final String
the method's name.
name
,
final String
the method's descriptor (see Type).
descriptor
,
final String
the method's signature. May be null if the method parameters, return type and exceptions do not use generic types.
signature
,
final String[]
the internal names of the method's exception classes (see Type#getInternalName()). May be null.
exceptions
)

Overrides org.easymock.asm.ClassVisitor.visitMethod.

Visits a method of the class.
public final ModuleVisitor
visitModule(final String
the fully qualified name (using dots) of the module.
name
,
final int
the module access flags, among ACC_OPEN, ACC_SYNTHETIC and ACC_MANDATED.
access
,
final String
the module version, or null.
version
)

Overrides org.easymock.asm.ClassVisitor.visitModule.

Visit the module corresponding to the class.
public final void
visitNestHost(final String
the internal name of the host class of the nest (see Type#getInternalName()).
nestHost
)

Overrides org.easymock.asm.ClassVisitor.visitNestHost.

Visits the nest host class of the class.
public final void
visitNestMember(final String
the internal name of a nest member (see Type#getInternalName()).
nestMember
)

Overrides org.easymock.asm.ClassVisitor.visitNestMember.

Visits a member of the nest.
public final void
visitOuterClass(final String
internal name of the enclosing class of the class (see Type#getInternalName()).
owner
,
final String
the name of the method that contains the class, or null if the class is not enclosed in a method or constructor of its enclosing class (e.g. if it is enclosed in an instance initializer, static initializer, instance variable initializer, or class variable initializer).
name
,
final String
the descriptor of the method that contains the class, or null if the class is not enclosed in a method or constructor of its enclosing class (e.g. if it is enclosed in an instance initializer, static initializer, instance variable initializer, or class variable initializer).
descriptor
)

Overrides org.easymock.asm.ClassVisitor.visitOuterClass.

Visits the enclosing class of the class.
public final void
visitPermittedSubclass(final String
the internal name of a permitted subclass (see Type#getInternalName()).
permittedSubclass
)

Overrides org.easymock.asm.ClassVisitor.visitPermittedSubclass.

Visits a permitted subclasses.
public final RecordComponentVisitor
visitRecordComponent(final String
the record component name.
name
,
final String
the record component descriptor (see Type).
descriptor
,
final String
the record component signature. May be null if the record component type does not use generic types.
signature
)

Overrides org.easymock.asm.ClassVisitor.visitRecordComponent.

Visits a record component of the class.
public final void
visitSource(final String
the name of the source file from which the class was compiled. May be null.
file
,
final String
additional debug information to compute the correspondence between source and compiled elements of the class. May be null.
debug
)

Overrides org.easymock.asm.ClassVisitor.visitSource.

Visits the source of the class.
public final AnnotationVisitor
visitTypeAnnotation(final int
a reference to the annotated type. The sort of this type reference must be TypeReference#CLASS_TYPE_PARAMETER, TypeReference#CLASS_TYPE_PARAMETER_BOUND or TypeReference#CLASS_EXTENDS. See TypeReference.
typeRef
,
final TypePath
the path to the annotated type argument, wildcard bound, array element type, or static inner type within 'typeRef'. May be null if the annotation targets 'typeRef' as a whole.
typePath
,
final String
the class descriptor of the annotation class.
descriptor
,
final boolean
true if the annotation is visible at runtime.
visible
)

Overrides org.easymock.asm.ClassVisitor.visitTypeAnnotation.

Visits an annotation on a type in the class signature.
Inherited from org.easymock.asm.ClassVisitor:
getDelegate