org.objectweb.asm.commons

Class GeneratorAdapter

public class GeneratorAdapter extends LocalVariablesSorter

A {@link org.objectweb.asm.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();
 

Author: Juozas Baliuka Chris Nokleberg Eric Bruneton

Field Summary
static intADD
Constant for the {@link #math math} method.
static intAND
Constant for the {@link #math math} method.
static intDIV
Constant for the {@link #math math} method.
static intEQ
Constant for the {@link #ifCmp ifCmp} method.
static intGE
Constant for the {@link #ifCmp ifCmp} method.
static intGT
Constant for the {@link #ifCmp ifCmp} method.
static intLE
Constant for the {@link #ifCmp ifCmp} method.
static intLT
Constant for the {@link #ifCmp ifCmp} method.
static intMUL
Constant for the {@link #math math} method.
static intNE
Constant for the {@link #ifCmp ifCmp} method.
static intNEG
Constant for the {@link #math math} method.
static intOR
Constant for the {@link #math math} method.
static intREM
Constant for the {@link #math math} method.
static intSHL
Constant for the {@link #math math} method.
static intSHR
Constant for the {@link #math math} method.
static intSUB
Constant for the {@link #math math} method.
static intUSHR
Constant for the {@link #math math} method.
static intXOR
Constant for the {@link #math math} method.
Constructor Summary
GeneratorAdapter(MethodVisitor mv, int access, String name, String desc)
Creates a new {@link GeneratorAdapter}.
GeneratorAdapter(int access, Method method, MethodVisitor mv)
Creates a new {@link GeneratorAdapter}.
GeneratorAdapter(int access, Method method, String signature, Type[] exceptions, ClassVisitor cv)
Creates a new {@link GeneratorAdapter}.
Method Summary
voidarrayLength()
Generates the instruction to compute the length of an array.
voidarrayLoad(Type type)
Generates the instruction to load an element from an array.
voidarrayStore(Type type)
Generates the instruction to store an element in an array.
voidbox(Type type)
Generates the instructions to box the top stack value.
voidcast(Type from, Type to)
Generates the instructions to cast a numerical value from one type to another.
voidcatchException(Label start, Label end, Type exception)
Marks the start of an exception handler.
voidcheckCast(Type type)
Generates the instruction to check that the top stack value is of the given type.
voiddup()
Generates a DUP instruction.
voiddup2()
Generates a DUP2 instruction.
voiddup2X1()
Generates a DUP2_X1 instruction.
voiddup2X2()
Generates a DUP2_X2 instruction.
voiddupX1()
Generates a DUP_X1 instruction.
voiddupX2()
Generates a DUP_X2 instruction.
voidendMethod()
Marks the end of the visited method.
voidgetField(Type owner, String name, Type type)
Generates the instruction to push the value of a non static field on the stack.
TypegetLocalType(int local)
Returns the type of the given local variable.
voidgetStatic(Type owner, String name, Type type)
Generates the instruction to push the value of a static field on the stack.
voidgoTo(Label label)
Generates the instruction to jump to the given label.
voidifCmp(Type type, int mode, Label label)
Generates the instructions to jump to a label based on the comparison of the top two stack values.
voidifICmp(int mode, Label label)
Generates the instructions to jump to a label based on the comparison of the top two integer stack values.
voidifNonNull(Label label)
Generates the instruction to jump to the given label if the top stack value is not null.
voidifNull(Label label)
Generates the instruction to jump to the given label if the top stack value is null.
voidifZCmp(int mode, Label label)
Generates the instructions to jump to a label based on the comparison of the top integer stack value with zero.
voidiinc(int local, int amount)
Generates the instruction to increment the given local variable.
voidinstanceOf(Type type)
Generates the instruction to test if the top stack value is of the given type.
voidinvokeConstructor(Type type, Method method)
Generates the instruction to invoke a constructor.
voidinvokeInterface(Type owner, Method method)
Generates the instruction to invoke an interface method.
voidinvokeStatic(Type owner, Method method)
Generates the instruction to invoke a static method.
voidinvokeVirtual(Type owner, Method method)
Generates the instruction to invoke a normal method.
voidloadArg(int arg)
Generates the instruction to load the given method argument on the stack.
voidloadArgArray()
Generates the instructions to load all the method arguments on the stack, as a single object array.
voidloadArgs(int arg, int count)
Generates the instructions to load the given method arguments on the stack.
voidloadArgs()
Generates the instructions to load all the method arguments on the stack.
voidloadLocal(int local)
Generates the instruction to load the given local variable on the stack.
voidloadLocal(int local, Type type)
Generates the instruction to load the given local variable on the stack.
voidloadThis()
Generates the instruction to load 'this' on the stack.
voidmark(Label label)
Marks the current code position with the given label.
Labelmark()
Marks the current code position with a new label.
voidmath(int op, Type type)
Generates the instruction to do the specified mathematical or logical operation.
voidmonitorEnter()
Generates the instruction to get the monitor of the top stack value.
voidmonitorExit()
Generates the instruction to release the monitor of the top stack value.
voidnewArray(Type type)
Generates the instruction to create a new array.
voidnewInstance(Type type)
Generates the instruction to create a new object.
LabelnewLabel()
Creates a new {@link Label}.
intnewLocal(Type type)
Creates a new local variable of the given type.
voidnot()
Generates the instructions to compute the bitwise negation of the top stack value.
voidpop()
Generates a POP instruction.
voidpop2()
Generates a POP2 instruction.
voidpush(boolean value)
Generates the instruction to push the given value on the stack.
voidpush(int value)
Generates the instruction to push the given value on the stack.
voidpush(long value)
Generates the instruction to push the given value on the stack.
voidpush(float value)
Generates the instruction to push the given value on the stack.
voidpush(double value)
Generates the instruction to push the given value on the stack.
voidpush(String value)
Generates the instruction to push the given value on the stack.
voidpush(Type value)
Generates the instruction to push the given value on the stack.
voidputField(Type owner, String name, Type type)
Generates the instruction to store the top stack value in a non static field.
voidputStatic(Type owner, String name, Type type)
Generates the instruction to store the top stack value in a static field.
voidret(int local)
Generates a RET instruction.
voidreturnValue()
Generates the instruction to return the top stack value to the caller.
voidstoreArg(int arg)
Generates the instruction to store the top stack value in the given method argument.
voidstoreLocal(int local)
Generates the instruction to store the top stack value in the given local variable.
voidstoreLocal(int local, Type type)
Generates the instruction to store the top stack value in the given local variable.
voidswap()
Generates a SWAP instruction.
voidswap(Type prev, Type type)
Generates the instructions to swap the top two stack values.
voidtableSwitch(int[] keys, TableSwitchGenerator generator)
Generates the instructions for a switch statement.
voidtableSwitch(int[] keys, TableSwitchGenerator generator, boolean useTable)
Generates the instructions for a switch statement.
voidthrowException()
Generates the instruction to throw an exception.
voidthrowException(Type type, String msg)
Generates the instructions to create and throw an exception.
voidunbox(Type type)
Generates the instructions to unbox the top stack value.

Field Detail

ADD

public static final int ADD
Constant for the {@link #math math} method.

AND

public static final int AND
Constant for the {@link #math math} method.

DIV

public static final int DIV
Constant for the {@link #math math} method.

EQ

public static final int EQ
Constant for the {@link #ifCmp ifCmp} method.

GE

public static final int GE
Constant for the {@link #ifCmp ifCmp} method.

GT

public static final int GT
Constant for the {@link #ifCmp ifCmp} method.

LE

public static final int LE
Constant for the {@link #ifCmp ifCmp} method.

LT

public static final int LT
Constant for the {@link #ifCmp ifCmp} method.

MUL

public static final int MUL
Constant for the {@link #math math} method.

NE

public static final int NE
Constant for the {@link #ifCmp ifCmp} method.

NEG

public static final int NEG
Constant for the {@link #math math} method.

OR

public static final int OR
Constant for the {@link #math math} method.

REM

public static final int REM
Constant for the {@link #math math} method.

SHL

public static final int SHL
Constant for the {@link #math math} method.

SHR

public static final int SHR
Constant for the {@link #math math} method.

SUB

public static final int SUB
Constant for the {@link #math math} method.

USHR

public static final int USHR
Constant for the {@link #math math} method.

XOR

public static final int XOR
Constant for the {@link #math math} method.

Constructor Detail

GeneratorAdapter

public GeneratorAdapter(MethodVisitor mv, int access, String name, String desc)
Creates a new {@link GeneratorAdapter}.

Parameters: mv the method visitor to which this adapter delegates calls. access the method's access flags (see {@link Opcodes}). name the method's name. desc the method's descriptor (see {@link Type Type}).

GeneratorAdapter

public GeneratorAdapter(int access, Method method, MethodVisitor mv)
Creates a new {@link GeneratorAdapter}.

Parameters: access access flags of the adapted method. method the adapted method. mv the method visitor to which this adapter delegates calls.

GeneratorAdapter

public GeneratorAdapter(int access, Method method, String signature, Type[] exceptions, ClassVisitor cv)
Creates a new {@link GeneratorAdapter}.

Parameters: 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.

Method Detail

arrayLength

public void arrayLength()
Generates the instruction to compute the length of an array.

arrayLoad

public void arrayLoad(Type type)
Generates the instruction to load an element from an array.

Parameters: type the type of the array element to be loaded.

arrayStore

public void arrayStore(Type type)
Generates the instruction to store an element in an array.

Parameters: type the type of the array element to be stored.

box

public void box(Type type)
Generates the instructions to box the top stack value. This value is replaced by its boxed equivalent on top of the stack.

Parameters: type the type of the top stack value.

cast

public void cast(Type from, Type to)
Generates the instructions to cast a numerical value from one type to another.

Parameters: from the type of the top stack value to the type into which this value must be cast.

catchException

public void catchException(Label start, Label end, Type exception)
Marks the start of an exception handler.

Parameters: 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.

checkCast

public void checkCast(Type type)
Generates the instruction to check that the top stack value is of the given type.

Parameters: type a class or interface type.

dup

public void dup()
Generates a DUP instruction.

dup2

public void dup2()
Generates a DUP2 instruction.

dup2X1

public void dup2X1()
Generates a DUP2_X1 instruction.

dup2X2

public void dup2X2()
Generates a DUP2_X2 instruction.

dupX1

public void dupX1()
Generates a DUP_X1 instruction.

dupX2

public void dupX2()
Generates a DUP_X2 instruction.

endMethod

public void endMethod()
Marks the end of the visited method.

getField

public void getField(Type owner, String name, Type type)
Generates the instruction to push the value of a non static field on the stack.

Parameters: owner the class in which the field is defined. name the name of the field. type the type of the field.

getLocalType

public Type getLocalType(int local)
Returns the type of the given local variable.

Parameters: local a local variable identifier, as returned by {@link #newLocal newLocal}.

Returns: the type of the given local variable.

getStatic

public void getStatic(Type owner, String name, Type type)
Generates the instruction to push the value of a static field on the stack.

Parameters: owner the class in which the field is defined. name the name of the field. type the type of the field.

goTo

public void goTo(Label label)
Generates the instruction to jump to the given label.

Parameters: label where to jump if the condition is true.

ifCmp

public 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.

Parameters: 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.

ifICmp

public 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.

Parameters: 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.

ifNonNull

public void ifNonNull(Label label)
Generates the instruction to jump to the given label if the top stack value is not null.

Parameters: label where to jump if the condition is true.

ifNull

public void ifNull(Label label)
Generates the instruction to jump to the given label if the top stack value is null.

Parameters: label where to jump if the condition is true.

ifZCmp

public 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.

Parameters: 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.

iinc

public void iinc(int local, int amount)
Generates the instruction to increment the given local variable.

Parameters: local the local variable to be incremented. amount the amount by which the local variable must be incremented.

instanceOf

public void instanceOf(Type type)
Generates the instruction to test if the top stack value is of the given type.

Parameters: type a class or interface type.

invokeConstructor

public void invokeConstructor(Type type, Method method)
Generates the instruction to invoke a constructor.

Parameters: type the class in which the constructor is defined. method the constructor to be invoked.

invokeInterface

public void invokeInterface(Type owner, Method method)
Generates the instruction to invoke an interface method.

Parameters: owner the class in which the method is defined. method the method to be invoked.

invokeStatic

public void invokeStatic(Type owner, Method method)
Generates the instruction to invoke a static method.

Parameters: owner the class in which the method is defined. method the method to be invoked.

invokeVirtual

public void invokeVirtual(Type owner, Method method)
Generates the instruction to invoke a normal method.

Parameters: owner the class in which the method is defined. method the method to be invoked.

loadArg

public void loadArg(int arg)
Generates the instruction to load the given method argument on the stack.

Parameters: arg the index of a method argument.

loadArgArray

public void loadArgArray()
Generates the instructions to load all the method arguments on the stack, as a single object array.

loadArgs

public void loadArgs(int arg, int count)
Generates the instructions to load the given method arguments on the stack.

Parameters: arg the index of the first method argument to be loaded. count the number of method arguments to be loaded.

loadArgs

public void loadArgs()
Generates the instructions to load all the method arguments on the stack.

loadLocal

public void loadLocal(int local)
Generates the instruction to load the given local variable on the stack.

Parameters: local a local variable identifier, as returned by {@link #newLocal newLocal}.

loadLocal

public void loadLocal(int local, Type type)
Generates the instruction to load the given local variable on the stack.

Parameters: local a local variable identifier, as returned by {@link #newLocal newLocal}. type the type of this local variable.

loadThis

public void loadThis()
Generates the instruction to load 'this' on the stack.

mark

public void mark(Label label)
Marks the current code position with the given label.

Parameters: label a label.

mark

public Label mark()
Marks the current code position with a new label.

Returns: the label that was created to mark the current code position.

math

public void math(int op, Type type)
Generates the instruction to do the specified mathematical or logical operation.

Parameters: 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.

monitorEnter

public void monitorEnter()
Generates the instruction to get the monitor of the top stack value.

monitorExit

public void monitorExit()
Generates the instruction to release the monitor of the top stack value.

newArray

public void newArray(Type type)
Generates the instruction to create a new array.

Parameters: type the type of the array elements.

newInstance

public void newInstance(Type type)
Generates the instruction to create a new object.

Parameters: type the class of the object to be created.

newLabel

public Label newLabel()
Creates a new {@link Label}.

Returns: a new {@link Label}.

newLocal

public int newLocal(Type type)
Creates a new local variable of the given type.

Parameters: type the type of the local variable to be created.

Returns: the identifier of the newly created local variable.

not

public void not()
Generates the instructions to compute the bitwise negation of the top stack value.

pop

public void pop()
Generates a POP instruction.

pop2

public void pop2()
Generates a POP2 instruction.

push

public void push(boolean value)
Generates the instruction to push the given value on the stack.

Parameters: value the value to be pushed on the stack.

push

public void push(int value)
Generates the instruction to push the given value on the stack.

Parameters: value the value to be pushed on the stack.

push

public void push(long value)
Generates the instruction to push the given value on the stack.

Parameters: value the value to be pushed on the stack.

push

public void push(float value)
Generates the instruction to push the given value on the stack.

Parameters: value the value to be pushed on the stack.

push

public void push(double value)
Generates the instruction to push the given value on the stack.

Parameters: value the value to be pushed on the stack.

push

public void push(String value)
Generates the instruction to push the given value on the stack.

Parameters: value the value to be pushed on the stack. May be null.

push

public void push(Type value)
Generates the instruction to push the given value on the stack.

Parameters: value the value to be pushed on the stack.

putField

public void putField(Type owner, String name, Type type)
Generates the instruction to store the top stack value in a non static field.

Parameters: owner the class in which the field is defined. name the name of the field. type the type of the field.

putStatic

public void putStatic(Type owner, String name, Type type)
Generates the instruction to store the top stack value in a static field.

Parameters: owner the class in which the field is defined. name the name of the field. type the type of the field.

ret

public void ret(int local)
Generates a RET instruction.

Parameters: local a local variable identifier, as returned by {@link #newLocal newLocal}.

returnValue

public void returnValue()
Generates the instruction to return the top stack value to the caller.

storeArg

public void storeArg(int arg)
Generates the instruction to store the top stack value in the given method argument.

Parameters: arg the index of a method argument.

storeLocal

public void storeLocal(int local)
Generates the instruction to store the top stack value in the given local variable.

Parameters: local a local variable identifier, as returned by {@link #newLocal newLocal}.

storeLocal

public void storeLocal(int local, Type type)
Generates the instruction to store the top stack value in the given local variable.

Parameters: local a local variable identifier, as returned by {@link #newLocal newLocal}. type the type of this local variable.

swap

public void swap()
Generates a SWAP instruction.

swap

public void swap(Type prev, Type type)
Generates the instructions to swap the top two stack values.

Parameters: prev type of the top - 1 stack value. type type of the top stack value.

tableSwitch

public void tableSwitch(int[] keys, TableSwitchGenerator generator)
Generates the instructions for a switch statement.

Parameters: keys the switch case keys. generator a generator to generate the code for the switch cases.

tableSwitch

public void tableSwitch(int[] keys, TableSwitchGenerator generator, boolean useTable)
Generates the instructions for a switch statement.

Parameters: 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.

throwException

public void throwException()
Generates the instruction to throw an exception.

throwException

public void throwException(Type type, String msg)
Generates the instructions to create and throw an exception. The exception class must have a constructor with a single String argument.

Parameters: type the class of the exception to be thrown. msg the detailed message of the exception.

unbox

public void unbox(Type type)
Generates the instructions to unbox the top stack value. This value is replaced by its unboxed equivalent on top of the stack.

Parameters: type the type of the top stack value.