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.
public static final int | COMPUTE_FRAMES = 2 A flag to automatically compute the stack map frames of methods from scratch. |
public static final int | COMPUTE_MAXS = 1 A flag to automatically compute the maximum stack size and the maximum number of local
variables of methods. |
public | ClassWriter( Constructs a new ClassWriter object. |
public | ClassWriter( Constructs a new ClassWriter object and enables optimizations for "mostly add" bytecode
transformations. |
protected ClassLoader | Returns the ClassLoader to be used by the default implementation of getCommonSuperClass( |
protected String | getCommonSuperClass( Returns the common super type of the two given types. |
public int | Adds a class reference to the constant pool of the class being build. |
public int | Adds a number or string constant to the constant pool of the class being build. |
public int | newConstantDynamic( Adds a dynamic constant reference to the constant pool of the class being build. |
public int | Adds a field reference to the constant pool of the class being build. |
public int | this method is superseded by newHandle( |
public int | Adds a handle to the constant pool of the class being build. |
public int | newInvokeDynamic( Adds an invokedynamic reference to the constant pool of the class being build. |
public int | Adds a method reference to the constant pool of the class being build. |
public int | newMethodType( Adds a method type reference to the constant pool of the class being build. |
public int | Adds a module reference to the constant pool of the class being build. |
public int | newNameType( Adds a name and type to the constant pool of the class being build. |
public int | newPackage( Adds a package reference to the constant pool of the class being build. |
public int | Adds an UTF8 string to the constant pool of the class being build. |
public byte[] | Returns the content of the class file that was built by this ClassWriter. |
public final void | visit( Visits the header of the class. |
public final AnnotationVisitor | visitAnnotation( Visits an annotation of the class. |
public final void | visitAttribute( Visits a non standard attribute of the class. |
public final void | visitEnd() Visits the end of the class. |
public final FieldVisitor | visitField( Visits a field of the class. |
public final void | visitInnerClass( Visits information about an inner class. |
public final MethodVisitor | visitMethod( Visits a method of the class. |
public final ModuleVisitor | visitModule( Visit the module corresponding to the class. |
public final void | visitNestHost( Visits the nest host class of the class. |
public final void | visitNestMember( Visits a member of the nest. |
public final void | visitOuterClass( Visits the enclosing class of the class. |
public final void | visitPermittedSubclass( Visits a permitted subclasses. |
public final RecordComponentVisitor | visitRecordComponent( Visits a record component of the class. |
public final void | visitSource( Visits the source of the class. |
public final AnnotationVisitor | visitTypeAnnotation( Visits an annotation on a type in the class signature. |
A flag to automatically compute the stack map frames of methods from scratch. If this flag is
set, then the calls to the MethodVisitor.visitFrame(int, int, Object[], int, Object[]) method are ignored, and the stack
map frames are recomputed from the methods bytecode. The arguments of the MethodVisitor.visitMaxs(int, int) method are also ignored and recomputed from the bytecode. In other
words, COMPUTE_FRAMES implies COMPUTE_MAXS.
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 MethodVisitor.visitMaxs(int, int) method of the MethodVisitor returned by the visitMethod(int, String, String, String, String[]) method will be ignored, and computed automatically from the signature and the
bytecode of each method.
Note: for classes whose version is 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 COMPUTE_FRAMES instead.
Constructs a new ClassWriter object.
flagsoption flags that can be used to modify the default behavior of this class. Must
be zero or more of COMPUTE_MAXS and COMPUTE_FRAMES.
Constructs a new ClassWriter object and enables optimizations for "mostly add" bytecode
transformations. These optimizations are the following:
- The constant pool and bootstrap methods from the original class are copied as is in the new class, which saves time. New constant pool entries and new bootstrap methods will be added at the end if necessary, but unused constant pool entries or bootstrap methods 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).
classReaderthe 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.
flagsoption 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.
Returns the ClassLoader to be used by the default implementation of getCommonSuperClass(String, String) , that of this ClassWriter's runtime type by
default.
ClassLoader
Returns the common super type of the two given types. The default implementation of this method
loads the two given classes and uses the java.lang.Class methods to find the common
super class. It can be overridden to compute this common super type in other ways, in
particular without actually loading any class, or to take into account the class that is
currently being generated by this ClassWriter, which can of course not be loaded since it is
under construction.
type1the internal name of a class.
type2the internal name of another class.
the internal name of the common super class of the two given classes.
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.
valuethe internal name of the class.
the index of a new or already existing class reference item.
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.
the index of a new or already existing constant item with the given value.
public int newConstantDynamic(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments)
Adds a dynamic constant 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.
namename of the invoked method.
descriptorfield descriptor of the constant type.
bootstrapMethodHandlethe bootstrap method.
bootstrapMethodArgumentsthe bootstrap method constant arguments.
the index of a new or already existing dynamic constant reference item.
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.
ownerthe internal name of the field's owner class.
namethe field's name.
descriptorthe field's descriptor.
the index of a new or already existing field reference item.
this method is superseded by newHandle(int, String, String, String, boolean) .
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.
tagthe kind of this handle. Must be Opcodes.H_GETFIELD, Opcodes.H_GETSTATIC, Opcodes.H_PUTFIELD, Opcodes.H_PUTSTATIC, Opcodes.H_INVOKEVIRTUAL, Opcodes.H_INVOKESTATIC, Opcodes.H_INVOKESPECIAL,
Opcodes.H_NEWINVOKESPECIAL or Opcodes.H_INVOKEINTERFACE.
ownerthe internal name of the field or method owner class.
namethe name of the field or method.
descriptorthe descriptor of the field or method.
the index of a new or already existing method type reference item.
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.
tagthe kind of this handle. Must be Opcodes.H_GETFIELD, Opcodes.H_GETSTATIC, Opcodes.H_PUTFIELD, Opcodes.H_PUTSTATIC, Opcodes.H_INVOKEVIRTUAL, Opcodes.H_INVOKESTATIC, Opcodes.H_INVOKESPECIAL,
Opcodes.H_NEWINVOKESPECIAL or Opcodes.H_INVOKEINTERFACE.
ownerthe internal name of the field or method owner class.
namethe name of the field or method.
descriptorthe descriptor of the field or method.
isInterfacetrue if the owner is an interface.
the index of a new or already existing method type reference item.
public int newInvokeDynamic(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments)
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.
namename of the invoked method.
descriptordescriptor of the invoke method.
bootstrapMethodHandlethe bootstrap method.
bootstrapMethodArgumentsthe bootstrap method constant arguments.
the index of a new or already existing invokedynamic reference item.
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.
ownerthe internal name of the method's owner class.
namethe method's name.
descriptorthe method's descriptor.
isInterfacetrue if
owner
is an interface.the index of a new or already existing method reference item.
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.
methodDescriptormethod descriptor of the method type.
the index of a new or already existing method type reference item.
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.
moduleNamename of the module.
the index of a new or already existing module reference item.
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.
namea name.
descriptora type descriptor.
the index of a new or already existing name and type item.
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.
packageNamename of the package in its internal form.
the index of a new or already existing module reference item.
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.
valuethe String value.
the index of a new or already existing UTF8 item.
Returns the content of the class file that was built by this ClassWriter.
the binary content of the JVMS ClassFile structure that was built by this ClassWriter.
public final void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
Overridden from: ClassVisitor
Visits the header of the class.
versionthe class version. The minor version is stored in the 16 most significant bits,
and the major version in the 16 least significant bits.
accessthe class's access flags (see Opcodes). This parameter also indicates if
the class is deprecated Opcodes.ACC_DEPRECATED or a record Opcodes.ACC_RECORD.
namethe internal name of the class (see Type.getInternalName()).
signaturethe 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.
superNamethe 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.
interfacesthe internal names of the class's interfaces (see Type.getInternalName()). May be null.
Overridden from: ClassVisitor
Visits an annotation of the class.
descriptorthe class descriptor of the annotation class.
visibletrue if the annotation is visible at runtime.
a visitor to visit the annotation values, or null if this visitor is not
interested in visiting this annotation.
Overridden from: ClassVisitor
Visits the end of the class. This method, which is the last one to be called, is used to inform
the visitor that all the fields and methods of the class have been visited.
public final FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value)
Overridden from: ClassVisitor
Visits a field of the class.
accessthe field's access flags (see Opcodes). This parameter also indicates if
the field is synthetic and/or deprecated.
namethe field's name.
descriptorthe field's descriptor (see Type).
signaturethe field's signature. May be null if the field's type does not use
generic types.
valuethe 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.a visitor to visit field annotations and attributes, or null if this class
visitor is not interested in visiting these annotations and attributes.
Overridden from: ClassVisitor
Visits information about an inner class. This inner class is not necessarily a member of the
class being visited.
namethe internal name of an inner class (see Type.getInternalName()).
outerNamethe internal name of the class to which the inner class belongs (see Type.getInternalName()). May be null for not member classes.
innerNamethe (simple) name of the inner class inside its enclosing class. May be
null for anonymous inner classes.
accessthe access flags of the inner class as originally declared in the enclosing
class.
public final MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions)
Overridden from: ClassVisitor
Visits a method of the class. This method must return a new MethodVisitor
instance (or null) each time it is called, i.e., it should not return a previously
returned visitor.
accessthe method's access flags (see Opcodes). This parameter also indicates if
the method is synthetic and/or deprecated.
namethe method's name.
descriptorthe method's descriptor (see Type).
signaturethe method's signature. May be null if the method parameters,
return type and exceptions do not use generic types.
exceptionsthe internal names of the method's exception classes (see Type.getInternalName()). May be null.
an object to visit the byte code of the method, or null if this class
visitor is not interested in visiting the code of this method.
Overridden from: ClassVisitor
Visit the module corresponding to the class.
namethe fully qualified name (using dots) of the module.
accessthe module access flags, among
ACC_OPEN
, ACC_SYNTHETIC
and
ACC_MANDATED
.versionthe module version, or null.
a visitor to visit the module values, or null if this visitor is not
interested in visiting this module.
Overridden from: ClassVisitor
Visits the nest host class of the class. A nest is a set of classes of the same package that
share access to their private members. One of these classes, called the host, lists the other
members of the nest, which in turn should link to the host of their nest. This method must be
called only once and only if the visited class is a non-host member of a nest. A class is
implicitly its own nest, so it's invalid to call this method with the visited class name as
argument.
nestHostthe internal name of the host class of the nest.
Overridden from: ClassVisitor
Visits a member of the nest. A nest is a set of classes of the same package that share access
to their private members. One of these classes, called the host, lists the other members of the
nest, which in turn should link to the host of their nest. This method must be called only if
the visited class is the host of a nest. A nest host is implicitly a member of its own nest, so
it's invalid to call this method with the visited class name as argument.
nestMemberthe internal name of a nest member.
Overridden from: ClassVisitor
Visits the enclosing class of the class. This method must be called only if the class has an
enclosing class.
ownerinternal name of the enclosing class of the class.
namethe name of the method that contains the class, or null if the class is
not enclosed in a method of its enclosing class.
descriptorthe descriptor of the method that contains the class, or null if
the class is not enclosed in a method of its enclosing class.
Overridden from: ClassVisitor
Visits a permitted subclasses. A permitted subclass is one of the allowed subclasses of the
current class.
permittedSubclassthe internal name of a permitted subclass.
public final RecordComponentVisitor visitRecordComponent(String name, String descriptor, String signature)
Overridden from: ClassVisitor
Visits a record component of the class.
namethe record component name.
descriptorthe record component descriptor (see Type).
signaturethe record component signature. May be null if the record component
type does not use generic types.
a visitor to visit this record component annotations and attributes, or null
if this class visitor is not interested in visiting these annotations and attributes.
Overridden from: ClassVisitor
Visits the source of the class.
filethe name of the source file from which the class was compiled. May be
null.
debugadditional debug information to compute the correspondence between source and
compiled elements of the class. May be null.
public final AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String descriptor, boolean visible)
Overridden from: ClassVisitor
Visits an annotation on a type in the class signature.
typeRefa 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.
typePaththe 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.
descriptorthe class descriptor of the annotation class.
visibletrue if the annotation is visible at runtime.
a visitor to visit the annotation values, or null if this visitor is not
interested in visiting this annotation.