Interface CodeBuilder

All Superinterfaces:
ClassFileBuilder<CodeElement, CodeBuilder>, Consumer<CodeElement>
All Known Subinterfaces:
CodeBuilder.BlockCodeBuilder

public sealed interface CodeBuilder extends ClassFileBuilder<CodeElement, CodeBuilder> permits CodeBuilder.BlockCodeBuilder (not exhaustive)
A builder for code attributes (method bodies). Builders are not created directly; they are passed to handlers by methods such as MethodBuilder.withCode(Consumer) or to code transforms. The elements of a code can be specified abstractly, by passing a CodeElement to ClassFileBuilder.with(ClassFileElement) or concretely by calling the various withXxx methods.

Instruction Factories

CodeBuilder provides convenience methods to create instructions (See JVMS 6.5 Instructions) by their mnemonic, taking necessary operands.
  • Instructions that encode their operands in their opcode, such as aload_<n>, share their factories with their generic version like aload. Note that some constant instructions, such as iconst_1, do not have generic versions, and thus have their own factories.
  • Instructions that accept wide operands, such as ldc2_w or wide, share their factories with their regular version like ldc(ConstantDesc). Note that goto_w has its own factory to avoid short jumps.
  • The goto, instanceof, new, and return instructions' factories are named goto_, instanceOf, new_, and return_ respectively, due to clashes with keywords in the Java programming language.
  • Factories are not provided for instructions jsr, jsr_w, ret, and wide ret, which cannot appear in class files with major version 51 or higher. (JVMS 4.9.1)
