public class GeneratorAdapter extends LocalVariablesSorter
MethodAdapter with convenient methods to generate
 code. For example, using this adapter, the class below
 
 
 public class Example {
     public static void main(String[] args) {
         System.out.println("Hello world!");
     }
 }
 
 
 can be generated as follows:
 
 
 ClassWriter cw = new ClassWriter(true);
 cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
 
 Method m = Method.getMethod("void <init> ()");
 GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw);
 mg.loadThis();
 mg.invokeConstructor(Type.getType(Object.class), m);
 mg.returnValue();
 mg.endMethod();
 
 m = Method.getMethod("void main (String[])");
 mg = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
 mg.getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class));
 mg.push("Hello world!");
 mg.invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println (String)"));
 mg.returnValue();
 mg.endMethod();
 
 cw.visitEnd();
 | Modifier and Type | Field and Description | 
|---|---|
| private int | accessAccess flags of the method visited by this adapter. | 
| static int | ADDConstant for the  mathmethod. | 
| static int | ANDConstant for the  mathmethod. | 
| private Type[] | argumentTypesArgument types of the method visited by this adapter. | 
| private static Type | BOOLEAN_TYPE | 
| private static Method | BOOLEAN_VALUE | 
| private static Type | BYTE_TYPE | 
| private static Method | CHAR_VALUE | 
| private static Type | CHARACTER_TYPE | 
| static int | DIVConstant for the  mathmethod. | 
| private static Type | DOUBLE_TYPE | 
| private static Method | DOUBLE_VALUE | 
| static int | EQConstant for the  ifCmpmethod. | 
| private static Type | FLOAT_TYPE | 
| private static Method | FLOAT_VALUE | 
| static int | GEConstant for the  ifCmpmethod. | 
| static int | GTConstant for the  ifCmpmethod. | 
| private static Method | INT_VALUE | 
| private static Type | INTEGER_TYPE | 
| static int | LEConstant for the  ifCmpmethod. | 
| private java.util.List | localTypesTypes of the local variables of the method visited by this adapter. | 
| private static Type | LONG_TYPE | 
| private static Method | LONG_VALUE | 
| static int | LTConstant for the  ifCmpmethod. | 
| static int | MULConstant for the  mathmethod. | 
| static int | NEConstant for the  ifCmpmethod. | 
| static int | NEGConstant for the  mathmethod. | 
| private static Type | NUMBER_TYPE | 
| private static Type | OBJECT_TYPE | 
| static int | ORConstant for the  mathmethod. | 
| static int | REMConstant for the  mathmethod. | 
| private Type | returnTypeReturn type of the method visited by this adapter. | 
| static int | SHLConstant for the  mathmethod. | 
| private static Type | SHORT_TYPE | 
| static int | SHRConstant for the  mathmethod. | 
| static int | SUBConstant for the  mathmethod. | 
| static int | USHRConstant for the  mathmethod. | 
| static int | XORConstant for the  mathmethod. | 
firstLocal, nextLocalmv| Constructor and Description | 
|---|
| GeneratorAdapter(int access,
                Method method,
                MethodVisitor mv)Creates a new  GeneratorAdapter. | 
| GeneratorAdapter(int access,
                Method method,
                java.lang.String signature,
                Type[] exceptions,
                ClassVisitor cv)Creates a new  GeneratorAdapter. | 
| GeneratorAdapter(MethodVisitor mv,
                int access,
                java.lang.String name,
                java.lang.String desc)Creates a new  GeneratorAdapter. | 
| Modifier and Type | Method and Description | 
|---|---|
| void | arrayLength()Generates the instruction to compute the length of an array. | 
| void | arrayLoad(Type type)Generates the instruction to load an element from an array. | 
| void | arrayStore(Type type)Generates the instruction to store an element in an array. | 
| void | box(Type type)Generates the instructions to box the top stack value. | 
| void | cast(Type from,
    Type to)Generates the instructions to cast a numerical value from one type to
 another. | 
| void | catchException(Label start,
              Label end,
              Type exception)Marks the start of an exception handler. | 
| void | checkCast(Type type)Generates the instruction to check that the top stack value is of the
 given type. | 
| void | dup()Generates a DUP instruction. | 
| void | dup2()Generates a DUP2 instruction. | 
| void | dup2X1()Generates a DUP2_X1 instruction. | 
| void | dup2X2()Generates a DUP2_X2 instruction. | 
| void | dupX1()Generates a DUP_X1 instruction. | 
| void | dupX2()Generates a DUP_X2 instruction. | 
| void | endMethod()Marks the end of the visited method. | 
| private void | fieldInsn(int opcode,
         Type ownerType,
         java.lang.String name,
         Type fieldType)Generates a get field or set field instruction. | 
