Class MethodWriter


public final class MethodWriter extends MethodVisitor
A MethodVisitor that generates methods in bytecode form. Each visit method of this class appends the bytecode corresponding to the visited instruction to a byte vector, in the order these methods are called.
  • Field Details

    • cw

      @NonNull public final ClassWriter cw
      The class writer to which this method must be added.
  • Constructor Details

    • MethodWriter

      public MethodWriter(@NonNull ClassWriter cw, int access, @NonNull String name, @NonNull String desc, @Nullable String signature, @Nullable String[] exceptions, boolean computeFrames)
      Initializes this MethodWriter.
      Parameters:
      cw - the class writer in which the method must be added
      access - the method's access flags (see Opcodes)
      name - the method's name
      desc - the method's descriptor (see JavaType)
      signature - the method's signature
      exceptions - the internal names of the method's exceptions
      computeFrames - true if the stack map tables must be recomputed from scratch
  • Method Details

    • visitParameterAnnotation

      @NonNull public AnnotationVisitor visitParameterAnnotation(@org.checkerframework.checker.index.qual.NonNegative int parameter, @NonNull String desc)
      Description copied from class: MethodVisitor
      Visits an annotation on a parameter of the method being visited.
      Overrides:
      visitParameterAnnotation in class MethodVisitor
      Parameters:
      parameter - the parameter index
      desc - the descriptor of the annotation type
      Returns:
      a visitor to visit the annotation values, or null if this visitor is not interested in visiting this annotation
    • visitInsn

      public void visitInsn(int opcode)
      Description copied from class: MethodVisitor
      Visits a zero operand instruction.
      Overrides:
      visitInsn in class MethodVisitor
      Parameters:
      opcode - the opcode of the instruction to be visited: NOP, ACONST_NULL, ICONST_M1, ICONST_0 ICONST_0 to ICONST_5, LCONST_0, LCONST_1, FCONST_0 to FCONST_2, DCONST_0, DCONST_1, IALOAD, LALOAD, FALOAD, DALOAD, AALOAD, BALOAD, CALOAD, SALOAD, IASTORE, LASTORE, FASTORE, DASTORE, AASTORE, BASTORE, CASTORE, SASTORE, POP, POP2, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, SWAP, IADD, LADD, FADD, DADD, ISUB, LSUB, FSUB, DSUB, IMUL, LMUL, FMUL, DMUL, IDIV, LDIV, FDIV, DDIV, IREM, LREM, FREM, DREM, INEG, LNEG, FNEG, DNEG, ISHL, LSHL, ISHR, LSHR, IUSHR, LUSHR, IAND, LAND, IOR, LOR, IXOR, LXOR, I2L, I2F, I2D, L2I, L2F, L2D, F2I, F2L, F2D, D2I, D2L, D2F, I2B, I2C, I2S, LCMP, FCMPL, FCMPG, DCMPL, DCMPG, IRETURN, LRETURN, FRETURN, DRETURN, ARETURN, RETURN, ARRAYLENGTH, ATHROW, MONITORENTER, or MONITOREXIT
    • visitIntInsn

      public void visitIntInsn(int opcode, int operand)
      Description copied from class: MethodVisitor
      Visits an instruction with a single int operand.
      Overrides:
      visitIntInsn in class MethodVisitor
      Parameters:
      opcode - the opcode of the instruction to be visited: BIPUSH, SIPUSH, or NEWARRAY
      operand - the operand of the instruction to be visited: when opcode is BIPUSH, it's between Byte.MIN_VALUE and Byte.MAX_VALUE; when opcode is SIPUSH, it's between Short.MIN_VALUE and Short.MAX_VALUE; when opcode is NEWARRAY, the operand value is one of the ArrayElementType values
    • visitVarInsn

      public void visitVarInsn(int opcode, @org.checkerframework.checker.index.qual.NonNegative int varIndex)
      Description copied from class: MethodVisitor
      Visits a local variable instruction, which loads or stores the value of a local variable.
      Overrides:
      visitVarInsn in class MethodVisitor
      Parameters:
      opcode - the opcode of the local variable instruction to be visited: ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE, LSTORE, FSTORE, DSTORE, ASTORE or RET
      varIndex - the operand of the instruction to be visited, which is the index of a local variable
    • visitTypeInsn

      public void visitTypeInsn(int opcode, @NonNull String typeDesc)
      Description copied from class: MethodVisitor
      Visits a type instruction, which takes the internal name of a class as parameter.
      Overrides:
      visitTypeInsn in class MethodVisitor
      Parameters:
      opcode - the opcode of the instruction to be visited: NEW, ANEWARRAY, CHECKCAST, or INSTANCEOF
      typeDesc - the operand of the instruction, which is the internal name of an object or array class
    • visitFieldInsn

      public void visitFieldInsn(int opcode, @NonNull String owner, @NonNull String name, @NonNull String desc)
      Description copied from class: MethodVisitor
      Visits a field access instruction, which loads or stores the value of a field of an object or a class.
      Overrides:
      visitFieldInsn in class MethodVisitor
      Parameters:
      opcode - the opcode of the instruction to be visited: GETSTATIC, PUTSTATIC, GETFIELD, or PUTFIELD
      owner - the internal name of the field's owner class
      name - the field's name
      desc - the field's descriptor (see JavaType)
    • visitMethodInsn

      public void visitMethodInsn(int opcode, @NonNull String owner, @NonNull String name, @NonNull String desc, boolean itf)
      Description copied from class: MethodVisitor
      Visits a method invocation instruction, which invokes a method or constructor.
      Overrides:
      visitMethodInsn in class MethodVisitor
      Parameters:
      opcode - the opcode of the instruction: INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC, or INVOKEINTERFACE
      owner - the internal name of the method's owner class
      name - the method's name
      desc - the method's descriptor (see JavaType)
      itf - whether the method's owner class is an interface or not
    • visitInvokeDynamicInsn

      public void visitInvokeDynamicInsn(@NonNull String name, @NonNull String desc, @NonNull MethodHandle bsm, @NonNull Object... bsmArgs)
      Description copied from class: MethodVisitor
      Visits an INVOKEDYNAMIC instruction.
      Overrides:
      visitInvokeDynamicInsn in class MethodVisitor
      Parameters:
      name - the method's name
      desc - the method's descriptor (see JavaType)
      bsm - the bootstrap method
      bsmArgs - the bootstrap method constant arguments, where each argument must be an Integer, Float, Long, Double, String, JavaType, or MethodHandle value
    • visitJumpInsn

      public void visitJumpInsn(int opcode, @NonNull Label label)
      Description copied from class: MethodVisitor
      Visits a jump instruction.
      Overrides:
      visitJumpInsn in class MethodVisitor
      Parameters:
      opcode - the opcode of the jump instruction to be visited: IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE, IF_ICMPEQ, IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ACMPEQ, IF_ACMPNE, GOTO, JSR, IFNULL, or IFNONNULL
      label - the operand of the instruction to be visited, which is a label that designates the instruction to which the jump instruction may jump
    • visitLabel

      public void visitLabel(@NonNull Label label)
      Description copied from class: MethodVisitor
      Visits a label, which designates the instruction that will be visited just after it.
      Overrides:
      visitLabel in class MethodVisitor
    • visitLdcInsn

      public void visitLdcInsn(@NonNull Object cst)
      Description copied from class: MethodVisitor
      Visits a LDC instruction.
      Overrides:
      visitLdcInsn in class MethodVisitor
      Parameters:
      cst - the constant to be loaded on the stack, which must be a non null Integer/Float/Long/Double/String, an ObjectType or ArrayType for .class constants, a MethodType, or a MethodHandle
    • visitIincInsn

      public void visitIincInsn(@org.checkerframework.checker.index.qual.NonNegative int varIndex, int increment)
      Description copied from class: MethodVisitor
      Visits an IINC instruction.
      Overrides:
      visitIincInsn in class MethodVisitor
      Parameters:
      varIndex - index of the local variable to be incremented
      increment - amount to increment the local variable by
    • visitTableSwitchInsn

      public void visitTableSwitchInsn(int min, int max, @NonNull Label dflt, @NonNull Label... labels)
      Description copied from class: MethodVisitor
      Visits a TABLESWITCH instruction.
      Overrides:
      visitTableSwitchInsn in class MethodVisitor
      Parameters:
      min - the minimum key value
      max - the maximum key value
      dflt - beginning of the default handler block
      labels - beginnings of the handler blocks; labels[i] is the beginning of the handler block for the min + i key
    • visitLookupSwitchInsn

      public void visitLookupSwitchInsn(@NonNull Label dflt, @NonNull int[] keys, @NonNull Label[] labels)
      Description copied from class: MethodVisitor
      Visits a LOOKUPSWITCH instruction.
      Overrides:
      visitLookupSwitchInsn in class MethodVisitor
      Parameters:
      dflt - beginning of the default handler block
      keys - the values of the keys
      labels - beginnings of the handler blocks; labels[i] is the beginning of the handler block for the keys[i]
    • visitMultiANewArrayInsn

      public void visitMultiANewArrayInsn(@NonNull String desc, @org.checkerframework.checker.index.qual.NonNegative int dims)
      Description copied from class: MethodVisitor
      Visits a MULTIANEWARRAY instruction.
      Overrides:
      visitMultiANewArrayInsn in class MethodVisitor
      Parameters:
      desc - an array type descriptor (see ArrayType)
      dims - number of dimensions of the array to allocate
    • visitTryCatchBlock

      public void visitTryCatchBlock(@NonNull Label start, @NonNull Label end, @NonNull Label handler, @Nullable String type)
      Description copied from class: MethodVisitor
      Visits a try..catch block.
      Overrides:
      visitTryCatchBlock in class MethodVisitor
      Parameters:
      start - beginning of the exception handler's scope (inclusive)
      end - end of the exception handler's scope (exclusive)
      handler - beginning of the exception handler's code
      type - internal name of the type of exceptions handled by the handler, or null to catch any exceptions (for "finally" blocks)
    • visitLocalVariable

      public void visitLocalVariable(@NonNull String name, @NonNull String desc, @Nullable String signature, @NonNull Label start, @NonNull Label end, @org.checkerframework.checker.index.qual.NonNegative int index)
      Description copied from class: MethodVisitor
      Visits a local variable declaration.
      Overrides:
      visitLocalVariable in class MethodVisitor
      Parameters:
      name - the name of the local variable
      desc - the type descriptor of the local variable
      signature - the type signature of the local variable; null when the local variable type does not use generic types
      start - the first instruction corresponding to the scope of this local variable (inclusive)
      end - the last instruction corresponding to the scope of this local variable (exclusive)
      index - the local variable's index
    • visitLineNumber

      public void visitLineNumber(@org.checkerframework.checker.index.qual.NonNegative int line, @NonNull Label start)
      Description copied from class: MethodVisitor
      Visits a line number within the body of the method.
      Overrides:
      visitLineNumber in class MethodVisitor
      Parameters:
      line - a line number, which refers to the source file from which the class was compiled
      start - the first instruction corresponding to this line number
    • visitMaxStack

      public void visitMaxStack(@org.checkerframework.checker.index.qual.NonNegative int maxStack)
      Description copied from class: MethodVisitor
      Visits the maximum stack size of the method.
      Overrides:
      visitMaxStack in class MethodVisitor
    • getSize

      public @org.checkerframework.checker.index.qual.NonNegative int getSize()
      Returns the size of the bytecode of this method.
    • put

      protected void put(@NonNull ByteVector out)
      Puts the bytecode of this method in the given byte vector.
      Overrides:
      put in class BaseWriter
    • getCurrentBlock

      @Nullable public Label getCurrentBlock()