Since:
24
See Also:
  • Method Details

    • newLabel

      Label newLabel()
      Returns a fresh unbound label.
      Returns:
      a fresh unbound label
    • startLabel

      Label startLabel()
      Returns the label associated with the beginning of the current block. If the current CodeBuilder is not a "block" builder, such as those provided by block(Consumer) or ifThenElse(Consumer, Consumer), the current block will be the entire method body.
      Returns:
      the label associated with the beginning of the current block
    • endLabel

      Label endLabel()
      Returns the label associated with the end of the current block. If the current CodeBuilder is not a "block" builder, such as those provided by block(Consumer) or ifThenElse(Consumer, Consumer), the current block will be the entire method body.
      Returns:
      the label associated with the end of the current block
    • receiverSlot

      int receiverSlot()
      Returns the local variable slot associated with the receiver..
      Returns:
      the local variable slot associated with the receiver
      Throws:
      IllegalStateException - if this is a static method
    • parameterSlot

      int parameterSlot(int paramNo)
      Returns the local variable slot associated with the specified parameter.. The returned value is adjusted for the receiver slot (if the method is an instance method) and for the requirement that long and double values require two slots.
      Parameters:
      paramNo - the index of the parameter
      Returns:
      the local variable slot associated with the specified parameter
    • allocateLocal

      int allocateLocal(TypeKind typeKind)
      Returns the local variable slot of a fresh local variable. This method makes reasonable efforts to determine which slots are in use and which are not. When transforming a method, fresh locals begin at the maxLocals of the original method. For a method being built directly, fresh locals begin after the last parameter slot.

      If the current code builder is a "block" code builder provided by block(Consumer), ifThen(Consumer), or ifThenElse(Consumer, Consumer), at the end of the block, locals are reset to their value at the beginning of the block.

      Parameters:
      typeKind - the type of the local variable
      Returns:
      the local variable slot of a fresh local variable
    • transforming

      default CodeBuilder transforming(CodeTransform transform, Consumer<CodeBuilder> handler)
      Apply a transform to the code built by a handler, directing results to this builder.
      Parameters:
      transform - the transform to apply to the code built by the handler
      handler - the handler that receives a CodeBuilder to build the code.
      Returns:
      this builder
    • block

      Add a lexical block to the method being built.

      Within this block, the startLabel() and endLabel() correspond to the start and end of the block, and the CodeBuilder.BlockCodeBuilder.breakLabel() also corresponds to the end of the block.

      Parameters:
      handler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the lexical block.
      Returns:
      this builder
    • ifThen

      default CodeBuilder ifThen(Consumer<CodeBuilder.BlockCodeBuilder> thenHandler)
      Add an "if-then" block that is conditional on the boolean value on top of the operand stack.

      The CodeBuilder.BlockCodeBuilder.breakLabel() for the "then" block corresponds to the end of that block.

      Parameters:
      thenHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the if
      Returns:
      this builder
    • ifThen

      default CodeBuilder ifThen(Opcode opcode, Consumer<CodeBuilder.BlockCodeBuilder> thenHandler)
      Add an "if-then" block that is conditional on the value(s) on top of the operand stack in accordance with the given opcode.

      The CodeBuilder.BlockCodeBuilder.breakLabel() for the "then" block corresponds to the end of that block.

      Parameters:
      opcode - the operation code for a branch instructions that accepts one or two operands on the stack
      thenHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the if
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the operation code is not for a branch instruction that accepts one or two operands
    • ifThenElse

      Add an "if-then-else" block that is conditional on the boolean value on top of the operand stack.

      The CodeBuilder.BlockCodeBuilder.breakLabel() for each block corresponds to the end of the "else" block.

      Parameters:
      thenHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the if
      elseHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the else
      Returns:
      this builder
    • ifThenElse

      default CodeBuilder ifThenElse(Opcode opcode, Consumer<CodeBuilder.BlockCodeBuilder> thenHandler, Consumer<CodeBuilder.BlockCodeBuilder> elseHandler)
      Add an "if-then-else" block that is conditional on the value(s) on top of the operand stack in accordance with the given opcode.

      The CodeBuilder.BlockCodeBuilder.breakLabel() for each block corresponds to the end of the "else" block.

      Parameters:
      opcode - the operation code for a branch instructions that accepts one or two operands on the stack
      thenHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the if
      elseHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the else
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the operation code is not for a branch instruction that accepts one or two operands
    • trying

      default CodeBuilder trying(Consumer<CodeBuilder.BlockCodeBuilder> tryHandler, Consumer<CodeBuilder.CatchBuilder> catchesHandler)
      Adds a "try-catch" block comprising one try block and zero or more catch blocks. Exceptions thrown by instructions in the try block may be caught by catch blocks.
      Parameters:
      tryHandler - handler that receives a CodeBuilder to generate the body of the try block.
      catchesHandler - a handler that receives a CodeBuilder.CatchBuilder to generate bodies of catch blocks.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the try block is empty.
      See Also:
    • loadLocal

      default CodeBuilder loadLocal(TypeKind tk, int slot)
      Generate an instruction to load a value from a local variable
      Parameters:
      tk - the load type
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if tk is void or slot is out of range
    • storeLocal

      default CodeBuilder storeLocal(TypeKind tk, int slot)
      Generate an instruction to store a value to a local variable
      Parameters:
      tk - the store type
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if tk is void or slot is out of range
    • branch

      default CodeBuilder branch(Opcode op, Label target)
      Generate a branch instruction
      Parameters:
      op - the branch opcode
      target - the branch target
      Returns:
      this builder
      See Also:
    • return_

      default CodeBuilder return_(TypeKind tk)
      Generate return instruction
      Parameters:
      tk - the return type
      Returns:
      this builder
    • fieldAccess

      default CodeBuilder fieldAccess(Opcode opcode, FieldRefEntry ref)
      Generate an instruction to access a field
      Parameters:
      opcode - the field access opcode
      ref - the field reference
      Returns:
      this builder
      See Also:
    • fieldAccess

      default CodeBuilder fieldAccess(Opcode opcode, ClassDesc owner, String name, ClassDesc type)
      Generate an instruction to access a field
      Parameters:
      opcode - the field access opcode
      owner - the class
      name - the field name
      type - the field type
      Returns:
      this builder
      See Also:
    • invoke

      default CodeBuilder invoke(Opcode opcode, MemberRefEntry ref)
      Generate an instruction to invoke a method or constructor
      Parameters:
      opcode - the invoke opcode
      ref - the interface method or method reference
      Returns:
      this builder
      See Also:
    • invoke

      default CodeBuilder invoke(Opcode opcode, ClassDesc owner, String name, MethodTypeDesc desc, boolean isInterface)
      Generate an instruction to invoke a method or constructor
      Parameters:
      opcode - the invoke opcode
      owner - the class
      name - the method name
      desc - the method type
      isInterface - the interface method invocation indication
      Returns:
      this builder
      See Also:
    • arrayLoad

      default CodeBuilder arrayLoad(TypeKind tk)
      Generate an instruction to load from an array
      Parameters:
      tk - the array element type
      Returns:
      this builder
    • arrayStore

      default CodeBuilder arrayStore(TypeKind tk)
      Generate an instruction to store into an array
      Parameters:
      tk - the array element type
      Returns:
      this builder
    • conversion

      default CodeBuilder conversion(TypeKind fromType, TypeKind toType)
      Generate instruction(s) to convert fromType to toType
      Parameters:
      fromType - the source type
      toType - the target type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - for conversions of void or reference
    • loadConstant

      default CodeBuilder loadConstant(ConstantDesc value)
      Generate an instruction pushing a constant onto the operand stack
      Parameters:
      value - the constant value
      Returns:
      this builder
    • loadConstant

      default CodeBuilder loadConstant(int value)
      Generate an instruction pushing a constant int value onto the operand stack. This is identical to loadConstant(Integer.valueOf(value)).
      Parameters:
      value - the int value
      Returns:
      this builder
      Since:
      24
    • loadConstant

      default CodeBuilder loadConstant(long value)
      Generate an instruction pushing a constant long value onto the operand stack. This is identical to loadConstant(Long.valueOf(value)).
      Parameters:
      value - the long value
      Returns:
      this builder
      Since:
      24
    • loadConstant

      default CodeBuilder loadConstant(float value)
      Generate an instruction pushing a constant float value onto the operand stack. This is identical to loadConstant(Float.valueOf(value)).
      Parameters:
      value - the float value
      Returns:
      this builder
      Since:
      24
    • loadConstant

      default CodeBuilder loadConstant(double value)
      Generate an instruction pushing a constant double value onto the operand stack. This is identical to loadConstant(Double.valueOf(value)).
      Parameters:
      value - the double value
      Returns:
      this builder
      Since:
      24
    • nop

      default CodeBuilder nop()
      Generate a do nothing instruction
      Returns:
      this builder
    • newBoundLabel

      default Label newBoundLabel()
      Create new label bound with current position
      Returns:
      this builder
    • labelBinding

      default CodeBuilder labelBinding(Label label)
      Bind label with current position
      Parameters:
      label - the label
      Returns:
      this builder
    • lineNumber

      default CodeBuilder lineNumber(int line)
      Declare a source line number of the current builder position
      Parameters:
      line - the line number
      Returns:
      this builder
    • exceptionCatch

      default CodeBuilder exceptionCatch(Label start, Label end, Label handler, ClassEntry catchType)
      Declare an exception table entry
      Parameters:
      start - the try block start
      end - the try block end
      handler - the exception handler start
      catchType - the catch type or null to catch all exceptions and errors
      Returns:
      this builder
    • exceptionCatch

      default CodeBuilder exceptionCatch(Label start, Label end, Label handler, Optional<ClassEntry> catchType)
      Declare an exception table entry
      Parameters:
      start - the try block start
      end - the try block end
      handler - the exception handler start
      catchType - the optional catch type, empty to catch all exceptions and errors
      Returns:
      this builder
    • exceptionCatch

      default CodeBuilder exceptionCatch(Label start, Label end, Label handler, ClassDesc catchType)
      Declare an exception table entry
      Parameters:
      start - the try block start
      end - the try block end
      handler - the exception handler start
      catchType - the catch type
      Returns:
      this builder
    • exceptionCatchAll

      default CodeBuilder exceptionCatchAll(Label start, Label end, Label handler)
      Declare an exception table entry catching all exceptions and errors
      Parameters:
      start - the try block start
      end - the try block end
      handler - the exception handler start
      Returns:
      this builder
    • characterRange

      default CodeBuilder characterRange(Label startScope, Label endScope, int characterRangeStart, int characterRangeEnd, int flags)
      Declare a character range entry
      Parameters:
      startScope - the start scope of the character range
      endScope - the end scope of the character range
      characterRangeStart - the encoded start of the character range region (inclusive)
      characterRangeEnd - the encoded end of the character range region (exclusive)
      flags - the flags word, indicating the kind of range
      Returns:
      this builder
    • localVariable

      default CodeBuilder localVariable(int slot, Utf8Entry nameEntry, Utf8Entry descriptorEntry, Label startScope, Label endScope)
      Declare a local variable entry
      Parameters:
      slot - the local variable slot
      nameEntry - the variable name
      descriptorEntry - the variable descriptor
      startScope - the start scope of the variable
      endScope - the end scope of the variable
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • localVariable

      default CodeBuilder localVariable(int slot, String name, ClassDesc descriptor, Label startScope, Label endScope)
      Declare a local variable entry
      Parameters:
      slot - the local variable slot
      name - the variable name
      descriptor - the variable descriptor
      startScope - the start scope of the variable
      endScope - the end scope of the variable
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • localVariableType

      default CodeBuilder localVariableType(int slot, Utf8Entry nameEntry, Utf8Entry signatureEntry, Label startScope, Label endScope)
      Declare a local variable type entry
      Parameters:
      slot - the local variable slot
      nameEntry - the variable name
      signatureEntry - the variable signature
      startScope - the start scope of the variable
      endScope - the end scope of the variable
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • localVariableType

      default CodeBuilder localVariableType(int slot, String name, Signature signature, Label startScope, Label endScope)
      Declare a local variable type entry
      Parameters:
      slot - the local variable slot
      name - the variable name
      signature - the variable signature
      startScope - the start scope of the variable
      endScope - the end scope of the variable
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • aconst_null

      default CodeBuilder aconst_null()
      Generate an instruction pushing the null object reference onto the operand stack
      Returns:
      this builder
    • aaload

      default CodeBuilder aaload()
      Generate an instruction to load a reference from an array
      Returns:
      this builder
    • aastore

      default CodeBuilder aastore()
      Generate an instruction to store into a reference array
      Returns:
      this builder
    • aload

      default CodeBuilder aload(int slot)
      Generate an instruction to load a reference from a local variable

      This may also generate aload_<N> and wide aload instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • anewarray

      default CodeBuilder anewarray(ClassEntry classEntry)
      Generate an instruction to create a new array of reference
      Parameters:
      classEntry - the component type
      Returns:
      this builder
    • anewarray

      default CodeBuilder anewarray(ClassDesc className)
      Generate an instruction to create a new array of reference
      Parameters:
      className - the component type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if className represents a primitive type
    • areturn

      default CodeBuilder areturn()
      Generate an instruction to return a reference from the method
      Returns:
      this builder
    • arraylength

      default CodeBuilder arraylength()
      Generate an instruction to get length of an array
      Returns:
      this builder
    • astore

      default CodeBuilder astore(int slot)
      Generate an instruction to store a reference into a local variable

      This may also generate astore_<N> and wide astore instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • athrow

      default CodeBuilder athrow()
      Generate an instruction to throw an exception or error
      Returns:
      this builder
    • baload

      default CodeBuilder baload()
      Generate an instruction to load a byte from a array
      Returns:
      this builder
    • bastore

      default CodeBuilder bastore()
      Generate an instruction to store into a byte array
      Returns:
      this builder
    • bipush

      default CodeBuilder bipush(int b)
      Generate an instruction pushing an int in the range of byte onto the operand stack.
      Parameters:
      b - the int in the range of byte
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if b is out of range of byte
    • caload

      default CodeBuilder caload()
      Generate an instruction to load a char from an array
      Returns:
      this builder
    • castore

      default CodeBuilder castore()
      Generate an instruction to store into a char array
      Returns:
      this builder
    • checkcast

      default CodeBuilder checkcast(ClassEntry type)
      Generate an instruction to check whether an object is of the given type
      Parameters:
      type - the object type
      Returns:
      this builder
    • checkcast

      default CodeBuilder checkcast(ClassDesc type)
      Generate an instruction to check whether an object is of the given type
      Parameters:
      type - the object type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if type represents a primitive type
    • d2f

      default CodeBuilder d2f()
      Generate an instruction to convert a double into a float
      Returns:
      this builder
    • d2i

      default CodeBuilder d2i()
      Generate an instruction to convert a double into an int
      Returns:
      this builder
    • d2l

      default CodeBuilder d2l()
      Generate an instruction to convert a double into a long
      Returns:
      this builder
    • dadd

      default CodeBuilder dadd()
      Generate an instruction to add a double
      Returns:
      this builder
    • daload

      default CodeBuilder daload()
      Generate an instruction to load a double from an array
      Returns:
      this builder
    • dastore

      default CodeBuilder dastore()
      Generate an instruction to store into a double array
      Returns:
      this builder
    • dcmpg

      default CodeBuilder dcmpg()
      Generate an instruction to add a double
      Returns:
      this builder
    • dcmpl

      default CodeBuilder dcmpl()
      Generate an instruction to compare doubles
      Returns:
      this builder
    • dconst_0

      default CodeBuilder dconst_0()
      Generate an instruction pushing double constant 0 onto the operand stack
      Returns:
      this builder
    • dconst_1

      default CodeBuilder dconst_1()
      Generate an instruction pushing double constant 1 onto the operand stack
      Returns:
      this builder
    • ddiv

      default CodeBuilder ddiv()
      Generate an instruction to divide doubles
      Returns:
      this builder
    • dload

      default CodeBuilder dload(int slot)
      Generate an instruction to load a double from a local variable

      This may also generate dload_<N> and wide dload instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • dmul

      default CodeBuilder dmul()
      Generate an instruction to multiply doubles
      Returns:
      this builder
    • dneg

      default CodeBuilder dneg()
      Generate an instruction to negate a double
      Returns:
      this builder
    • drem

      default CodeBuilder drem()
      Generate an instruction to calculate double remainder
      Returns:
      this builder
    • dreturn

      default CodeBuilder dreturn()
      Generate an instruction to return a double from the method
      Returns:
      this builder
    • dstore

      default CodeBuilder dstore(int slot)
      Generate an instruction to store a double into a local variable

      This may also generate dstore_<N> and wide dstore instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • dsub

      default CodeBuilder dsub()
      Generate an instruction to subtract doubles
      Returns:
      this builder
    • dup

      default CodeBuilder dup()
      Generate an instruction to duplicate the top operand stack value
      Returns:
      this builder
    • dup2

      default CodeBuilder dup2()
      Generate an instruction to duplicate the top one or two operand stack value
      Returns:
      this builder
    • dup2_x1

      default CodeBuilder dup2_x1()
      Generate an instruction to duplicate the top one or two operand stack values and insert two or three values down
      Returns:
      this builder
    • dup2_x2

      default CodeBuilder dup2_x2()
      Generate an instruction to duplicate the top one or two operand stack values and insert two, three, or four values down
      Returns:
      this builder
    • dup_x1

      default CodeBuilder dup_x1()
      Generate an instruction to duplicate the top operand stack value and insert two values down
      Returns:
      this builder
    • dup_x2

      default CodeBuilder dup_x2()
      Generate an instruction to duplicate the top operand stack value and insert two or three values down
      Returns:
      this builder
    • f2d

      default CodeBuilder f2d()
      Generate an instruction to convert a float into a double
      Returns:
      this builder
    • f2i

      default CodeBuilder f2i()
      Generate an instruction to convert a float into an int
      Returns:
      this builder
    • f2l

      default CodeBuilder f2l()
      Generate an instruction to convert a float into a long
      Returns:
      this builder
    • fadd

      default CodeBuilder fadd()
      Generate an instruction to add a float
      Returns:
      this builder
    • faload

      default CodeBuilder faload()
      Generate an instruction to load a float from an array
      Returns:
      this builder
    • fastore

      default CodeBuilder fastore()
      Generate an instruction to store into a float array
      Returns:
      this builder
    • fcmpg

      default CodeBuilder fcmpg()
      Generate an instruction to compare floats
      Returns:
      this builder
    • fcmpl

      default CodeBuilder fcmpl()
      Generate an instruction to compare floats
      Returns:
      this builder
    • fconst_0

      default CodeBuilder fconst_0()
      Generate an instruction pushing float constant 0 onto the operand stack
      Returns:
      this builder
    • fconst_1

      default CodeBuilder fconst_1()
      Generate an instruction pushing float constant 1 onto the operand stack
      Returns:
      this builder
    • fconst_2

      default CodeBuilder fconst_2()
      Generate an instruction pushing float constant 2 onto the operand stack
      Returns:
      this builder
    • fdiv

      default CodeBuilder fdiv()
      Generate an instruction to divide floats
      Returns:
      this builder
    • fload

      default CodeBuilder fload(int slot)
      Generate an instruction to load a float from a local variable

      This may also generate fload_<N> and wide fload instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • fmul

      default CodeBuilder fmul()
      Generate an instruction to multiply floats
      Returns:
      this builder
    • fneg

      default CodeBuilder fneg()
      Generate an instruction to negate a float
      Returns:
      this builder
    • frem

      default CodeBuilder frem()
      Generate an instruction to calculate floats remainder
      Returns:
      this builder
    • freturn

      default CodeBuilder freturn()
      Generate an instruction to return a float from the method
      Returns:
      this builder
    • fstore

      default CodeBuilder fstore(int slot)
      Generate an instruction to store a float into a local variable

      This may also generate fstore_<N> and wide fstore instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • fsub

      default CodeBuilder fsub()
      Generate an instruction to subtract floats
      Returns:
      this builder
    • getfield

      default CodeBuilder getfield(FieldRefEntry ref)
      Generate an instruction to fetch field from an object
      Parameters:
      ref - the field reference
      Returns:
      this builder
    • getfield

      default CodeBuilder getfield(ClassDesc owner, String name, ClassDesc type)
      Generate an instruction to fetch field from an object
      Parameters:
      owner - the owner class
      name - the field name
      type - the field type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • getstatic

      default CodeBuilder getstatic(FieldRefEntry ref)
      Generate an instruction to get static field from a class
      Parameters:
      ref - the field reference
      Returns:
      this builder
    • getstatic

      default CodeBuilder getstatic(ClassDesc owner, String name, ClassDesc type)
      Generate an instruction to get static field from a class
      Parameters:
      owner - the owner class
      name - the field name
      type - the field type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • goto_

      default CodeBuilder goto_(Label target)
      Generate an instruction to branch always

      This may also generate goto_w instructions if the FIX_SHORT_JUMPS option is set.

      API Note:
      The instruction's name is goto, which coincides with a reserved keyword of the Java programming language, thus this method is named with an extra _ suffix instead.
      Parameters:
      target - the branch target
      Returns:
      this builder
    • goto_w

      default CodeBuilder goto_w(Label target)
      Generate an instruction to branch always with wide index
      Parameters:
      target - the branch target
      Returns:
      this builder
    • i2b

      default CodeBuilder i2b()
      Generate an instruction to convert an int into a byte
      Returns:
      this builder
    • i2c

      default CodeBuilder i2c()
      Generate an instruction to convert an int into a char
      Returns:
      this builder
    • i2d

      default CodeBuilder i2d()
      Generate an instruction to convert an int into a double
      Returns:
      this builder
    • i2f

      default CodeBuilder i2f()
      Generate an instruction to convert an int into a float
      Returns:
      this builder
    • i2l

      default CodeBuilder i2l()
      Generate an instruction to convert an int into a long
      Returns:
      this builder
    • i2s

      default CodeBuilder i2s()
      Generate an instruction to convert an int into a short
      Returns:
      this builder
    • iadd

      default CodeBuilder iadd()
      Generate an instruction to add an int
      Returns:
      this builder
    • iaload

      default CodeBuilder iaload()
      Generate an instruction to load a int from an array
      Returns:
      this builder
    • iand

      default CodeBuilder iand()
      Generate an instruction to calculate boolean AND of ints
      Returns:
      this builder
    • iastore

      default CodeBuilder iastore()
      Generate an instruction to store into an int array
      Returns:
      this builder
    • iconst_0

      default CodeBuilder iconst_0()
      Generate an instruction pushing int constant 0 onto the operand stack
      Returns:
      this builder
    • iconst_1

      default CodeBuilder iconst_1()
      Generate an instruction pushing int constant 1 onto the operand stack
      Returns:
      this builder
    • iconst_2

      default CodeBuilder iconst_2()
      Generate an instruction pushing int constant 2 onto the operand stack
      Returns:
      this builder
    • iconst_3

      default CodeBuilder iconst_3()
      Generate an instruction pushing int constant 3 onto the operand stack
      Returns:
      this builder
    • iconst_4

      default CodeBuilder iconst_4()
      Generate an instruction pushing int constant 4 onto the operand stack
      Returns:
      this builder
    • iconst_5

      default CodeBuilder iconst_5()
      Generate an instruction pushing int constant 5 onto the operand stack
      Returns:
      this builder
    • iconst_m1

      default CodeBuilder iconst_m1()
      Generate an instruction pushing int constant -1 onto the operand stack
      Returns:
      this builder
    • idiv

      default CodeBuilder idiv()
      Generate an instruction to divide ints
      Returns:
      this builder
    • if_acmpeq

      default CodeBuilder if_acmpeq(Label target)
      Generate an instruction to branch if reference comparison succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • if_acmpne

      default CodeBuilder if_acmpne(Label target)
      Generate an instruction to branch if reference comparison succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • if_icmpeq

      default CodeBuilder if_icmpeq(Label target)
      Generate an instruction to branch if int comparison succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • if_icmpge

      default CodeBuilder if_icmpge(Label target)
      Generate an instruction to branch if int comparison succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • if_icmpgt

      default CodeBuilder if_icmpgt(Label target)
      Generate an instruction to branch if int comparison succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • if_icmple

      default CodeBuilder if_icmple(Label target)
      Generate an instruction to branch if int comparison succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • if_icmplt

      default CodeBuilder if_icmplt(Label target)
      Generate an instruction to branch if int comparison succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • if_icmpne

      default CodeBuilder if_icmpne(Label target)
      Generate an instruction to branch if int comparison succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • ifnonnull

      default CodeBuilder ifnonnull(Label target)
      Generate an instruction to branch if reference is not null
      Parameters:
      target - the branch target
      Returns:
      this builder
    • ifnull

      default CodeBuilder ifnull(Label target)
      Generate an instruction to branch if reference is null
      Parameters:
      target - the branch target
      Returns:
      this builder
    • ifeq

      default CodeBuilder ifeq(Label target)
      Generate an instruction to branch if int comparison with zero succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • ifge

      default CodeBuilder ifge(Label target)
      Generate an instruction to branch if int comparison with zero succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • ifgt

      default CodeBuilder ifgt(Label target)
      Generate an instruction to branch if int comparison with zero succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • ifle

      default CodeBuilder ifle(Label target)
      Generate an instruction to branch if int comparison with zero succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • iflt

      default CodeBuilder iflt(Label target)
      Generate an instruction to branch if int comparison with zero succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • ifne

      default CodeBuilder ifne(Label target)
      Generate an instruction to branch if int comparison with zero succeeds
      Parameters:
      target - the branch target
      Returns:
      this builder
    • iinc

      default CodeBuilder iinc(int slot, int val)
      Generate an instruction to increment a local variable by a constant
      Parameters:
      slot - the local variable slot
      val - the increment value
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot or val is out of range
    • iload

      default CodeBuilder iload(int slot)
      Generate an instruction to load an int from a local variable

      This may also generate iload_<N> and wide iload instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • imul

      default CodeBuilder imul()
      Generate an instruction to multiply ints
      Returns:
      this builder
    • ineg

      default CodeBuilder ineg()
      Generate an instruction to negate an int
      Returns:
      this builder
    • instanceOf

      default CodeBuilder instanceOf(ClassEntry target)
      Generate an instruction to determine if an object is of the given type
      API Note:
      The instruction's name is instanceof, which coincides with a reserved keyword of the Java programming language, thus this method is named with camel case instead.
      Parameters:
      target - the target type
      Returns:
      this builder
    • instanceOf

      default CodeBuilder instanceOf(ClassDesc target)
      Generate an instruction to determine if an object is of the given type
      API Note:
      The instruction's name is instanceof, which coincides with a reserved keyword of the Java programming language, thus this method is named with camel case instead.
      Parameters:
      target - the target type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if target represents a primitive type
    • invokedynamic

      default CodeBuilder invokedynamic(InvokeDynamicEntry ref)
      Generate an instruction to invoke a dynamically-computed call site
      Parameters:
      ref - the dynamic call site
      Returns:
      this builder
    • invokedynamic

      default CodeBuilder invokedynamic(DynamicCallSiteDesc ref)
      Generate an instruction to invoke a dynamically-computed call site
      Parameters:
      ref - the dynamic call site
      Returns:
      this builder
    • invokeinterface

      default CodeBuilder invokeinterface(InterfaceMethodRefEntry ref)
      Generate an instruction to invoke an interface method
      Parameters:
      ref - the interface method reference
      Returns:
      this builder
    • invokeinterface

      default CodeBuilder invokeinterface(ClassDesc owner, String name, MethodTypeDesc type)
      Generate an instruction to invoke an interface method
      Parameters:
      owner - the owner class
      name - the method name
      type - the method type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • invokespecial

      default CodeBuilder invokespecial(InterfaceMethodRefEntry ref)
      Generate an instruction to invoke an instance method; direct invocation of instance initialization methods and methods of the current class and its supertypes
      Parameters:
      ref - the interface method reference
      Returns:
      this builder
    • invokespecial

      default CodeBuilder invokespecial(MethodRefEntry ref)
      Generate an instruction to invoke an instance method; direct invocation of instance initialization methods and methods of the current class and its supertypes
      Parameters:
      ref - the method reference
      Returns:
      this builder
    • invokespecial

      default CodeBuilder invokespecial(ClassDesc owner, String name, MethodTypeDesc type)
      Generate an instruction to invoke an instance method; direct invocation of instance initialization methods and methods of the current class and its supertypes
      Parameters:
      owner - the owner class
      name - the method name
      type - the method type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • invokespecial

      default CodeBuilder invokespecial(ClassDesc owner, String name, MethodTypeDesc type, boolean isInterface)
      Generate an instruction to invoke an instance method; direct invocation of instance initialization methods and methods of the current class and its supertypes
      Parameters:
      owner - the owner class
      name - the method name
      type - the method type
      isInterface - the interface method invocation indication
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • invokestatic

      default CodeBuilder invokestatic(InterfaceMethodRefEntry ref)
      Generate an instruction to invoke a class (static) method
      Parameters:
      ref - the interface method reference
      Returns:
      this builder
    • invokestatic

      default CodeBuilder invokestatic(MethodRefEntry ref)
      Generate an instruction to invoke a class (static) method
      Parameters:
      ref - the method reference
      Returns:
      this builder
    • invokestatic

      default CodeBuilder invokestatic(ClassDesc owner, String name, MethodTypeDesc type)
      Generate an instruction to invoke a class (static) method
      Parameters:
      owner - the owner class
      name - the method name
      type - the method type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • invokestatic

      default CodeBuilder invokestatic(ClassDesc owner, String name, MethodTypeDesc type, boolean isInterface)
      Generate an instruction to invoke a class (static) method
      Parameters:
      owner - the owner class
      name - the method name
      type - the method type
      isInterface - the interface method invocation indication
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • invokevirtual

      default CodeBuilder invokevirtual(MethodRefEntry ref)
      Generate an instruction to invoke an instance method; dispatch based on class
      Parameters:
      ref - the method reference
      Returns:
      this builder
    • invokevirtual

      default CodeBuilder invokevirtual(ClassDesc owner, String name, MethodTypeDesc type)
      Generate an instruction to invoke an instance method; dispatch based on class
      Parameters:
      owner - the owner class
      name - the method name
      type - the method type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • ior

      default CodeBuilder ior()
      Generate an instruction to calculate boolean OR of ints
      Returns:
      this builder
    • irem

      default CodeBuilder irem()
      Generate an instruction to calculate ints remainder
      Returns:
      this builder
    • ireturn

      default CodeBuilder ireturn()
      Generate an instruction to return an int from the method
      Returns:
      this builder
    • ishl

      default CodeBuilder ishl()
      Generate an instruction to shift an int left
      Returns:
      this builder
    • ishr

      default CodeBuilder ishr()
      Generate an instruction to shift an int right
      Returns:
      this builder
    • istore

      default CodeBuilder istore(int slot)
      Generate an instruction to store an int into a local variable

      This may also generate istore_<N> and wide istore instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • isub

      default CodeBuilder isub()
      Generate an instruction to subtract ints
      Returns:
      this builder
    • iushr

      default CodeBuilder iushr()
      Generate an instruction to logical shift an int right
      Returns:
      this builder
    • ixor

      default CodeBuilder ixor()
      Generate an instruction to calculate boolean XOR of ints
      Returns:
      this builder
    • lookupswitch

      default CodeBuilder lookupswitch(Label defaultTarget, List<SwitchCase> cases)
      Generate an instruction to access a jump table by key match and jump
      Parameters:
      defaultTarget - the default jump target
      cases - the switch cases
      Returns:
      this builder
    • l2d

      default CodeBuilder l2d()
      Generate an instruction to convert a long into a double
      Returns:
      this builder
    • l2f

      default CodeBuilder l2f()
      Generate an instruction to convert a long into a float
      Returns:
      this builder
    • l2i

      default CodeBuilder l2i()
      Generate an instruction to convert a long into an int
      Returns:
      this builder
    • ladd

      default CodeBuilder ladd()
      Generate an instruction to add a long
      Returns:
      this builder
    • laload

      default CodeBuilder laload()
      Generate an instruction to load a long from an array
      Returns:
      this builder
    • land

      default CodeBuilder land()
      Generate an instruction to calculate boolean AND of longs
      Returns:
      this builder
    • lastore

      default CodeBuilder lastore()
      Generate an instruction to store into a long array
      Returns:
      this builder
    • lcmp

      default CodeBuilder lcmp()
      Generate an instruction to compare longs
      Returns:
      this builder
    • lconst_0

      default CodeBuilder lconst_0()
      Generate an instruction pushing long constant 0 onto the operand stack
      Returns:
      this builder
    • lconst_1

      default CodeBuilder lconst_1()
      Generate an instruction pushing long constant 1 onto the operand stack
      Returns:
      this builder
    • ldc

      default CodeBuilder ldc(ConstantDesc value)
      Generate an instruction pushing an item from the run-time constant pool onto the operand stack

      This may also generate ldc_w and ldc2_w instructions.

      API Note:
      loadConstant generates more optimal instructions and should be used for general constants if an ldc instruction is not strictly required.
      Parameters:
      value - the constant value
      Returns:
      this builder
    • ldc

      default CodeBuilder ldc(LoadableConstantEntry entry)
      Generate an instruction pushing an item from the run-time constant pool onto the operand stack

      This may also generate ldc_w and ldc2_w instructions.

      Parameters:
      entry - the constant value
      Returns:
      this builder
    • ldiv

      default CodeBuilder ldiv()
      Generate an instruction to divide longs
      Returns:
      this builder
    • lload

      default CodeBuilder lload(int slot)
      Generate an instruction to load a long from a local variable

      This may also generate lload_<N> and wide lload instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • lmul

      default CodeBuilder lmul()
      Generate an instruction to multiply longs
      Returns:
      this builder
    • lneg

      default CodeBuilder lneg()
      Generate an instruction to negate a long
      Returns:
      this builder
    • lor

      default CodeBuilder lor()
      Generate an instruction to calculate boolean OR of longs
      Returns:
      this builder
    • lrem

      default CodeBuilder lrem()
      Generate an instruction to calculate longs remainder
      Returns:
      this builder
    • lreturn

      default CodeBuilder lreturn()
      Generate an instruction to return a long from the method
      Returns:
      this builder
    • lshl

      default CodeBuilder lshl()
      Generate an instruction to shift a long left
      Returns:
      this builder
    • lshr

      default CodeBuilder lshr()
      Generate an instruction to shift a long right
      Returns:
      this builder
    • lstore

      default CodeBuilder lstore(int slot)
      Generate an instruction to store a long into a local variable

      This may also generate lstore_<N> and wide lstore instructions.

      Parameters:
      slot - the local variable slot
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if slot is out of range
    • lsub

      default CodeBuilder lsub()
      Generate an instruction to subtract longs
      Returns:
      this builder
    • lushr

      default CodeBuilder lushr()
      Generate an instruction to logical shift a long left
      Returns:
      this builder
    • lxor

      default CodeBuilder lxor()
      Generate an instruction to calculate boolean XOR of longs
      Returns:
      this builder
    • monitorenter

      default CodeBuilder monitorenter()
      Generate an instruction to enter monitor for an object
      Returns:
      this builder
    • monitorexit

      default CodeBuilder monitorexit()
      Generate an instruction to exit monitor for an object
      Returns:
      this builder
    • multianewarray

      default CodeBuilder multianewarray(ClassEntry array, int dims)
      Generate an instruction to create a new multidimensional array
      Parameters:
      array - the array type
      dims - the number of dimensions
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if dims is out of range
    • multianewarray

      default CodeBuilder multianewarray(ClassDesc array, int dims)
      Generate an instruction to create a new multidimensional array
      Parameters:
      array - the array type
      dims - the number of dimensions
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if array represents a primitive type or if dims is out of range
    • new_

      default CodeBuilder new_(ClassEntry clazz)
      Generate an instruction to create a new object
      API Note:
      The instruction's name is new, which coincides with a reserved keyword of the Java programming language, thus this method is named with an extra _ suffix instead.
      Parameters:
      clazz - the new class type
      Returns:
      this builder
    • new_

      default CodeBuilder new_(ClassDesc clazz)
      Generate an instruction to create a new object
      API Note:
      The instruction's name is new, which coincides with a reserved keyword of the Java programming language, thus this method is named with an extra _ suffix instead.
      Parameters:
      clazz - the new class type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if clazz represents a primitive type
    • newarray

      default CodeBuilder newarray(TypeKind typeKind)
      Generate an instruction to create a new array of a primitive type
      Parameters:
      typeKind - the primitive array type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - when the typeKind is not a legal primitive array component type
    • pop

      default CodeBuilder pop()
      Generate an instruction to pop the top operand stack value
      Returns:
      this builder
    • pop2

      default CodeBuilder pop2()
      Generate an instruction to pop the top one or two operand stack values
      Returns:
      this builder
    • putfield

      default CodeBuilder putfield(FieldRefEntry ref)
      Generate an instruction to set field in an object
      Parameters:
      ref - the field reference
      Returns:
      this builder
    • putfield

      default CodeBuilder putfield(ClassDesc owner, String name, ClassDesc type)
      Generate an instruction to set field in an object
      Parameters:
      owner - the owner class
      name - the field name
      type - the field type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • putstatic

      default CodeBuilder putstatic(FieldRefEntry ref)
      Generate an instruction to set static field in a class
      Parameters:
      ref - the field reference
      Returns:
      this builder
    • putstatic

      default CodeBuilder putstatic(ClassDesc owner, String name, ClassDesc type)
      Generate an instruction to set static field in a class
      Parameters:
      owner - the owner class
      name - the field name
      type - the field type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if owner represents a primitive type
    • return_

      default CodeBuilder return_()
      Generate an instruction to return void from the method
      API Note:
      The instruction's name is return, which coincides with a reserved keyword of the Java programming language, thus this method is named with an extra _ suffix instead.
      Returns:
      this builder
    • saload

      default CodeBuilder saload()
      Generate an instruction to load a short from an array
      Returns:
      this builder
    • sastore

      default CodeBuilder sastore()
      Generate an instruction to store into a short array
      Returns:
      this builder
    • sipush

      default CodeBuilder sipush(int s)
      Generate an instruction pushing an int in the range of short onto the operand stack.
      Parameters:
      s - the int in the range of short
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if s is out of range of short
    • swap

      default CodeBuilder swap()
      Generate an instruction to swap the top two operand stack values
      Returns:
      this builder
    • tableswitch

      default CodeBuilder tableswitch(int low, int high, Label defaultTarget, List<SwitchCase> cases)
      Generate an instruction to access a jump table by index and jump
      Parameters:
      low - the low key value
      high - the high key value
      defaultTarget - the default jump target
      cases - the switch cases
      Returns:
      this builder
    • tableswitch

      default CodeBuilder tableswitch(Label defaultTarget, List<SwitchCase> cases)
      Generate an instruction to access a jump table by index and jump
      Parameters:
      defaultTarget - the default jump target
      cases - the switch cases
      Returns:
      this builder