| private int | getArgIndex(int arg)Returns the index of the given method argument in the frame's local
 variables array. | 
| void | getField(Type owner,
        java.lang.String name,
        Type type)Generates the instruction to push the value of a non static field on the
 stack. | 
| private static java.lang.String[] | getInternalNames(Type[] types)Returns the internal names of the given types. | 
| Type | getLocalType(int local)Returns the type of the given local variable. | 
| void | getStatic(Type owner,
         java.lang.String name,
         Type type)Generates the instruction to push the value of a static field on the
 stack. | 
| void | goTo(Label label)Generates the instruction to jump to the given label. | 
| void | ifCmp(Type type,
     int mode,
     Label label)Generates the instructions to jump to a label based on the comparison of
 the top two stack values. | 
| void | ifICmp(int mode,
      Label label)Generates the instructions to jump to a label based on the comparison of
 the top two integer stack values. | 
| void | ifNonNull(Label label)Generates the instruction to jump to the given label if the top stack
 value is not null. | 
| void | ifNull(Label label)Generates the instruction to jump to the given label if the top stack
 value is null. | 
| void | ifZCmp(int mode,
      Label label)Generates the instructions to jump to a label based on the comparison of
 the top integer stack value with zero. | 
| void | iinc(int local,
    int amount)Generates the instruction to increment the given local variable. | 
| void | instanceOf(Type type)Generates the instruction to test if the top stack value is of the given
 type. | 
| void | invokeConstructor(Type type,
                 Method method)Generates the instruction to invoke a constructor. | 
| private void | invokeInsn(int opcode,
          Type type,
          Method method)Generates an invoke method instruction. | 
| void | invokeInterface(Type owner,
               Method method)Generates the instruction to invoke an interface method. | 
| void | invokeStatic(Type owner,
            Method method)Generates the instruction to invoke a static method. | 
| void | invokeVirtual(Type owner,
             Method method)Generates the instruction to invoke a normal method. | 
| void | loadArg(int arg)Generates the instruction to load the given method argument on the stack. | 
| void | loadArgArray()Generates the instructions to load all the method arguments on the stack,
 as a single object array. | 
| void | loadArgs()Generates the instructions to load all the method arguments on the stack. | 
| void | loadArgs(int arg,
        int count)Generates the instructions to load the given method arguments on the
 stack. | 
| private void | loadInsn(Type type,
        int index)Generates the instruction to push a local variable on the stack. | 
| void | loadLocal(int local)Generates the instruction to load the given local variable on the stack. | 
| void | loadLocal(int local,
         Type type)Generates the instruction to load the given local variable on the stack. | 
| void | loadThis()Generates the instruction to load 'this' on the stack. | 
| Label | mark()Marks the current code position with a new label. | 
| void | mark(Label label)Marks the current code position with the given label. | 
| void | math(int op,
    Type type)Generates the instruction to do the specified mathematical or logical
 operation. | 
| void | monitorEnter()Generates the instruction to get the monitor of the top stack value. | 
| void | monitorExit()Generates the instruction to release the monitor of the top stack value. | 
| void | newArray(Type type)Generates the instruction to create a new array. | 
| void | newInstance(Type type)Generates the instruction to create a new object. | 
| Label | newLabel()Creates a new  Label. | 
| void | not()Generates the instructions to compute the bitwise negation of the top
 stack value. | 
| void | pop()Generates a POP instruction. | 
| void | pop2()Generates a POP2 instruction. | 
| void | push(boolean value)Generates the instruction to push the given value on the stack. | 
| void | push(double value)Generates the instruction to push the given value on the stack. | 
| void | push(float value)Generates the instruction to push the given value on the stack. | 
| void | push(int value)Generates the instruction to push the given value on the stack. | 
| void | push(long value)Generates the instruction to push the given value on the stack. | 
| void | push(java.lang.String value)Generates the instruction to push the given value on the stack. | 
| void | push(Type value)Generates the instruction to push the given value on the stack. | 
| void | putField(Type owner,
        java.lang.String name,
        Type type)Generates the instruction to store the top stack value in a non static
 field. | 
| void | putStatic(Type owner,
         java.lang.String name,
         Type type)Generates the instruction to store the top stack value in a static field. | 
| void | ret(int local)Generates a RET instruction. | 
| void | returnValue()Generates the instruction to return the top stack value to the caller. | 
| protected void | setLocalType(int local,
            Type type)Sets the current type of the given local variable. | 
| void | storeArg(int arg)Generates the instruction to store the top stack value in the given
 method argument. | 
| private void | storeInsn(Type type,
         int index)Generates the instruction to store the top stack value in a local
 variable. | 
| void | storeLocal(int local)Generates the instruction to store the top stack value in the given local
 variable. | 
