Top Description Fields Constructors Methods
org.eclipse.persistence.internal.libraries.asm.commons

public Class GeneratorAdapter

extends LocalVariablesSorter
Class Inheritance
Known Direct Subclasses
org.eclipse.persistence.internal.libraries.asm.commons.AdviceAdapter
Imports
java.util.ArrayList, .Arrays, .List, org.eclipse.persistence.internal.libraries.asm.ClassVisitor, .ConstantDynamic, .Handle, .Label, .MethodVisitor, .Opcodes, .Type

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(0);
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();
Authors
Juozas Baliuka, Chris Nokleberg, Eric Bruneton, Prashant Deva

Field Summary

Modifier and TypeField and Description
private final int
access

The access flags of the visited method.

public static final int
ADD

Constant for the math method.

public static final int
AND

Constant for the math method.

private final Type[]
argumentTypes

The argument types of the visited method.

private static final Type
private static final Method
private static final Type
private static final Method
private static final Type
private static final String
public static final int
DIV

Constant for the math method.

private static final Type
private static final Method
public static final int
EQ

Constant for the ifCmp method.

private static final Type
private static final Method
public static final int
GE

Constant for the ifCmp method.

public static final int
GT

Constant for the ifCmp method.

private static final Method
private static final Type
public static final int
LE

Constant for the ifCmp method.

private final List<Type>
localTypes

The types of the local variables of the visited method.

private static final Type
private static final Method
public static final int
LT

Constant for the ifCmp method.

public static final int
MUL

Constant for the math method.

private final String
name

The name of the visited method.

public static final int
NE

Constant for the ifCmp method.

public static final int
NEG

Constant for the math method.

private static final Type
private static final Type
public static final int
OR

Constant for the math method.

public static final int
REM

Constant for the math method.

private final Type
returnType

The return type of the visited method.

public static final int
SHL

Constant for the math method.

private static final Type
public static final int
SHR

Constant for the math method.

public static final int
SUB

Constant for the math method.

public static final int
USHR

Constant for the math method.

public static final int
XOR

Constant for the math method.

Inherited from org.eclipse.persistence.internal.libraries.asm.commons.LocalVariablesSorter:
firstLocalnextLocal

Constructor Summary

AccessConstructor and Description
public
GeneratorAdapter(final MethodVisitor
the method visitor to which this adapter delegates calls.
methodVisitor
,
final int
the method's access flags (see Opcodes).
access
,
final String
the method's name.
name
,
final String
the method's descriptor (see Type).
descriptor
)

Constructs a new GeneratorAdapter.

protected
GeneratorAdapter(final int
the ASM API version implemented by this visitor. Must be one of the ASMx values in Opcodes.
api
,
final MethodVisitor
the method visitor to which this adapter delegates calls.
methodVisitor
,
final int
the method's access flags (see Opcodes).
access
,
final String
the method's name.
name
,
final String
the method's descriptor (see Type).
descriptor
)

Constructs a new GeneratorAdapter.

public
GeneratorAdapter(final int
access flags of the adapted method.
access
,
final Method
the adapted method.
method
,
final MethodVisitor
the method visitor to which this adapter delegates calls.
methodVisitor
)

Constructs a new GeneratorAdapter.

public
GeneratorAdapter(final int
access flags of the adapted method.
access
,
final Method
the adapted method.
method
,
final String
the signature of the adapted method (may be null).
signature
,
final Type[]
the exceptions thrown by the adapted method (may be null).
exceptions
,
final ClassVisitor
the class visitor to which this adapter delegates calls.
classVisitor
)

Constructs a new GeneratorAdapter.

Method Summary

Modifier and TypeMethod and Description
public void
arrayLength()

Generates the instruction to compute the length of an array.

public void
arrayLoad(final Type
the type of the array element to be loaded.
type
)

Generates the instruction to load an element from an array.

public void
arrayStore(final Type
the type of the array element to be stored.
type
)

