Class CodeBuilder


  • public class CodeBuilder
    extends Object
    A fluent assembler for a method body that records instructions as sized ops and resolves named labels to branch offsets when the body is built or assembled.
    • Method Detail

      • detached

        public static CodeBuilder detached()
        Creates a standalone builder for authoring a detached instruction snippet, independent of the ClassBuilder/MethodBuilder chain.
        Returns:
        a builder with no enclosing method
      • externalLabel

        public CodeBuilder externalLabel​(String name)
        Declares name as an external branch target - an instruction in the host method the snippet will be spliced into, not one defined in the snippet.
        Parameters:
        name - the label name, created on first use
        Returns:
        this builder
      • label

        public CodeBuilder label​(String name)
        Defines a branch target at the current position.
        Parameters:
        name - the label name, created on first use
        Returns:
        this builder
      • iconst

        public CodeBuilder iconst​(int value)
        Appends the shortest int constant push for the value.
        Parameters:
        value - the int to push
        Returns:
        this builder
      • lconst

        public CodeBuilder lconst​(long value)
        Appends a long constant push, using lconst_0/lconst_1 where possible.
        Parameters:
        value - the long to push
        Returns:
        this builder
      • fconst

        public CodeBuilder fconst​(float value)
        Appends a float constant push, using fconst_0/fconst_1/fconst_2 where possible.
        Parameters:
        value - the float to push
        Returns:
        this builder
      • dconst

        public CodeBuilder dconst​(double value)
        Appends a double constant push, using dconst_0/dconst_1 where possible.
        Parameters:
        value - the double to push
        Returns:
        this builder
      • aconst_null

        public CodeBuilder aconst_null()
        Appends aconst_null.
        Returns:
        this builder
      • ldc

        public CodeBuilder ldc​(Object value)
        Appends a constant load, routing numeric boxes to the matching typed constant push.
        Parameters:
        value - a String, Integer, Long, Float or Double
        Returns:
        this builder
        Throws:
        IllegalArgumentException - if the value is of any other type
      • bipush

        public CodeBuilder bipush​(int value)
        Appends bipush, pushing a signed byte widened to int.
        Parameters:
        value - the byte-ranged value to push
        Returns:
        this builder
      • sipush

        public CodeBuilder sipush​(int value)
        Appends sipush, pushing a signed short widened to int.
        Parameters:
        value - the short-ranged value to push
        Returns:
        this builder
      • iload

        public CodeBuilder iload​(int index)
        Appends a int load from a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • lload

        public CodeBuilder lload​(int index)
        Appends a long load from a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • fload

        public CodeBuilder fload​(int index)
        Appends a float load from a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • dload

        public CodeBuilder dload​(int index)
        Appends a double load from a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • aload

        public CodeBuilder aload​(int index)
        Appends a reference load from a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • istore

        public CodeBuilder istore​(int index)
        Appends a int store to a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • lstore

        public CodeBuilder lstore​(int index)
        Appends a long store to a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • fstore

        public CodeBuilder fstore​(int index)
        Appends a float store to a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • dstore

        public CodeBuilder dstore​(int index)
        Appends a double store to a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • astore

        public CodeBuilder astore​(int index)
        Appends a reference store to a local slot.
        Parameters:
        index - the local variable slot
        Returns:
        this builder
      • iaload

        public CodeBuilder iaload()
        Appends iaload, loading a int element from an array.
        Returns:
        this builder
      • laload

        public CodeBuilder laload()
        Appends laload, loading a long element from an array.
        Returns:
        this builder
      • faload

        public CodeBuilder faload()
        Appends faload, loading a float element from an array.
        Returns:
        this builder
      • daload

        public CodeBuilder daload()
        Appends daload, loading a double element from an array.
        Returns:
        this builder
      • aaload

        public CodeBuilder aaload()
        Appends aaload, loading a reference element from an array.
        Returns:
        this builder
      • baload

        public CodeBuilder baload()
        Appends baload, loading a byte or boolean element from an array.
        Returns:
        this builder
      • caload

        public CodeBuilder caload()
        Appends caload, loading a char element from an array.
        Returns:
        this builder
      • saload

        public CodeBuilder saload()
        Appends saload, loading a short element from an array.
        Returns:
        this builder
      • iastore

        public CodeBuilder iastore()
        Appends iastore, storing a int element into an array.
        Returns:
        this builder
      • lastore

        public CodeBuilder lastore()
        Appends lastore, storing a long element into an array.
        Returns:
        this builder
      • fastore

        public CodeBuilder fastore()
        Appends fastore, storing a float element into an array.
        Returns:
        this builder
      • dastore

        public CodeBuilder dastore()
        Appends dastore, storing a double element into an array.
        Returns:
        this builder
      • aastore

        public CodeBuilder aastore()
        Appends aastore, storing a reference element into an array.
        Returns:
        this builder
      • bastore

        public CodeBuilder bastore()
        Appends bastore, storing a byte or boolean element into an array.
        Returns:
        this builder
      • castore

        public CodeBuilder castore()
        Appends castore, storing a char element into an array.
        Returns:
        this builder
      • sastore

        public CodeBuilder sastore()
        Appends sastore, storing a short element into an array.
        Returns:
        this builder
      • arraylength

        public CodeBuilder arraylength()
        Appends arraylength, replacing an array reference with its length.
        Returns:
        this builder
      • pop

        public CodeBuilder pop()
        Appends pop, discarding the top one-slot value.
        Returns:
        this builder
      • pop2

        public CodeBuilder pop2()
        Appends pop2, discarding the top two slots.
        Returns:
        this builder
      • dup

        public CodeBuilder dup()
        Appends dup, duplicating the top one-slot value.
        Returns:
        this builder
      • dup_x1

        public CodeBuilder dup_x1()
        Appends dup_x1, duplicating the top value one slot down.
        Returns:
        this builder
      • dup_x2

        public CodeBuilder dup_x2()
        Appends dup_x2, duplicating the top value two slots down.
        Returns:
        this builder
      • dup2

        public CodeBuilder dup2()
        Appends dup2, duplicating the top two slots.
        Returns:
        this builder
      • dup2_x1

        public CodeBuilder dup2_x1()
        Appends dup2_x1, duplicating the top two slots one slot down.
        Returns:
        this builder
      • dup2_x2

        public CodeBuilder dup2_x2()
        Appends dup2_x2, duplicating the top two slots two slots down.
        Returns:
        this builder
      • swap

        public CodeBuilder swap()
        Appends swap, exchanging the top two one-slot values.
        Returns:
        this builder
      • iadd

        public CodeBuilder iadd()
        Appends iadd, adding the top two int values.
        Returns:
        this builder
      • isub

        public CodeBuilder isub()
        Appends isub, subtracting the top two int values.
        Returns:
        this builder
      • imul

        public CodeBuilder imul()
        Appends imul, multiplying the top two int values.
        Returns:
        this builder
      • idiv

        public CodeBuilder idiv()
        Appends idiv, dividing the top two int values.
        Returns:
        this builder
      • irem

        public CodeBuilder irem()
        Appends irem, taking the remainder of the top two int values.
        Returns:
        this builder
      • ineg

        public CodeBuilder ineg()
        Appends ineg, negating the top int value.
        Returns:
        this builder
      • ladd

        public CodeBuilder ladd()
        Appends ladd, adding the top two long values.
        Returns:
        this builder
      • lsub

        public CodeBuilder lsub()
        Appends lsub, subtracting the top two long values.
        Returns:
        this builder
      • lmul

        public CodeBuilder lmul()
        Appends lmul, multiplying the top two long values.
        Returns:
        this builder
      • ldiv

        public CodeBuilder ldiv()
        Appends ldiv, dividing the top two long values.
        Returns:
        this builder
      • lrem

        public CodeBuilder lrem()
        Appends lrem, taking the remainder of the top two long values.
        Returns:
        this builder
      • lneg

        public CodeBuilder lneg()
        Appends lneg, negating the top long value.
        Returns:
        this builder
      • fadd

        public CodeBuilder fadd()
        Appends fadd, adding the top two float values.
        Returns:
        this builder
      • fsub

        public CodeBuilder fsub()
        Appends fsub, subtracting the top two float values.
        Returns:
        this builder
      • fmul

        public CodeBuilder fmul()
        Appends fmul, multiplying the top two float values.
        Returns:
        this builder
      • fdiv

        public CodeBuilder fdiv()
        Appends fdiv, dividing the top two float values.
        Returns:
        this builder
      • frem

        public CodeBuilder frem()
        Appends frem, taking the remainder of the top two float values.
        Returns:
        this builder
      • fneg

        public CodeBuilder fneg()
        Appends fneg, negating the top float value.
        Returns:
        this builder
      • dadd

        public CodeBuilder dadd()
        Appends dadd, adding the top two double values.
        Returns:
        this builder
      • dsub

        public CodeBuilder dsub()
        Appends dsub, subtracting the top two double values.
        Returns:
        this builder
      • dmul

        public CodeBuilder dmul()
        Appends dmul, multiplying the top two double values.
        Returns:
        this builder
      • ddiv

        public CodeBuilder ddiv()
        Appends ddiv, dividing the top two double values.
        Returns:
        this builder
      • drem

        public CodeBuilder drem()
        Appends drem, taking the remainder of the top two double values.
        Returns:
        this builder
      • dneg

        public CodeBuilder dneg()
        Appends dneg, negating the top double value.
        Returns:
        this builder
      • iand

        public CodeBuilder iand()
        Appends iand, bitwise and of the top two int values.
        Returns:
        this builder
      • ior

        public CodeBuilder ior()
        Appends ior, bitwise or of the top two int values.
        Returns:
        this builder
      • ixor

        public CodeBuilder ixor()
        Appends ixor, bitwise exclusive or of the top two int values.
        Returns:
        this builder
      • ishl

        public CodeBuilder ishl()
        Appends ishl, shifting a int left.
        Returns:
        this builder
      • ishr

        public CodeBuilder ishr()
        Appends ishr, arithmetic right shift of a int.
        Returns:
        this builder
      • iushr

        public CodeBuilder iushr()
        Appends iushr, logical right shift of a int.
        Returns:
        this builder
      • land

        public CodeBuilder land()
        Appends land, bitwise and of the top two long values.
        Returns:
        this builder
      • lor

        public CodeBuilder lor()
        Appends lor, bitwise or of the top two long values.
        Returns:
        this builder
      • lxor

        public CodeBuilder lxor()
        Appends lxor, bitwise exclusive or of the top two long values.
        Returns:
        this builder
      • lshl

        public CodeBuilder lshl()
        Appends lshl, shifting a long left.
        Returns:
        this builder
      • lshr

        public CodeBuilder lshr()
        Appends lshr, arithmetic right shift of a long.
        Returns:
        this builder
      • lushr

        public CodeBuilder lushr()
        Appends lushr, logical right shift of a long.
        Returns:
        this builder
      • i2l

        public CodeBuilder i2l()
        Appends i2l, widening an int to long.
        Returns:
        this builder
      • i2f

        public CodeBuilder i2f()
        Appends i2f, converting an int to float.
        Returns:
        this builder
      • i2d

        public CodeBuilder i2d()
        Appends i2d, converting an int to double.
        Returns:
        this builder
      • l2i

        public CodeBuilder l2i()
        Appends l2i, narrowing a long to int.
        Returns:
        this builder
      • l2f

        public CodeBuilder l2f()
        Appends l2f, converting a long to float.
        Returns:
        this builder
      • l2d

        public CodeBuilder l2d()
        Appends l2d, converting a long to double.
        Returns:
        this builder
      • f2i

        public CodeBuilder f2i()
        Appends f2i, converting a float to int.
        Returns:
        this builder
      • f2l

        public CodeBuilder f2l()
        Appends f2l, converting a float to long.
        Returns:
        this builder
      • f2d

        public CodeBuilder f2d()
        Appends f2d, widening a float to double.
        Returns:
        this builder
      • d2i

        public CodeBuilder d2i()
        Appends d2i, converting a double to int.
        Returns:
        this builder
      • d2l

        public CodeBuilder d2l()
        Appends d2l, converting a double to long.
        Returns:
        this builder
      • d2f

        public CodeBuilder d2f()
        Appends d2f, narrowing a double to float.
        Returns:
        this builder
      • i2b

        public CodeBuilder i2b()
        Appends i2b, truncating an int to byte.
        Returns:
        this builder
      • i2c

        public CodeBuilder i2c()
        Appends i2c, truncating an int to char.
        Returns:
        this builder
      • i2s

        public CodeBuilder i2s()
        Appends i2s, truncating an int to short.
        Returns:
        this builder
      • lcmp

        public CodeBuilder lcmp()
        Appends lcmp, pushing -1, 0 or 1 for the top two longs.
        Returns:
        this builder
      • fcmpl

        public CodeBuilder fcmpl()
        Appends fcmpl, comparing two floats and pushing -1 when either is NaN.
        Returns:
        this builder
      • fcmpg

        public CodeBuilder fcmpg()
        Appends fcmpg, comparing two floats and pushing 1 when either is NaN.
        Returns:
        this builder
      • dcmpl

        public CodeBuilder dcmpl()
        Appends dcmpl, comparing two doubles and pushing -1 when either is NaN.
        Returns:
        this builder
      • dcmpg

        public CodeBuilder dcmpg()
        Appends dcmpg, comparing two doubles and pushing 1 when either is NaN.
        Returns:
        this builder
      • ifeq

        public CodeBuilder ifeq​(String labelName)
        Appends ifeq, branching when the top int is zero.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • ifne

        public CodeBuilder ifne​(String labelName)
        Appends ifne, branching when the top int is non-zero.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • iflt

        public CodeBuilder iflt​(String labelName)
        Appends iflt, branching when the top int is less than zero.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • ifge

        public CodeBuilder ifge​(String labelName)
        Appends ifge, branching when the top int is at least zero.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • ifgt

        public CodeBuilder ifgt​(String labelName)
        Appends ifgt, branching when the top int is greater than zero.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • ifle

        public CodeBuilder ifle​(String labelName)
        Appends ifle, branching when the top int is at most zero.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • if_icmpeq

        public CodeBuilder if_icmpeq​(String labelName)
        Appends if_icmpeq, branching when the top two ints are equal.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • if_icmpne

        public CodeBuilder if_icmpne​(String labelName)
        Appends if_icmpne, branching when the top two ints differ.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • if_icmplt

        public CodeBuilder if_icmplt​(String labelName)
        Appends if_icmplt, branching when the first int is less than the second.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • if_icmpge

        public CodeBuilder if_icmpge​(String labelName)
        Appends if_icmpge, branching when the first int is at least the second.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • if_icmpgt

        public CodeBuilder if_icmpgt​(String labelName)
        Appends if_icmpgt, branching when the first int is greater than the second.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • if_icmple

        public CodeBuilder if_icmple​(String labelName)
        Appends if_icmple, branching when the first int is at most the second.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • if_acmpeq

        public CodeBuilder if_acmpeq​(String labelName)
        Appends if_acmpeq, branching when the top two references are the same object.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • if_acmpne

        public CodeBuilder if_acmpne​(String labelName)
        Appends if_acmpne, branching when the top two references are different objects.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • ifnull

        public CodeBuilder ifnull​(String labelName)
        Appends ifnull, branching when the top reference is null.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • ifnonnull

        public CodeBuilder ifnonnull​(String labelName)
        Appends ifnonnull, branching when the top reference is non-null.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • goto_

        public CodeBuilder goto_​(String labelName)
        Appends an unconditional goto.
        Parameters:
        labelName - the target label, created on first use
        Returns:
        this builder
      • tableswitch

        public CodeBuilder tableswitch​(int low,
                                       int high,
                                       String defaultLabel,
                                       String... caseLabels)
        Emits a tableswitch over the contiguous keys low..high.
        Parameters:
        low - the lowest key
        high - the highest key
        defaultLabel - label taken when the value is outside the key range
        caseLabels - target labels for the keys in ascending order
        Returns:
        this builder
        Throws:
        IllegalArgumentException - if high is below low, or the label count does not match the key range
      • lookupswitch

        public CodeBuilder lookupswitch​(String defaultLabel,
                                        Map<Integer,​String> cases)
        Emits a lookupswitch mapping each key in cases to its target label, with defaultLabel as the fallthrough.
        Parameters:
        defaultLabel - label taken when no key matches
        cases - key to target label mapping
        Returns:
        this builder
      • invokevirtual

        public CodeBuilder invokevirtual​(String owner,
                                         String name,
                                         String descriptor)
        Appends invokevirtual, adding the method reference to the constant pool.
        Parameters:
        owner - internal name of the declaring class
        name - the method name
        descriptor - the method descriptor
        Returns:
        this builder
      • invokespecial

        public CodeBuilder invokespecial​(String owner,
                                         String name,
                                         String descriptor)
        Appends invokespecial, adding the method reference to the constant pool.
        Parameters:
        owner - internal name of the declaring class
        name - the method name
        descriptor - the method descriptor
        Returns:
        this builder
      • invokestatic

        public CodeBuilder invokestatic​(String owner,
                                        String name,
                                        String descriptor)
        Appends invokestatic, adding the method reference to the constant pool.
        Parameters:
        owner - internal name of the declaring class
        name - the method name
        descriptor - the method descriptor
        Returns:
        this builder
      • invokeinterface

        public CodeBuilder invokeinterface​(String owner,
                                           String name,
                                           String descriptor)
        Appends invokeinterface, adding the interface method reference and deriving the argument slot count from the descriptor.
        Parameters:
        owner - internal name of the declaring interface
        name - the method name
        descriptor - the method descriptor
        Returns:
        this builder
      • invokedynamic

        public CodeBuilder invokedynamic​(String name,
                                         String descriptor,
                                         MethodHandle bootstrap,
                                         Object... bsmArgs)
        Appends invokedynamic, registering the bootstrap method and its static arguments on the host class - the enclosing class builder, or the assemble(ClassFile) target when detached.
        Parameters:
        name - the call site name
        descriptor - the call site descriptor
        bootstrap - handle of the bootstrap method
        bsmArgs - static bootstrap arguments; String, Integer, Long, Float, Double, TypeDescriptor and MethodHandle are encoded, any other type is skipped
        Returns:
        this builder
      • getfield

        public CodeBuilder getfield​(String owner,
                                    String name,
                                    String descriptor)
        Appends getfield, adding the field reference to the constant pool.
        Parameters:
        owner - internal name of the declaring class
        name - the field name
        descriptor - the field type descriptor
        Returns:
        this builder
      • putfield

        public CodeBuilder putfield​(String owner,
                                    String name,
                                    String descriptor)
        Appends putfield, adding the field reference to the constant pool.
        Parameters:
        owner - internal name of the declaring class
        name - the field name
        descriptor - the field type descriptor
        Returns:
        this builder
      • getstatic

        public CodeBuilder getstatic​(String owner,
                                     String name,
                                     String descriptor)
        Appends getstatic, adding the field reference to the constant pool.
        Parameters:
        owner - internal name of the declaring class
        name - the field name
        descriptor - the field type descriptor
        Returns:
        this builder
      • putstatic

        public CodeBuilder putstatic​(String owner,
                                     String name,
                                     String descriptor)
        Appends putstatic, adding the field reference to the constant pool.
        Parameters:
        owner - internal name of the declaring class
        name - the field name
        descriptor - the field type descriptor
        Returns:
        this builder
      • new_

        public CodeBuilder new_​(String type)
        Appends new, adding the class reference to the constant pool.
        Parameters:
        type - internal name of the class to allocate
        Returns:
        this builder
      • newarray

        public CodeBuilder newarray​(int arrayType)
        Appends newarray for a primitive element type.
        Parameters:
        arrayType - the JVM array type code for the element
        Returns:
        this builder
      • anewarray

        public CodeBuilder anewarray​(String type)
        Appends anewarray for a reference element type.
        Parameters:
        type - internal name of the element class
        Returns:
        this builder
      • multianewarray

        public CodeBuilder multianewarray​(String descriptor,
                                          int dims)
        Appends multianewarray, consuming one length from the stack per dimension.
        Parameters:
        descriptor - internal name of the array class
        dims - number of dimensions to allocate
        Returns:
        this builder
      • checkcast

        public CodeBuilder checkcast​(String type)
        Appends checkcast.
        Parameters:
        type - internal name to cast the top reference to
        Returns:
        this builder
      • instanceof_

        public CodeBuilder instanceof_​(String type)
        Appends instanceof, replacing the top reference with 1 when it is of the given type.
        Parameters:
        type - internal name to test against
        Returns:
        this builder
      • ireturn

        public CodeBuilder ireturn()
        Appends ireturn, returning an int.
        Returns:
        this builder
      • lreturn

        public CodeBuilder lreturn()
        Appends lreturn, returning a long.
        Returns:
        this builder
      • freturn

        public CodeBuilder freturn()
        Appends freturn, returning a float.
        Returns:
        this builder
      • dreturn

        public CodeBuilder dreturn()
        Appends dreturn, returning a double.
        Returns:
        this builder
      • areturn

        public CodeBuilder areturn()
        Appends areturn, returning a reference.
        Returns:
        this builder
      • vreturn

        public CodeBuilder vreturn()
        Appends return, returning from a void method.
        Returns:
        this builder
      • athrow

        public CodeBuilder athrow()
        Appends athrow, throwing the reference on top of the stack.
        Returns:
        this builder
      • trycatch

        public CodeBuilder trycatch​(String startLabel,
                                    String endLabel,
                                    String handlerLabel,
                                    String exceptionType)
        Records an exception-table region, resolved to offsets when the body is built or assembled.
        Parameters:
        startLabel - label at the first protected instruction
        endLabel - label just past the protected range
        handlerLabel - label at the handler entry
        exceptionType - internal name of the caught class, null to catch any throwable
        Returns:
        this builder
      • nop

        public CodeBuilder nop()
        Appends nop.
        Returns:
        this builder
      • monitorenter

        public CodeBuilder monitorenter()
        Appends monitorenter, acquiring the monitor of the top reference.
        Returns:
        this builder
      • monitorexit

        public CodeBuilder monitorexit()
        Appends monitorexit, releasing the monitor of the top reference.
        Returns:
        this builder
      • iinc

        public CodeBuilder iinc​(int varIndex,
                                int increment)
        Appends iinc, adding a constant to an int local in place.
        Parameters:
        varIndex - the local variable slot
        increment - the signed amount to add
        Returns:
        this builder
      • end

        public MethodBuilder end()
        Ends the body and hands back to the enclosing method builder.
        Returns:
        the enclosing MethodBuilder, or null when this builder was created by detached()
      • assemble

        public CodeWriter.ClonedRange assemble​(ClassFile target)
        Materializes the recorded ops into a detached instruction snapshot resolved against target's constant pool.
        Parameters:
        target - the class whose constant pool backs the snippet and which will host the result
        Returns:
        the assembled snippet