org.objectweb.asm.commons

Class GeneratorAdapter

Implemented Interfaces:
MethodVisitor

public class GeneratorAdapter
extends LocalVariablesSorter

A MethodAdapter with convenient methods to generate code.

Authors:
Juozas Baliuka
Chris Nokleberg
Eric Bruneton

Field Summary

static int
ADD
Constant for the math method.
static int
AND
Constant for the math method.
static int
DIV
Constant for the math method.
static int
EQ
Constant for the ifCmp method.
static int
GE
Constant for the ifCmp method.
static int
GT
Constant for the ifCmp method.
static int
LE
Constant for the ifCmp method.
static int
LT
Constant for the ifCmp method.
static int
MUL
Constant for the math method.
static int
NE
Constant for the ifCmp method.
static int
NEG
Constant for the math method.
static int
OR
Constant for the math method.
static int
REM
Constant for the math method.
static int
SHL
Constant for the math method.
static int
SHR
Constant for the math method.
static int
SUB
Constant for the math method.
static int
USHR
Constant for the math method.
static int
XOR
Constant for the math method.

Fields inherited from class org.objectweb.asm.commons.LocalVariablesSorter

firstLocal

Fields inherited from class org.objectweb.asm.MethodAdapter

mv

Constructor Summary

GeneratorAdapter(int access, Method method, MethodVisitor mv)
Creates a new GeneratorAdapter.

Method Summary

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.
void
getField(Type owner, String name, Type type)
Generates the instruction to push the value of a non static field on the stack.
int
getLocalIndex(int local)
Returns the index of the given local variable.
Type
getLocalType(int local)
Returns the type of the given local variable.
void
getStatic(Type owner, 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
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.
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.
void
loadLocal(int local)
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.
int
newLocal(Type type)
Creates a new local variable of the given type.
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(String value)
Generates the instruction to push the given value on the stack.
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
putField(Type owner, String name, Type type)
Generates the instruction to store the top stack value in a non static field.
void
putStatic(Type owner, String name, Type type)
Generates the instruction to store the top stack value in a static field.
void
returnValue()
Generates the instruction to return the top stack value to the caller.
void
storeArg(int arg)
Generates the instruction to store the top stack value in the given method argument.
void
storeLocal(int local)
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, String msg)
Generates the instructions to create and throw an exception.
void
unbox(Type type)
Generates the instructions to unbox the top stack value.

Methods inherited from class org.objectweb.asm.commons.LocalVariablesSorter

newLocal, visitIincInsn, visitLocalVariable, visitMaxs, visitVarInsn

Methods inherited from class org.objectweb.asm.MethodAdapter

visitAnnotation, visitAnnotationDefault, visitAttribute, visitCode, visitEnd, visitFieldInsn, visitIincInsn, visitInsn, visitIntInsn, visitJumpInsn, visitLabel, visitLdcInsn, visitLineNumber, visitLocalVariable, visitLookupSwitchInsn, visitMaxs, visitMethodInsn, visitMultiANewArrayInsn, visitParameterAnnotation, visitTableSwitchInsn, visitTryCatchBlock, visitTypeInsn, visitVarInsn

Field Details

ADD

public static final int ADD

Field Value:
96


AND

public static final int AND

Field Value:
126


DIV

public static final int DIV

Field Value:
108


EQ

public static final int EQ

Field Value:
153


GE

public static final int GE

Field Value:
156


GT

public static final int GT

Field Value:
157


LE

public static final int LE

Field Value:
158


LT

public static final int LT

Field Value:
155


MUL

public static final int MUL

Field Value:
104


NE

public static final int NE

Field Value:
154


NEG

public static final int NEG

Field Value:
116


OR

public static final int OR

Field Value:
128


REM

public static final int REM

Field Value:
112


SHL

public static final int SHL

Field Value:
120


SHR

public static final int SHR

Field Value:
122


SUB

public static final int SUB

Field Value:
100


USHR

public static final int USHR

Field Value:
124


XOR

public static final int XOR

Field Value:
130

Constructor Details

GeneratorAdapter

public GeneratorAdapter(int access,
                        Method method,
                        MethodVisitor mv)
Creates a new GeneratorAdapter.

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

Method Details

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.


getLocalIndex

public int getLocalIndex(int local)
Returns the index of the given local variable.

Parameters:
local - a local variable identifier, as returned by newLocal.

Returns:
the index of the given local variable in the frame's variables array.


getLocalType

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

Parameters:
local - a local variable identifier, as returned by 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.


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()
Generates the instructions to load all the method arguments on the stack.


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.


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


loadThis

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


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.


mark

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

Parameters:
label - a label.


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

Returns:
a new 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(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(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(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(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(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.


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.


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


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.