Generates the instruction to store an element in an array.

public void
box(final Type
the type of the top stack value.
type
)

Generates the instructions to box the top stack value.

public void
cast(final Type
the type of the top stack value
from
,
final Type
the type into which this value must be cast.
to
)

Generates the instructions to cast a numerical value from one type to another.

public void
catchException(final Label
beginning of the exception handler's scope (inclusive).
start
,
final Label
end of the exception handler's scope (exclusive).
end
,
final Type
internal name of the type of exceptions handled by the handler (see Type#getInternalName()).
exception
)

Marks the start of an exception handler.

public void
checkCast(final Type
a class or interface type.
type
)

Generates the instruction to check that the top stack value is of the given type.

public void
dup()

Generates a DUP instruction.

public void
dup2()

Generates a DUP2 instruction.

public void
dup2X1()

Generates a DUP2_X1 instruction.

public void
dup2X2()

Generates a DUP2_X2 instruction.

public void
dupX1()

Generates a DUP_X1 instruction.

public void
dupX2()

Generates a DUP_X2 instruction.

public void
endMethod()

Marks the end of the visited method.

private void
fieldInsn(final int
the instruction's opcode.
opcode
,
final Type
the class in which the field is defined.
ownerType
,
final String
the name of the field.
name
,
final Type
the type of the field.
fieldType
)

Generates a get field or set field instruction.

public int
private int

Returns:

the index of the given method argument in the frame's local variables array.
getArgIndex
(final int
the index of a method argument.
arg
)

Returns the index of the given method argument in the frame's local variables array.

public Type[]
private static Type
getBoxedType(final Type type)

public void
getField(final Type
the class in which the field is defined.
owner
,
final String
the name of the field.
name
,
final Type
the type of the field.
type
)

Generates the instruction to push the value of a non static field on the stack.

private static String[]

Returns:

the internal names of the given types (see Type#getInternalName()).
getInternalNames
(final Type[]
a set of types.
types
)

Returns the internal names of the given types.

public Type

Returns:

the type of the given local variable.
getLocalType
(final int
a local variable identifier, as returned by LocalVariablesSorter#newLocal(Type).
local
)

Returns the type of the given local variable.

public String
public Type
public void
getStatic(final Type
the class in which the field is defined.
owner
,
final String
the name of the field.
name
,
final Type
the type of the field.
type
)

Generates the instruction to push the value of a static field on the stack.

public void
goTo(final Label
where to jump if the condition is true.
label
)

Generates the instruction to jump to the given label.

public void
ifCmp(final Type
the type of the top two stack values.
type
,
final int
how these values must be compared. One of EQ, NE, LT, GE, GT, LE.
mode
,
final Label
where to jump if the comparison result is true.
label
)

Generates the instructions to jump to a label based on the comparison of the top two stack values.

public void
ifICmp(final int
how these values must be compared. One of EQ, NE, LT, GE, GT, LE.
mode
,
final Label
where to jump if the comparison result is true.
label
)

Generates the instructions to jump to a label based on the comparison of the top two integer stack values.

public void
ifNonNull(final Label
where to jump if the condition is true.
label
)

Generates the instruction to jump to the given label if the top stack value is not null.

public void
ifNull(final Label
where to jump if the condition is true.
label
)

Generates the instruction to jump to the given label if the top stack value is null.

public void
ifZCmp(final int
how these values must be compared. One of EQ, NE, LT, GE, GT, LE.
mode
,
final Label
where to jump if the comparison result is true.
label
)

Generates the instructions to jump to a label based on the comparison of the top integer stack value with zero.

public void
iinc(final int
the local variable to be incremented.
local
,
final int
the amount by which the local variable must be incremented.
amount
)

Generates the instruction to increment the given local variable.

public void
instanceOf(final Type
a class or interface type.
type
)

Generates the instruction to test if the top stack value is of the given type.

public void
invokeConstructor(final Type
the class in which the constructor is defined.
type
,
final Method
the constructor to be invoked.
method
)

Generates the instruction to invoke a constructor.

public void
invokeDynamic(final String
the method's name.
name
,
final String
the method's descriptor (see Type).
descriptor
,
final Handle
the bootstrap method.
bootstrapMethodHandle
,
final Object...
the bootstrap method constant arguments. Each argument must be an Integer, Float, Long, Double, String, Type or Handle value. This method is allowed to modify the content of the array so a caller should expect that this array may change.
bootstrapMethodArguments
)

Generates an invokedynamic instruction.

private void
invokeInsn(final int
the instruction's opcode.
opcode
,
final Type
the class in which the method is defined.
type
,
final Method
the method to be invoked.
method
,
final boolean
whether the 'type' class is an interface or not.
isInterface
)

Generates an invoke method instruction.

public void
invokeInterface(final Type
the class in which the method is defined.
owner
,
final Method
the method to be invoked.
method
)

Generates the instruction to invoke an interface method.

public void
invokeStatic(final Type
the class in which the method is defined.
owner
,
final Method
the method to be invoked.
method
)

Generates the instruction to invoke a static method.

public void
invokeVirtual(final Type
the class in which the method is defined.
owner
,
final Method
the method to be invoked.
method
)

Generates the instruction to invoke a normal method.

public void
loadArg(final int
the index of a method argument.
arg
)

Generates the instruction to load the given method argument on the stack.

public void
loadArgArray()

Generates the instructions to load all the method arguments on the stack, as a single object array.

public void
loadArgs(final int
the index of the first method argument to be loaded.
arg
,
final int
the number of method arguments to be loaded.
count
)

Generates the instructions to load the given method arguments on the stack.

public void
loadArgs()

Generates the instructions to load all the method arguments on the stack.

private void
loadInsn(final Type
the type of the local variable to be loaded.
type
,
final int
an index in the frame's local variables array.
index
)

Generates the instruction to push a local variable on the stack.

public void
loadLocal(final int
a local variable identifier, as returned by LocalVariablesSorter#newLocal(Type).
local
)

Generates the instruction to load the given local variable on the stack.

public void
loadLocal(final int
a local variable identifier, as returned by LocalVariablesSorter#newLocal(Type).
local
,
final Type
the type of this local variable.
type
)

Generates the instruction to load the given local variable on the stack.

public void
loadThis()

Generates the instruction to load 'this' on the stack.

public void
mark(final Label
a label.
label
)

Marks the current code position with the given label.

public Label

Returns:

the label that was created to mark the current code position.
mark
()

Marks the current code position with a new label.

public void
math(final int
a mathematical or logical operation. Must be one of ADD, SUB, MUL, DIV, REM, NEG, SHL, SHR, USHR, AND, OR, XOR.
op
,
final Type
the type of the operand(s) for this operation.
type
)

Generates the instruction to do the specified mathematical or logical operation.

public void
monitorEnter()

Generates the instruction to get the monitor of the top stack value.

public void
monitorExit()

Generates the instruction to release the monitor of the top stack value.

public void
newArray(final Type
the type of the array elements.
type
)

Generates the instruction to create a new array.

public void
newInstance(final Type
the class of the object to be created.
type
)

Generates the instruction to create a new object.

public Label

Returns:

a new Label.
newLabel
()

Constructs a new Label.

public void
not()

Generates the instructions to compute the bitwise negation of the top stack value.

public void
pop()

Generates a POP instruction.

public void
pop2()

Generates a POP2 instruction.

public void
push(final boolean
the value to be pushed on the stack.
value
)

Generates the instruction to push the given value on the stack.

public void
push(final int
the value to be pushed on the stack.
value
)

Generates the instruction to push the given value on the stack.

public void
push(final long
the value to be pushed on the stack.
value
)

Generates the instruction to push the given value on the stack.

public void
push(final float
the value to be pushed on the stack.
value
)

Generates the instruction to push the given value on the stack.

public void
push(final double
the value to be pushed on the stack.
value
)

Generates the instruction to push the given value on the stack.

public void
push(final String
the value to be pushed on the stack. May be null.
value
)

Generates the instruction to push the given value on the stack.

public void
push(final Type
the value to be pushed on the stack.
value
)

Generates the instruction to push the given value on the stack.

public void
push(final Handle
the handle to be pushed on the stack.
handle
)

Generates the instruction to push a handle on the stack.

public void
push(final ConstantDynamic
the constant dynamic to be pushed on the stack.
constantDynamic
)

Generates the instruction to push a constant dynamic on the stack.

public void
putField(final Type
the class in which the field is defined.
owner
,
final String
the name of the field.
name
,
final Type
the type of the field.
type
)

Generates the instruction to store the top stack value in a non static field.

public void
putStatic(final Type
the class in which the field is defined.
owner
,
final String
the name of the field.
name
,
final Type
the type of the field.
type
)

Generates the instruction to store the top stack value in a static field.

public void
ret(final int
a local variable identifier, as returned by LocalVariablesSorter#newLocal(Type).
local
)

Generates a RET instruction.

public void
returnValue()

Generates the instruction to return the top stack value to the caller.

protected void
setLocalType(final int
a local variable identifier, as returned by newLocal.
local
,
final Type
the type of the value being stored in the local variable.
type
)

Overrides org.eclipse.persistence.internal.libraries.asm.commons.LocalVariablesSorter.setLocalType.

Notifies subclasses that a local variable has been added or remapped.
public void
storeArg(final int
the index of a method argument.
arg
)

Generates the instruction to store the top stack value in the given method argument.

private void
storeInsn(final Type
the type of the local variable to be stored.
type
,
final int
an index in the frame's local variables array.
index
)

Generates the instruction to store the top stack value in a local variable.

public void
storeLocal(final int
a local variable identifier, as returned by LocalVariablesSorter#newLocal(Type).
local
)

Generates the instruction to store the top stack value in the given local variable.

public void
storeLocal(final int
a local variable identifier, as returned by LocalVariablesSorter#newLocal(Type).
local
,
final Type
the type of this local variable.
type
)

Generates the instruction to store the top stack value in the given local variable.

public void
swap()

Generates a SWAP instruction.

public void
swap(final Type
type of the top - 1 stack value.
prev
,
final Type
type of the top stack value.
type
)

Generates the instructions to swap the top two stack values.

public void
tableSwitch(final int[]
the switch case keys.
keys
,
final TableSwitchGenerator
a generator to generate the code for the switch cases.
generator
)

Generates the instructions for a switch statement.

public void
tableSwitch(final int[]
the switch case keys.
keys
,
final TableSwitchGenerator
a generator to generate the code for the switch cases.
generator
,
final boolean
true to use a TABLESWITCH instruction, or false to use a LOOKUPSWITCH instruction.
useTable
)

Generates the instructions for a switch statement.

public void
throwException()

Generates the instruction to throw an exception.

public void
throwException(final Type
the class of the exception to be thrown.
type
,
final String
the detailed message of the exception.
message
)

Generates the instructions to create and throw an exception.

private void
typeInsn(final int
the instruction's opcode.
opcode
,
final Type
the instruction's operand.
type
)

Generates a type dependent instruction.

public void
unbox(final Type
the type of the top stack value.
type
)

Generates the instructions to unbox the top stack value.

public void
valueOf(final Type
the type of the top stack value.
type
)

Generates the instructions to box the top stack value using Java 5's valueOf() method.

Inherited from org.eclipse.persistence.internal.libraries.asm.commons.LocalVariablesSorter:
newLocalnewLocalMappingupdateNewLocalsvisitFramevisitIincInsnvisitLocalVariablevisitLocalVariableAnnotationvisitMaxsvisitVarInsn