类 GeneratorAdapter

直接已知子类:
AdviceAdapter, CodeEmitter

public class GeneratorAdapter extends LocalVariablesSorter
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);

 MethodSignature m = MethodSignature.from("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 = MethodSignature.from("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),
         MethodSignature.from("void println (String)"));
 mg.returnValue();
 mg.endMethod();

 cw.visitEnd();
 
作者:
Juozas Baliuka, Chris Nokleberg, Eric Bruneton, Prashant Deva, Harry Yang
  • 字段详细资料

  • 构造器详细资料

    • GeneratorAdapter

      public GeneratorAdapter(MethodVisitor methodVisitor, int access, String name, String descriptor)
      Constructs a new GeneratorAdapter.
      参数:
      methodVisitor - the method visitor to which this adapter delegates calls.
      access - the method's access flags (see Opcodes).
      name - the method's name.
      descriptor - the method's descriptor (see Type).
    • GeneratorAdapter

      public GeneratorAdapter(GeneratorAdapter other)
    • GeneratorAdapter

      public GeneratorAdapter(int access, MethodSignature method, MethodVisitor methodVisitor)
      Constructs a new GeneratorAdapter.
      参数:
      access - access flags of the adapted method.
      method - the adapted method.
      methodVisitor - the method visitor to which this adapter delegates calls.
    • GeneratorAdapter

      public GeneratorAdapter(int access, MethodSignature method, String signature, Type[] exceptions, ClassVisitor classVisitor)
      Constructs a new GeneratorAdapter.
      参数:
      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).
      classVisitor - the class visitor to which this adapter delegates calls.
  • 方法详细资料

    • getInternalNames

      private static String[] getInternalNames(Type[] types)
      Returns the internal names of the given types.
      参数:
      types - a set of types.
      返回:
      the internal names of the given types.
    • getAccess

      public int getAccess()
    • getName

      public String getName()
    • getReturnType

      public Type getReturnType()
    • push

      public void push(boolean value)
      Generates the instruction to push the given value on the stack.
      参数:
      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.
      参数:
      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.
      参数:
      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.
      参数:
      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.
      参数:
      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.
      参数:
      value - the value to be pushed on the stack. May be null.
    • aconst_null

      public void aconst_null()
    • push

      public void push(Type value)
      Generates the instruction to push the given value on the stack.
      参数:
      value - the value to be pushed on the stack.
    • push

      public void push(Handle handle)
      Generates the instruction to push a handle on the stack.
      参数:
      handle - the handle to be pushed on the stack.
    • push

      public void push(ConstantDynamic constantDynamic)
      Generates the instruction to push a constant dynamic on the stack.
      参数:
      constantDynamic - the constant dynamic to be pushed on the stack.
    • getArgIndex

      public int getArgIndex(int arg)
      Returns the index of the given method argument in the frame's local variables array.
      参数:
      arg - the index of a method argument.
      返回:
      the index of the given method argument in the frame's local variables array.
    • loadInsn

      public void loadInsn(Type type, int index)
      Generates the instruction to push a local variable on the stack.
      参数:
      type - the type of the local variable to be loaded.
      index - an index in the frame's local variables array.
    • storeInsn

      public void storeInsn(Type type, int index)
      Generates the instruction to store the top stack value in a local variable.
      参数:
      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.

      Pushes the specified argument of the current method onto the stack.

      参数:
      arg - the index of a method argument. the zero-based index into the argument list
    • loadArgs

      public void loadArgs(int arg, int count)
      Generates the instructions to load the given method arguments on the stack.
      参数:
      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.

      Pushes all of the arguments of the current method onto the stack.

    • loadArgArray

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

      Allocates and fills an Object[] array with the arguments to the current method. Primitive values are inserted as their boxed (Object) equivalents.

    • storeArg

      public void storeArg(int arg)
      Generates the instruction to store the top stack value in the given method argument.
      参数:
      arg - the index of a method argument.
    • getLocalType

      public Type getLocalType(int local)
      Returns the type of the given local variable.
      参数:
      local - a local variable identifier, as returned by LocalVariablesSorter.newLocalIndex(Type).
      返回:
      the type of the given local variable.
    • setLocalType

      protected void setLocalType(int local, Type type)
      从类复制的说明: LocalVariablesSorter
      Notifies subclasses that a local variable has been added or remapped. The default implementation of this method does nothing.
      覆盖:
      setLocalType 在类中 LocalVariablesSorter
      参数:
      local - a local variable identifier, as returned by LocalVariablesSorter.newLocalIndex(cn.taketoday.bytecode.Type).
      type - the type of the value being stored in the local variable.
    • loadLocal

      public void loadLocal(Local local)
    • loadLocal

      public void loadLocal(int local)
      Generates the instruction to load the given local variable on the stack.
      参数:
      local - a local variable identifier, as returned by LocalVariablesSorter.newLocalIndex(Type).
    • loadLocal

      public void loadLocal(int local, Type type)
      Generates the instruction to load the given local variable on the stack.
      参数:
      local - a local variable identifier, as returned by LocalVariablesSorter.newLocalIndex(Type).
      type - the type of this local variable.
    • storeLocal

      public void storeLocal(Local local)
    • storeLocal

      public void storeLocal(int local)
      Generates the instruction to store the top stack value in the given local variable.
      参数:
      local - a local variable identifier, as returned by LocalVariablesSorter.newLocalIndex(Type).
    • storeLocal

      public void storeLocal(int local, Type type)
      Generates the instruction to store the top stack value in the given local variable.
      参数:
      local - a local variable identifier, as returned by LocalVariablesSorter.newLocalIndex(Type).
      type - the type of this local variable.
    • arrayLoad

      public void arrayLoad(Type type)
      Generates the instruction to load an element from an array.
      参数:
      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.
      参数:
      type - the type of the array element to be stored.
    • aastore

      public void aastore()
    • aaload

      public void aaload(int index)
    • aaload

      public void aaload()
    • 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

      public void swap(Type prev, Type type)
      Generates the instructions to swap the top two stack values.
      参数:
      prev - type of the top - 1 stack value.
      type - type of the top stack value.
    • math

      public void math(int op, Type type)
      Generates the instruction to do the specified mathematical or logical operation.
      参数:
      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(Local local, int amount)
    • iinc

      public void iinc(int local, int amount)
      Generates the instruction to increment the given local variable.
      参数:
      local - the local variable to be incremented.
      amount - the amount by which the local variable must be incremented.
    • cast

      public void cast(Type from, Type to)
      Generates the instructions to cast a numerical value from one type to another.
      参数:
      from - the type of the top stack value
      to - the type into which this value must be cast.
    • 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.

      If the argument is a primitive class, replaces the primitive value on the top of the stack with the wrapped (Object) equivalent. For example, char -> Character. If the class is Void, a null is pushed onto the stack instead.

      参数:
      type - the type of the top stack value.
    • valueOf

      public void valueOf(Type type)
      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.
      参数:
      type - the type of the top stack value.
    • 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.

      If the argument is a primitive class, replaces the object on the top of the stack with the unwrapped (primitive) equivalent. For example, Character -> char.

      参数:
      type - the class indicating the desired type of the top stack value
    • newLabel

      public Label newLabel()
      Constructs a new Label.
      返回:
      a new Label.
    • mark

      public void mark(Label label)
      Marks the current code position with the given label.
      参数:
      label - a label.
    • mark

      public Label mark()
      Marks the current code position with a new label.
      返回:
      the label that was created to mark the current code position.
    • 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.
      参数:
      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.
      参数:
      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

      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.
      参数:
      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.
    • ifJump

      public void ifJump(int mode, Label label)
    • ifIcmp

      public void ifIcmp(int mode, Label label)
    • ifNull

      public void ifNull(Label label)
      Generates the instruction to jump to the given label if the top stack value is null.
      参数:
      label - where to jump if the condition 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.
      参数:
      label - where to jump if the condition is true.
    • goTo

      public void goTo(Label label)
      Generates the instruction to jump to the given label.
      参数:
      label - where to jump if the condition is true.
    • ret

      public void ret(int local)
      Generates a RET instruction.
      参数:
      local - a local variable identifier, as returned by LocalVariablesSorter.newLocalIndex(Type).
    • tableSwitch

      public void tableSwitch(int[] keys, TableSwitchGenerator generator)
      Generates the instructions for a switch statement.
      参数:
      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.
      参数:
      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

      public void fieldInsn(int opcode, Type ownerType, String name, Type fieldType)
      Generates a get field or set field instruction.
      参数:
      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

      public void getStatic(Type owner, String name, Type type)
      Generates the instruction to push the value of a static field on the stack.
      参数:
      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.
      参数:
      owner - the class in which the field is defined.
      name - the name of the field.
      type - the type of the field.
    • 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.
      参数:
      owner - the class in which the field is defined.
      name - the name of the field.
      type - the type of the field.
    • putField

      public void putField(Type owner, String name, Type type)
      Generates the instruction to store the top stack value in a non static field.
      参数:
      owner - the class in which the field is defined.
      name - the name of the field.
      type - the type of the field.
    • invokeInsn

      public void invokeInsn(int opcode, Type type, MethodSignature method, boolean isInterface)
      Generates an invoke method instruction.
      参数:
      opcode - the instruction's opcode.
      type - the class in which the method is defined.
      method - the method to be invoked.
      isInterface - whether the 'type' class is an interface or not.
    • invokeVirtual

      public void invokeVirtual(Type owner, MethodSignature method)
      Generates the instruction to invoke a normal method.
      参数:
      owner - the class in which the method is defined.
      method - the method to be invoked.
    • invokeConstructor

      public void invokeConstructor(Type type)
      Generates the instruction to invoke a default constructor.
      参数:
      type - the class in which the constructor is defined.
    • invokeConstructor

      public void invokeConstructor(Type type, MethodSignature method)
      Generates the instruction to invoke a constructor.
      参数:
      type - the class in which the constructor is defined.
      method - the constructor to be invoked.
    • invokeStatic

      public void invokeStatic(Type owner, MethodSignature method)
      Generates the instruction to invoke a static method.
      参数:
      owner - the class in which the method is defined.
      method - the method to be invoked.
    • invokeInterface

      public void invokeInterface(Type owner, MethodSignature method)
      Generates the instruction to invoke an interface method.
      参数:
      owner - the class in which the method is defined.
      method - the method to be invoked.
    • invokeDynamic

      public void invokeDynamic(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments)
      Generates an invokedynamic instruction.
      参数:
      name - the method's name.
      descriptor - the method's descriptor (see Type).
      bootstrapMethodHandle - the bootstrap method.
      bootstrapMethodArguments - 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.
    • typeInsn

      protected void typeInsn(int opcode, Type type)
      Generates a type dependent instruction.
      参数:
      opcode - the instruction's opcode.
      type - the instruction's operand.
    • newInstance

      public void newInstance(Type type)
      Generates the instruction to create a new object.
      参数:
      type - the class of the object to be created.
    • newArray

      public void newArray()
    • newArray

      public void newArray(Type type)
      Generates the instruction to create a new array.
      参数:
      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

      public void throwException(Type type, String message)
      Generates the instructions to create and throw an exception. The exception class must have a constructor with a single String argument.
      参数:
      type - the class of the exception to be thrown.
      message - the detailed message of the exception.
    • checkCast

      public void checkCast(Type type)
      Generates the instruction to check that the top stack value is of the given type.
      参数:
      type - a class or interface type.
    • instanceOf

      public void instanceOf(Type type)
      Generates the instruction to test if the top stack value is of the given type.
      参数:
      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

      public void catchException(Label start, Label end, Type exception)
      Marks the start of an exception handler.
      参数:
      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.