Class GeneratorAdapter
java.lang.Object
com.googlecode.aviator.asm.MethodVisitor
com.googlecode.aviator.asm.commons.LocalVariablesSorter
com.googlecode.aviator.asm.commons.GeneratorAdapter
- Direct Known Subclasses:
AdviceAdapter
A
MethodVisitor
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();
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
Access flags of the method visited by this adapter.static final int
Constant for themath
method.static final int
Constant for themath
method.private final Type[]
Argument types of the method visited by this adapter.private static final Type
private static final Method
private static final Type
private static final Method
private static final Type
private static final String
static final int
Constant for themath
method.private static final Type
private static final Method
static final int
Constant for theifCmp
method.private static final Type
private static final Method
static final int
Constant for theifCmp
method.static final int
Constant for theifCmp
method.private static final Method
private static final Type
static final int
Constant for theifCmp
method.Types of the local variables of the method visited by this adapter.private static final Type
private static final Method
static final int
Constant for theifCmp
method.static final int
Constant for themath
method.static final int
Constant for theifCmp
method.static final int
Constant for themath
method.private static final Type
private static final Type
static final int
Constant for themath
method.static final int
Constant for themath
method.private final Type
Return type of the method visited by this adapter.static final int
Constant for themath
method.private static final Type
static final int
Constant for themath
method.static final int
Constant for themath
method.static final int
Constant for themath
method.static final int
Constant for themath
method.Fields inherited from class com.googlecode.aviator.asm.commons.LocalVariablesSorter
firstLocal, nextLocal
Fields inherited from class com.googlecode.aviator.asm.MethodVisitor
api, mv
-
Constructor Summary
ConstructorsModifierConstructorDescriptionGeneratorAdapter
(int access, Method method, MethodVisitor mv) Creates a newGeneratorAdapter
.GeneratorAdapter
(int access, Method method, String signature, Type[] exceptions, ClassVisitor cv) Creates a newGeneratorAdapter
.protected
GeneratorAdapter
(int api, MethodVisitor mv, int access, String name, String desc) Creates a newGeneratorAdapter
.GeneratorAdapter
(MethodVisitor mv, int access, String name, String desc) Creates a newGeneratorAdapter
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Generates the instruction to compute the length of an array.void
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
Generates the instructions to box the top stack value.void
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
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
Marks the end of the visited method.private void
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.private static Type
getBoxedType
(Type type) void
Generates the instruction to push the value of a non static field on the stack.private static String[]
getInternalNames
(Type[] types) Returns the internal names of the given types.getLocalType
(int local) Returns the type of the given local variable.void
Generates the instruction to push the value of a static field on the stack.void
Generates the instruction to jump to the given label.void
Generates the instructions to jump to a label based on the comparison of the top two stack values.void
Generates the instructions to jump to a label based on the comparison of the top two integer stack values.void
Generates the instruction to jump to the given label if the top stack value is not null.void
Generates the instruction to jump to the given label if the top stack value is null.void
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.void
invokeDynamic
(String name, String desc, Handle bsm, Object... bsmArgs) Generates an invokedynamic instruction.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
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
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
Generates the instruction to load the given local variable on the stack.void
loadThis()
Generates the instruction to load 'this' on the stack.mark()
Marks the current code position with a new label.void
Marks the current code position with the given label.void
Generates the instruction to do the specified mathematical or logical operation.void
Generates the instruction to get the monitor of the top stack value.void
Generates the instruction to release the monitor of the top stack value.void
Generates the instruction to create a new array.void
newInstance
(Type type) Generates the instruction to create a new object.newLabel()
Creates a newLabel
.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
Generates the instruction to push a handle on the stack.void
Generates the instruction to push the given value on the stack.void
Generates the instruction to push the given value on the stack.void
Generates the instruction to store the top stack value in a non static field.void
Generates the instruction to store the top stack value in a static field.void
ret
(int local) Generates a RET instruction.void
Generates the instruction to return the top stack value to the caller.protected void
setLocalType
(int local, Type type) Notifies subclasses that a local variable has been added or remapped.void
storeArg
(int arg) Generates the instruction to store the top stack value in the given method argument.private void
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
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
Generates the instruction to throw an exception.void
throwException
(Type type, String msg) Generates the instructions to create and throw an exception.private void
Generates a type dependent instruction.void
Generates the instructions to unbox the top stack value.void
Generates the instructions to box the top stack value using Java 5's valueOf() method.Methods inherited from class com.googlecode.aviator.asm.commons.LocalVariablesSorter
newLocal, newLocalMapping, updateNewLocals, visitFrame, visitIincInsn, visitLocalVariable, visitMaxs, visitVarInsn
Methods inherited from class com.googlecode.aviator.asm.MethodVisitor
visitAnnotation, visitAnnotationDefault, visitAttribute, visitCode, visitEnd, visitFieldInsn, visitInsn, visitIntInsn, visitInvokeDynamicInsn, visitJumpInsn, visitLabel, visitLdcInsn, visitLineNumber, visitLookupSwitchInsn, visitMethodInsn, visitMultiANewArrayInsn, visitParameterAnnotation, visitTableSwitchInsn, visitTryCatchBlock, visitTypeInsn
-
Field Details
-
CLDESC
- See Also:
-
BYTE_TYPE
-
BOOLEAN_TYPE
-
SHORT_TYPE
-
CHARACTER_TYPE
-
INTEGER_TYPE
-
FLOAT_TYPE
-
LONG_TYPE
-
DOUBLE_TYPE
-
NUMBER_TYPE
-
OBJECT_TYPE
-
BOOLEAN_VALUE
-
CHAR_VALUE
-
INT_VALUE
-
FLOAT_VALUE
-
LONG_VALUE
-
DOUBLE_VALUE
-
ADD
public static final int ADDConstant for themath
method.- See Also:
-
SUB
public static final int SUBConstant for themath
method.- See Also:
-
MUL
public static final int MULConstant for themath
method.- See Also:
-
DIV
public static final int DIVConstant for themath
method.- See Also:
-
REM
public static final int REMConstant for themath
method.- See Also:
-
NEG
public static final int NEGConstant for themath
method.- See Also:
-
SHL
public static final int SHLConstant for themath
method.- See Also:
-
SHR
public static final int SHRConstant for themath
method.- See Also:
-
USHR
public static final int USHRConstant for themath
method.- See Also:
-
AND
public static final int ANDConstant for themath
method.- See Also:
-
OR
public static final int ORConstant for themath
method.- See Also:
-
XOR
public static final int XORConstant for themath
method.- See Also:
-
EQ
public static final int EQConstant for theifCmp
method.- See Also:
-
NE
public static final int NEConstant for theifCmp
method.- See Also:
-
LT
public static final int LTConstant for theifCmp
method.- See Also:
-
GE
public static final int GEConstant for theifCmp
method.- See Also:
-
GT
public static final int GTConstant for theifCmp
method.- See Also:
-
LE
public static final int LEConstant for theifCmp
method.- See Also:
-
access
private final int accessAccess flags of the method visited by this adapter. -
returnType
Return type of the method visited by this adapter. -
argumentTypes
Argument types of the method visited by this adapter. -
localTypes
Types of the local variables of the method visited by this adapter.
-
-
Constructor Details
-
GeneratorAdapter
Creates a newGeneratorAdapter
. Subclasses must not use this constructor. Instead, they must use theGeneratorAdapter(int, MethodVisitor, int, String, String)
version. -
GeneratorAdapter
Creates a newGeneratorAdapter
.- Parameters:
api
- the ASM API version implemented by this visitor. Must be one ofOpcodes.ASM4
.mv
- the method visitor to which this adapter delegates calls.access
- the method's access flags (seeOpcodes
).name
- the method's name.desc
- the method's descriptor (seeType
).
-
GeneratorAdapter
Creates a newGeneratorAdapter
. Subclasses must not use this constructor. Instead, they must use theGeneratorAdapter(int, MethodVisitor, int, String, String)
version.- 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 newGeneratorAdapter
. Subclasses must not use this constructor. Instead, they must use theGeneratorAdapter(int, MethodVisitor, int, String, String)
version.- 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 Details
-
getInternalNames
Returns the internal names of the given types.- Parameters:
types
- a set of types.- Returns:
- the internal names of the given types.
-
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
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
Generates the instruction to push the given value on the stack.- Parameters:
value
- the value to be pushed on the stack.
-
push
Generates the instruction to push a handle on the stack.- Parameters:
handle
- the handle to be pushed on the stack.
-
getArgIndex
private int getArgIndex(int arg) Returns the index of the given method argument in the frame's local variables array.- Parameters:
arg
- the index of a method argument.- Returns:
- the index of the given method argument in the frame's local variables array.
-
loadInsn
Generates the instruction to push a local variable on the stack.- Parameters:
type
- the type of the local variable to be loaded.index
- an index in the frame's local variables array.
-
storeInsn
Generates the instruction to store the top stack value in a local variable.- Parameters:
type
- the type of the local variable to be stored.index
- an index in the frame's local variables array.
-
loadThis
public void loadThis()Generates the instruction to load 'this' on the stack. -
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.
-
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. -
loadArgArray
public void loadArgArray()Generates the instructions to load all the method arguments on the stack, as a single object array. -
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.
-
getLocalType
Returns the type of the given local variable.- Parameters:
local
- a local variable identifier, as returned bynewLocal()
.- Returns:
- the type of the given local variable.
-
setLocalType
Description copied from class:LocalVariablesSorter
Notifies subclasses that a local variable has been added or remapped. The default implementation of this method does nothing.- Overrides:
setLocalType
in classLocalVariablesSorter
- Parameters:
local
- a local variable identifier, as returned bynewLocal()
.type
- the type of the value being stored in the local variable.
-
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 bynewLocal()
.
-
loadLocal
Generates the instruction to load the given local variable on the stack.- Parameters:
local
- a local variable identifier, as returned bynewLocal()
.type
- the type of this local variable.
-
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 bynewLocal()
.
-
storeLocal
Generates the instruction to store the top stack value in the given local variable.- Parameters:
local
- a local variable identifier, as returned bynewLocal()
.type
- the type of this local variable.
-
arrayLoad
Generates the instruction to load an element from an array.- Parameters:
type
- the type of the array element to be loaded.
-
arrayStore
Generates the instruction to store an element in an array.- Parameters:
type
- the type of the array element to be stored.
-
pop
public void pop()Generates a POP instruction. -
pop2
public void pop2()Generates a POP2 instruction. -
dup
public void dup()Generates a DUP instruction. -
dup2
public void dup2()Generates a DUP2 instruction. -
dupX1
public void dupX1()Generates a DUP_X1 instruction. -
dupX2
public void dupX2()Generates a DUP_X2 instruction. -
dup2X1
public void dup2X1()Generates a DUP2_X1 instruction. -
dup2X2
public void dup2X2()Generates a DUP2_X2 instruction. -
swap
public void swap()Generates a SWAP instruction. -
swap
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.
-
math
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.
-
not
public void not()Generates the instructions to compute the bitwise negation of the top stack value. -
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.
-
cast
Generates the instructions to cast a numerical value from one type to another.- Parameters:
from
- the type of the top stack valueto
- the type into which this value must be cast.
-
getBoxedType
-
box
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.
-
valueOf
Generates the instructions to box the top stack value using Java 5's valueOf() method. This value is replaced by its boxed equivalent on top of the stack.- Parameters:
type
- the type of the top stack value.
-
unbox
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.
-
newLabel
Creates a newLabel
.- Returns:
- a new
Label
.
-
mark
Marks the current code position with the given label.- Parameters:
label
- a label.
-
mark
Marks the current code position with a new label.- Returns:
- the label that was created to mark the current code position.
-
ifCmp
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
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.
-
ifZCmp
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.
-
ifNull
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.
-
ifNonNull
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.
-
goTo
Generates the instruction to jump to the given label.- Parameters:
label
- where to jump if the condition is true.
-
ret
public void ret(int local) Generates a RET instruction.- Parameters:
local
- a local variable identifier, as returned bynewLocal()
.
-
tableSwitch
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
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.
-
returnValue
public void returnValue()Generates the instruction to return the top stack value to the caller. -
fieldInsn
Generates a get field or set field instruction.- Parameters:
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.
-
getStatic
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.
-
putStatic
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.
-
getField
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.
-
putField
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.
-
invokeInsn
Generates an invoke method instruction.- Parameters:
opcode
- the instruction's opcode.type
- the class in which the method is defined.method
- the method to be invoked.
-
invokeVirtual
Generates the instruction to invoke a normal method.- Parameters:
owner
- the class in which the method is defined.method
- the method to be invoked.
-
invokeConstructor
Generates the instruction to invoke a constructor.- Parameters:
type
- the class in which the constructor is defined.method
- the constructor to be invoked.
-
invokeStatic
Generates the instruction to invoke a static method.- Parameters:
owner
- the class in which the method is defined.method
- the method to be invoked.
-
invokeInterface
Generates the instruction to invoke an interface method.- Parameters:
owner
- the class in which the method is defined.method
- the method to be invoked.
-
invokeDynamic
Generates an invokedynamic instruction.- Parameters:
name
- the method's name.desc
- the method's descriptor (seeType
).bsm
- the bootstrap method.bsmArgs
- the bootstrap method constant arguments. Each argument must be anInteger
,Float
,Long
,Double
,String
,Type
orHandle
value. This method is allowed to modify the content of the array so a caller should expect that this array may change.
-
typeInsn
Generates a type dependent instruction.- Parameters:
opcode
- the instruction's opcode.type
- the instruction's operand.
-
newInstance
Generates the instruction to create a new object.- Parameters:
type
- the class of the object to be created.
-
newArray
Generates the instruction to create a new array.- Parameters:
type
- the type of the array elements.
-
arrayLength
public void arrayLength()Generates the instruction to compute the length of an array. -
throwException
public void throwException()Generates the instruction to throw an exception. -
throwException
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.
-
checkCast
Generates the instruction to check that the top stack value is of the given type.- Parameters:
type
- a class or interface type.
-
instanceOf
Generates the instruction to test if the top stack value is of the given type.- Parameters:
type
- a class or interface type.
-
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. -
endMethod
public void endMethod()Marks the end of the visited method. -
catchException
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.
-