| void | storeLocal(int local,
          Type type)Generates the instruction to store the top stack value in the given local
 variable. | 
| void | swap()Generates a SWAP instruction. | 
| void | swap(Type prev,
    Type type)Generates the instructions to swap the top two stack values. | 
| void | tableSwitch(int[] keys,
           TableSwitchGenerator generator)Generates the instructions for a switch statement. | 
| void | tableSwitch(int[] keys,
           TableSwitchGenerator generator,
           boolean useTable)Generates the instructions for a switch statement. | 
| void | throwException()Generates the instruction to throw an exception. | 
| void | throwException(Type type,
              java.lang.String msg)Generates the instructions to create and throw an exception. | 
| private void | typeInsn(int opcode,
        Type type)Generates a type dependent instruction. | 
| void | unbox(Type type)Generates the instructions to unbox the top stack value. | 
newLocal, visitFrame, visitIincInsn, visitLocalVariable, visitMaxs, visitVarInsnvisitAnnotation, visitAnnotationDefault, visitAttribute, visitCode, visitEnd, visitFieldInsn, visitInsn, visitIntInsn, visitJumpInsn, visitLabel, visitLdcInsn, visitLineNumber, visitLookupSwitchInsn, visitMethodInsn, visitMultiANewArrayInsn, visitParameterAnnotation, visitTableSwitchInsn, visitTryCatchBlock, visitTypeInsnprivate static final Type BYTE_TYPE
private static final Type BOOLEAN_TYPE
private static final Type SHORT_TYPE
private static final Type CHARACTER_TYPE
private static final Type INTEGER_TYPE
private static final Type FLOAT_TYPE
private static final Type LONG_TYPE
private static final Type DOUBLE_TYPE
private static final Type NUMBER_TYPE
private static final Type OBJECT_TYPE
private static final Method BOOLEAN_VALUE
private static final Method CHAR_VALUE
private static final Method INT_VALUE
private static final Method FLOAT_VALUE
private static final Method LONG_VALUE
private static final Method DOUBLE_VALUE
public static final int ADD
math method.public static final int SUB
math method.public static final int MUL
math method.public static final int DIV
math method.public static final int REM
math method.public static final int NEG
math method.public static final int SHL
math method.public static final int SHR
math method.public static final int USHR
math method.public static final int AND
math method.public static final int OR
math method.public static final int XOR
math method.public static final int EQ
ifCmp method.public static final int NE
ifCmp method.public static final int LT
ifCmp method.public static final int GE
ifCmp method.public static final int GT
ifCmp method.public static final int LE
ifCmp method.private final int access
private final Type returnType
private final Type[] argumentTypes
private final java.util.List localTypes
public GeneratorAdapter(MethodVisitor mv, int access, java.lang.String name, java.lang.String desc)
GeneratorAdapter.public GeneratorAdapter(int access,
                Method method,
                MethodVisitor mv)
GeneratorAdapter.access - access flags of the adapted method.method - the adapted method.mv - the method visitor to which this adapter delegates calls.public GeneratorAdapter(int access,
                Method method,
                java.lang.String signature,
                Type[] exceptions,
                ClassVisitor cv)
GeneratorAdapter.access - access flags of the adapted method.method - the adapted method.signature - the signature of the adapted method (may be
                   null).exceptions - the exceptions thrown by the adapted method (may be
                   null).cv - the class visitor to which this adapter delegates calls.private static java.lang.String[] getInternalNames(Type[] types)
types - a set of types.public void push(boolean value)
value - the value to be pushed on the stack.public void push(int value)
value - the value to be pushed on the stack.public void push(long value)
value - the value to be pushed on the stack.public void push(float value)
value - the value to be pushed on the stack.public void push(double value)
value - the value to be pushed on the stack.public void push(java.lang.String value)
value - the value to be pushed on the stack. May be null.public void push(Type value)
value - the value to be pushed on the stack.private int getArgIndex(int arg)
arg - the index of a method argument.private void loadInsn(Type type, int index)
type - the type of the local variable to be loaded.index - an index in the frame's local variables array.private void storeInsn(Type type, int index)
type - the type of the local variable to be stored.index - an index in the frame's local variables array.public void loadThis()
public void loadArg(int arg)
arg - the index of a method argument.public void loadArgs(int arg,
            int count)
arg - the index of the first method argument to be loaded.count - the number of method arguments to be loaded.public void loadArgs()
public void loadArgArray()
public void storeArg(int arg)
arg - the index of a method argument.public Type getLocalType(int local)
local - a local variable identifier, as returned by
              newLocal().protected void setLocalType(int local,
                Type type)
LocalVariablesSortersetLocalType in class LocalVariablesSorterlocal - a local variable identifier, as returned by newLocal().type - the type of the value being stored in the local variablepublic void loadLocal(int local)
local - a local variable identifier, as returned by
              newLocal().public void loadLocal(int local,
             Type type)
