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();
| Modifier and Type | Field and Description |
|---|---|
| private final int | access
The access flags of the visited method. |
| public static final int | |
| public static final int | |
| 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 | |
| private static final Type | |
| private static final Method | |
| public static final int | |
| private static final Type | |
| private static final Method | |
| public static final int | |
| public static final int | |
| private static final Method | |
| private static final Type | |
| public static final int | |
| private final List | localTypes
The types of the local variables of the visited method. |
| private static final Type | |
| private static final Method | |
| public static final int | |
| public static final int | |
| private final String | name
The name of the visited method. |
| public static final int | |
| public static final int | |
| private static final Type | |
| private static final Type | |
| public static final int | |
| public static final int | |
| private final Type | returnType
The return type of the visited method. |
| public static final int | |
| private static final Type | |
| public static final int | |
| public static final int | |
| public static final int | |
| public static final int |
| Access | Constructor and Description |
|---|---|
| public | GeneratorAdapter(final MethodVisitor
the method visitor to which this adapter delegates calls. methodVisitor, final int the method's access flags (see access, final String Opcodes).the method's name. name, final String the method's descriptor (see descriptor)Type).Constructs a new |
| protected | GeneratorAdapter(final int
the ASM API version implemented by this visitor. Must be one of the api, final MethodVisitor ASMx values in Opcodes.the method visitor to which this adapter delegates calls. methodVisitor, final int the method's access flags (see access, final String Opcodes).the method's name. name, final String the method's descriptor (see descriptor)Type).Constructs a new |
| 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 |
| 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 |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| 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 | |
| public void | |
| 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 exception)Type#getInternalName()).Marks the start of an exception handler. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| private void | |
| public int | |
| private int | Returns: the index of the given method argument in the frame's local variables array.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 | |
| public void | |
| private static String[] | Returns: the internal names of the given types (seeType#getInternalName()).a set of types. types)Returns the internal names of the given types. |
| public Type | Returns: the type of the given local variable.a local variable identifier, as returned by local)LocalVariablesSorter#newLocal(Type).Returns the type of the given local variable. |
| public String | |
| public Type | |
| public void | |
| public void | |
| 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 | |
| public void | |
| public void | |
| public void | |
| 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 | |
| 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 | |
| private void | |
| public void | loadLocal(final int
a local variable identifier, as returned by local)LocalVariablesSorter#newLocal(Type).Generates the instruction to load the given local variable on the stack. |
| public void | loadLocal(final int
a local variable identifier, as returned by local, final Type LocalVariablesSorter#newLocal(Type).the type of this local variable. type)Generates the instruction to load the given local variable on the stack. |
| public void | |
| public void | |
| public Label | Returns: the label that was created to mark the current code position.Marks the current code position with a new label. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | newInstance(final Type
the class of the object to be created. type)Generates the instruction to create a new object. |
| public Label | |
| public void | |
| public void | |
| public void | |
| 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 | |
| public void | |
| public void | |
| 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 | |
| public void | |
| public void | ret(final int
a local variable identifier, as returned by local)LocalVariablesSorter#newLocal(Type).Generates a RET instruction. |
| public void | |
| protected void | setLocalType(final int
a local variable identifier, as returned by local, final Type newLocal.the type of the value being stored in the local variable. type)Overrides org. |
| 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 | |
| public void | storeLocal(final int
a local variable identifier, as returned by local)LocalVariablesSorter#newLocal(Type).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 local, final Type LocalVariablesSorter#newLocal(Type).the type of this local variable. type)Generates the instruction to store the top stack value in the given local variable. |
| public void | |
| public void | |
| 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 | |
| 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 | |
| public void | |
| public void |