local - a local variable identifier, as returned by
              newLocal().type - the type of this local variable.public void storeLocal(int local)
local - a local variable identifier, as returned by
              newLocal().public void storeLocal(int local,
              Type type)
local - a local variable identifier, as returned by
              newLocal().type - the type of this local variable.public void arrayLoad(Type type)
type - the type of the array element to be loaded.public void arrayStore(Type type)
type - the type of the array element to be stored.public void pop()
public void pop2()
public void dup()
public void dup2()
public void dupX1()
public void dupX2()
public void dup2X1()
public void dup2X2()
public void swap()
public void swap(Type prev, Type type)
prev - type of the top - 1 stack value.type - type of the top stack value.public void math(int op,
        Type type)
op - a mathematical or logical operation. Must be one of ADD, SUB,
             MUL, DIV, REM, NEG, SHL, SHR, USHR, AND, OR, XOR.type - the type of the operand(s) for this operation.public void not()
public void iinc(int local,
        int amount)
local - the local variable to be incremented.amount - the amount by which the local variable must be incremented.public void cast(Type from, Type to)
from - the type of the top stack valueto - the type into which this value must be cast.public void box(Type type)
type - the type of the top stack value.public void unbox(Type type)
type - the type of the top stack value.public void mark(Label label)
label - a label.public Label mark()
public void ifCmp(Type type, int mode, Label label)
type - the type of the top two stack values.mode - how these values must be compared. One of EQ, NE, LT, GE, GT,
              LE.label - where to jump if the comparison result is true.public void ifICmp(int mode,
          Label label)
mode - how these values must be compared. One of EQ, NE, LT, GE, GT,
              LE.label - where to jump if the comparison result is true.public void ifZCmp(int mode,
          Label label)
mode - how these values must be compared. One of EQ, NE, LT, GE, GT,
              LE.label - where to jump if the comparison result is true.public void ifNull(Label label)
label - where to jump if the condition is true.public void ifNonNull(Label label)
label - where to jump if the condition is true.public void goTo(Label label)
label - where to jump if the condition is true.public void ret(int local)
local - a local variable identifier, as returned by
              newLocal().public void tableSwitch(int[] keys,
               TableSwitchGenerator generator)
keys - the switch case keys.generator - a generator to generate the code for the switch cases.public void tableSwitch(int[] keys,
               TableSwitchGenerator generator,
               boolean useTable)
keys - the switch case keys.generator - a generator to generate the code for the switch cases.useTable - true to use a TABLESWITCH instruction, or
                  false to use a LOOKUPSWITCH instruction.public void returnValue()
private void fieldInsn(int opcode,
             Type ownerType,
             java.lang.String name,
             Type fieldType)
opcode - the instruction's opcode.ownerType - the class in which the field is defined.name - the name of the field.fieldType - the type of the field.public void getStatic(Type owner, java.lang.String name, Type type)
owner - the class in which the field is defined.name - the name of the field.type - the type of the field.public void putStatic(Type owner, java.lang.String name, Type type)
owner - the class in which the field is defined.name - the name of the field.type - the type of the field.public void getField(Type owner, java.lang.String name, Type type)
owner - the class in which the field is defined.name - the name of the field.type - the type of the field.public void putField(Type owner, java.lang.String name, Type type)
owner - the class in which the field is defined.name - the name of the field.type - the type of the field.private void invokeInsn(int opcode,
              Type type,
              Method method)
opcode - the instruction's opcode.type - the class in which the method is defined.method - the method to be invoked.public void invokeVirtual(Type owner, Method method)
owner - the class in which the method is defined.method - the method to be invoked.public void invokeConstructor(Type type, Method method)
type - the class in which the constructor is defined.method - the constructor to be invoked.public void invokeStatic(Type owner, Method method)
owner - the class in which the method is defined.method - the method to be invoked.public void invokeInterface(Type owner, Method method)
owner - the class in which the method is defined.method - the method to be invoked.private void typeInsn(int opcode,
            Type type)
opcode - the instruction's opcode.type - the instruction's operand.public void newInstance(Type type)
type - the class of the object to be created.public void newArray(Type type)
type - the type of the array elements.public void arrayLength()
public void throwException()
public void throwException(Type type, java.lang.String msg)
type - the class of the exception to be thrown.msg - the detailed message of the exception.public void checkCast(Type type)
type - a class or interface type.public void instanceOf(Type type)
type - a class or interface type.public void monitorEnter()
public void monitorExit()
public void endMethod()
public void catchException(Label start, Label end, Type exception)
start - beginning of the exception handler's scope (inclusive).end - end of the exception handler's scope (exclusive).exception - internal name of the type of exceptions handled by the
                  handler.