ClassFileBuilder<CodeElement, CodeBuilder>, Consumer<CodeElement>CodeBuilder.BlockCodeBuilderpublic sealed interface CodeBuilder extends ClassFileBuilder<CodeElement, CodeBuilder> permits CodeBuilder.BlockCodeBuilder (not exhaustive)
Code attributes (method bodies). MethodBuilder.withCode(Consumer) is the basic way to obtain a code builder; ClassBuilder.withMethodBody(Utf8Entry, Utf8Entry, int, Consumer) is a shortcut. There are also derived code builders from block(Consumer), which handles code blocks and transforming(CodeTransform, Consumer), which runs transforms on existing handlers, both of which requires a code builder to be available first. Refer to ClassFileBuilder for general guidance and caution around the use of builders for structures in the class file format. Unlike in other builders, the order of member elements in a code builder is significant: they affect the resulting bytecode. Many Class-File API options affect code builders: ClassFile.DeadCodeOption and ClassFile.ShortJumpsOption affect the resulting bytecode, and ClassFile.DeadLabelsOption, ClassFile.DebugElementsOption, ClassFile.LineNumbersOption, ClassFile.StackMapsOption, and ClassFile.AttributesProcessingOption affect the resulting attributes on the built Code attribute, that some elements sent to a code builder is otherwise ignored.
CodeBuilder provides convenience methods to create instructions (See JVMS 6.5 Instructions) by their mnemonic, taking necessary operands.
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. 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. 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. jsr, jsr_w, ret, and wide ret, which cannot appear in class files with major version 51 or higher. (JVMS 4.9.1) They can still be provided via ClassFileBuilder.with(E). | Modifier and Type | Interface | Description |
|---|---|---|
static interface |
CodeBuilder.BlockCodeBuilder |
A builder for blocks of code. |
static interface |
CodeBuilder.CatchBuilder |
A builder to add catch blocks. |
| Modifier and Type | Method | Description |
|---|---|---|
default CodeBuilder |
aaload() |
Generates an instruction to load from a reference array. |
default CodeBuilder |
aastore() |
Generates an instruction to store into a reference array. |
default CodeBuilder |
aconst_null() |
Generates an instruction pushing the null object reference onto the operand stack. |
int |
allocateLocal |
Returns the local variable slot of a fresh local variable. |
default CodeBuilder |
aload |
Generates an instruction to load a reference from a local variable. |
default CodeBuilder |
anewarray |
Generates an instruction to create a new array of reference. |
default CodeBuilder |
anewarray |
Generates an instruction to create a new array of reference. |
default CodeBuilder |
areturn() |
Generates an instruction to return a reference from this method. |
default CodeBuilder |
arraylength() |
Generates an instruction to get the length of an array. |
default CodeBuilder |
arrayLoad |
Generates an instruction to load from an array. |
default CodeBuilder |
arrayStore |
Generates an instruction to store into an array. |
default CodeBuilder |
astore |
Generates an instruction to store a reference into a local variable. |
default CodeBuilder |
athrow() |
Generates an instruction to throw an exception or error. |
default CodeBuilder |
baload() |
|
default CodeBuilder |
bastore() |
|
default CodeBuilder |
bipush |
|
default CodeBuilder |
block |
Adds a lexical block to the method being built. |
default CodeBuilder |
branch |
Generates a branch instruction. |
default CodeBuilder |
caload() |
Generates an instruction to load from a char array. |
default CodeBuilder |
castore() |
Generates an instruction to store into a char array. |
default CodeBuilder |
characterRange |
Declares a character range entry. |
default CodeBuilder |
checkcast |
Generates an instruction to check whether an object is of the given type, throwing a ClassCastException if the check fails. |
default CodeBuilder |
checkcast |
Generates an instruction to check whether an object is of the given type, throwing a ClassCastException if the check fails. |
default CodeBuilder |
conversion |
Generates instruction(s) to convert fromType to toType. |
default CodeBuilder |
d2f() |
|
default CodeBuilder |
d2i() |
|
default CodeBuilder |
d2l() |
|
default CodeBuilder |
dadd() |
Generates an instruction to add two doubles. |
default CodeBuilder |
daload() |
Generates an instruction to load from a double array. |
default CodeBuilder |
dastore() |
Generates an instruction to store into a double array. |
default CodeBuilder |
dcmpg() |
|
default CodeBuilder |
dcmpl() |
|
default CodeBuilder |
dconst_0() |
Generates an instruction pushing double constant 0 onto the operand stack. |
default CodeBuilder |
dconst_1() |
Generates an instruction pushing double constant 1 onto the operand stack. |
default CodeBuilder |
ddiv() |
Generates an instruction to divide doubles. |
default CodeBuilder |
dload |
Generates an instruction to load a double from a local variable. |
default CodeBuilder |
dmul() |
Generates an instruction to multiply doubles. |
default CodeBuilder |
dneg() |
Generates an instruction to negate a double. |
default CodeBuilder |
drem() |
Generates an instruction to calculate double remainder. |
default CodeBuilder |
dreturn() |
Generates an instruction to return a double from this method. |
default CodeBuilder |
dstore |
Generates an instruction to store a double into a local variable. |
default CodeBuilder |
dsub() |
Generates an instruction to subtract doubles. |
default CodeBuilder |
dup() |
Generates an instruction to duplicate the top operand stack value. |
default CodeBuilder |
dup_x1() |
Generates an instruction to duplicate the top operand stack value and insert two values down. |
default CodeBuilder |
dup_x2() |
Generates an instruction to duplicate the top operand stack value and insert two or three values down. |
default CodeBuilder |
dup2() |
Generates an instruction to duplicate the top one or two operand stack value. |
default CodeBuilder |
dup2_x1() |
Generates an instruction to duplicate the top one or two operand stack values and insert two or three values down. |
default CodeBuilder |
dup2_x2() |
Generates an instruction to duplicate the top one or two operand stack values and insert two, three, or four values down. |
Label |
endLabel() |
Returns the label associated with the end of the current block. |
default CodeBuilder |
exceptionCatch |
Declares an exception table entry. |
default CodeBuilder |
exceptionCatch |
Declares an exception table entry. |
default CodeBuilder |
exceptionCatch |
Declares an exception table entry. |
default CodeBuilder |
exceptionCatchAll |
Declares an exception table entry catching all exceptions and errors. |
default CodeBuilder |
f2d() |
|
default CodeBuilder |
f2i() |
|
default CodeBuilder |
f2l() |
|
default CodeBuilder |
fadd() |
Generates an instruction to add two floats. |
default CodeBuilder |
faload() |
Generates an instruction to load from a float array. |
default CodeBuilder |
fastore() |
Generates an instruction to store into a float array. |
default CodeBuilder |
fcmpg() |
|
default CodeBuilder |
fcmpl() |
|
default CodeBuilder |
fconst_0() |
Generates an instruction pushing float constant 0 onto the operand stack. |
default CodeBuilder |
fconst_1() |
Generates an instruction pushing float constant 1 onto the operand stack. |
default CodeBuilder |
fconst_2() |
Generates an instruction pushing float constant 2 onto the operand stack. |
default CodeBuilder |
fdiv() |
Generates an instruction to divide floats. |
default CodeBuilder |
fieldAccess |
Generates an instruction to access a field. |
default CodeBuilder |
fieldAccess |
Generates an instruction to access a field. |
default CodeBuilder |
fload |
Generates an instruction to load a float from a local variable. |
default CodeBuilder |
fmul() |
Generates an instruction to multiply floats. |
default CodeBuilder |
fneg() |
Generates an instruction to negate a float. |
default CodeBuilder |
frem() |
Generates an instruction to calculate floats remainder. |
default CodeBuilder |
freturn() |
Generates an instruction to return a float from this method. |
default CodeBuilder |
fstore |
Generates an instruction to store a float into a local variable. |
default CodeBuilder |
fsub() |
Generates an instruction to subtract floats. |
default CodeBuilder |
getfield |
Generates an instruction to fetch field from an object. |
default CodeBuilder |
getfield |
Generates an instruction to fetch field from an object. |
default CodeBuilder |
getstatic |
Generates an instruction to get static field from a class or interface. |
default CodeBuilder |
getstatic |
Generates an instruction to get static field from a class or interface. |
default CodeBuilder |
goto_ |
Generates an instruction to branch always. |
default CodeBuilder |
goto_w |
Generates an instruction to branch always with wide index. |
default CodeBuilder |
i2b() |
|
default CodeBuilder |
i2c() |
|
default CodeBuilder |
i2d() |
|
default CodeBuilder |
i2f() |
|
default CodeBuilder |
i2l() |
|
default CodeBuilder |
i2s() |
|
default CodeBuilder |
iadd() |
Generates an instruction to add two ints. |
default CodeBuilder |
iaload() |
Generates an instruction to load from an int array. |
default CodeBuilder |
iand() |
|
default CodeBuilder |
iastore() |
Generates an instruction to store into an int array. |
default CodeBuilder |
iconst_0() |
Generates an instruction pushing int constant 0 onto the operand stack. |
default CodeBuilder |
iconst_1() |
Generates an instruction pushing int constant 1 onto the operand stack. |
default CodeBuilder |
iconst_2() |
Generates an instruction pushing int constant 2 onto the operand stack. |
default CodeBuilder |
iconst_3() |
Generates an instruction pushing int constant 3 onto the operand stack. |
default CodeBuilder |
iconst_4() |
Generates an instruction pushing int constant 4 onto the operand stack. |
default CodeBuilder |
iconst_5() |
Generates an instruction pushing int constant 5 onto the operand stack. |
default CodeBuilder |
iconst_m1() |
Generates an instruction pushing int constant -1 onto the operand stack. |
default CodeBuilder |
idiv() |
Generates an instruction to divide ints. |
default CodeBuilder |
if_acmpeq |
Generates an instruction to branch if reference comparison operand1 == operand2 succeeds. |
default CodeBuilder |
if_acmpne |
Generates an instruction to branch if reference comparison operand1 != operand2 succeeds. |
default CodeBuilder |
if_icmpeq |
Generates an instruction to branch if int comparison operand1 == operand2 succeeds. |
default CodeBuilder |
if_icmpge |
Generates an instruction to branch if int comparison operand1 >= operand2 succeeds. |
default CodeBuilder |
if_icmpgt |
Generates an instruction to branch if int comparison operand1 > operand2 succeeds. |
default CodeBuilder |
if_icmple |
Generates an instruction to branch if int comparison operand1 <= operand2 succeeds. |
default CodeBuilder |
if_icmplt |
Generates an instruction to branch if int comparison operand1 < operand2 succeeds. |
default CodeBuilder |
if_icmpne |
Generates an instruction to branch if int comparison operand1 != operand2 succeeds. |
default CodeBuilder |
ifeq |
Generates an instruction to branch if int comparison with zero == 0 succeeds. |
default CodeBuilder |
ifge |
Generates an instruction to branch if int comparison with zero >= 0 succeeds. |
default CodeBuilder |
ifgt |
Generates an instruction to branch if int comparison with zero > 0 succeeds. |
default CodeBuilder |
ifle |
Generates an instruction to branch if int comparison with zero <= 0 succeeds. |
default CodeBuilder |
iflt |
Generates an instruction to branch if int comparison with zero < 0 succeeds. |
default CodeBuilder |
ifne |
Generates an instruction to branch if int comparison with zero != 0 succeeds. |
default CodeBuilder |
ifnonnull |
Generates an instruction to branch if reference is not null. |
default CodeBuilder |
ifnull |
Generates an instruction to branch if reference is null. |
default CodeBuilder |
ifThen |
Adds an "if-then" block that is conditional on the value(s) on top of the operand stack in accordance with the given opcode. |
default CodeBuilder |
ifThen |
Adds an "if-then" block that is conditional on the boolean value on top of the operand stack. |
default CodeBuilder |
ifThenElse |
Adds an "if-then-else" block that is conditional on the value(s) on top of the operand stack in accordance with the given opcode. |
default CodeBuilder |
ifThenElse |
Adds an "if-then-else" block that is conditional on the boolean value on top of the operand stack. |
default CodeBuilder |
iinc |
Generates an instruction to increment an int local variable by a constant. |
default CodeBuilder |
iload |
Generates an instruction to load an int from a local variable. |
default CodeBuilder |
imul() |
Generates an instruction to multiply ints. |
default CodeBuilder |
ineg() |
Generates an instruction to negate an int. |
default CodeBuilder |
instanceOf |
Generates an instruction to determine if an object is of the given type, producing a boolean result on the operand stack. |
default CodeBuilder |
instanceOf |
Generates an instruction to determine if an object is of the given type, producing a boolean result on the operand stack. |
default CodeBuilder |
invoke |
Generates an instruction to invoke a method. |
default CodeBuilder |
invoke |
Generates an instruction to invoke a method. |
default CodeBuilder |
invokedynamic |
Generates an instruction to invoke a dynamically-computed call site. |
default CodeBuilder |
invokedynamic |
Generates an instruction to invoke a dynamically-computed call site. |
default CodeBuilder |
invokeinterface |
Generates an instruction to invoke an interface method. |
default CodeBuilder |
invokeinterface |
Generates an instruction to invoke an interface method. |
default CodeBuilder |
invokespecial |
Generates an instruction to invoke an instance method in an interface; direct invocation of methods of the current class and its supertypes. |
default CodeBuilder |
invokespecial |
Generates an instruction to invoke an instance method in a class; direct invocation of instance initialization methods and methods of the current class and its supertypes. |
default CodeBuilder |
invokespecial |
Generates an instruction to invoke an instance method in a class; direct invocation of instance initialization methods and methods of the current class and its supertypes. |
default CodeBuilder |
invokespecial |
Generates an instruction to invoke an instance method; direct invocation of instance initialization methods and methods of the current class and its supertypes. |
default CodeBuilder |
invokestatic |
Generates an instruction to invoke a class (static) method of an interface. |
default CodeBuilder |
invokestatic |
Generates an instruction to invoke a class (static) method of a class. |
default CodeBuilder |
invokestatic |
Generates an instruction to invoke a class (static) method of a class. |
default CodeBuilder |
invokestatic |
Generates an instruction to invoke a class (static) method. |
default CodeBuilder |
invokevirtual |
Generates an instruction to invoke an instance method; dispatch based on class. |
default CodeBuilder |
invokevirtual |
Generates an instruction to invoke an instance method; dispatch based on class. |
default CodeBuilder |
ior() |
|
default CodeBuilder |
irem() |
Generates an instruction to calculate ints remainder. |
default CodeBuilder |
ireturn() |
Generates an instruction to return an int from this method. |
default CodeBuilder |
ishl() |
Generates an instruction to shift an int left. |
default CodeBuilder |
ishr() |
Generates an instruction to shift an int right. |
default CodeBuilder |
istore |
Generates an instruction to store an int into a local variable. |
default CodeBuilder |
isub() |
Generates an instruction to subtract ints. |
default CodeBuilder |
iushr() |
Generates an instruction to logical shift an int right. |
default CodeBuilder |
ixor() |
Generates an instruction to calculate bitwise XOR of ints. |
default CodeBuilder |
l2d() |
|
default CodeBuilder |
l2f() |
|
default CodeBuilder |
l2i() |
|
default CodeBuilder |
labelBinding |
Binds a label to the current position. |
default CodeBuilder |
ladd() |
Generates an instruction to add two longs. |
default CodeBuilder |
laload() |
Generates an instruction to load from a long array. |
default CodeBuilder |
land() |
Generates an instruction to calculate bitwise AND of longs. |
default CodeBuilder |
lastore() |
Generates an instruction to store into a long array. |
default CodeBuilder |
lcmp() |
Generates an instruction to compare longs. |
default CodeBuilder |
lconst_0() |
Generates an instruction pushing long constant 0 onto the operand stack. |
default CodeBuilder |
lconst_1() |
Generates an instruction pushing long constant 1 onto the operand stack. |
default CodeBuilder |
ldc |
Generates an instruction pushing an item from the run-time constant pool onto the operand stack. |
default CodeBuilder |
ldc |
Generates an instruction pushing an item from the run-time constant pool onto the operand stack. |
default CodeBuilder |
ldiv() |
Generates an instruction to divide longs. |
default CodeBuilder |
lineNumber |
Declares a source line number beginning at the current position. |
default CodeBuilder |
lload |
Generates an instruction to load a long from a local variable. |
default CodeBuilder |
lmul() |
Generates an instruction to multiply longs. |
default CodeBuilder |
lneg() |
Generates an instruction to negate a long. |
default CodeBuilder |
loadConstant |
Generates an instruction pushing a constant double value onto the operand stack. |
default CodeBuilder |
loadConstant |
Generates an instruction pushing a constant float value onto the operand stack. |
default CodeBuilder |
loadConstant |
Generates an instruction pushing a constant int value onto the operand stack. |
default CodeBuilder |
loadConstant |
Generates an instruction pushing a constant long value onto the operand stack. |
default CodeBuilder |
loadConstant |
Generates an instruction pushing a constant onto the operand stack. |
default CodeBuilder |
loadLocal |
Generates an instruction to load a value from a local variable. |
default CodeBuilder |
localVariable |
Declares a local variable entry. |
default CodeBuilder |
localVariable |
Declares a local variable entry. |
default CodeBuilder |
localVariableType |
Declares a local variable type entry. |
default CodeBuilder |
localVariableType |
Declares a local variable type entry. |
default CodeBuilder |
lookupswitch |
Generates an instruction to access a jump table by key match and jump. |
default CodeBuilder |
lor() |
Generates an instruction to calculate bitwise OR of longs. |
default CodeBuilder |
lrem() |
Generates an instruction to calculate longs remainder. |
default CodeBuilder |
lreturn() |
Generates an instruction to return a long from this method. |
default CodeBuilder |
lshl() |
Generates an instruction to shift a long left. |
default CodeBuilder |
lshr() |
Generates an instruction to shift a long right. |
default CodeBuilder |
lstore |
Generates an instruction to store a long into a local variable. |
default CodeBuilder |
lsub() |
Generates an instruction to subtract longs. |
default CodeBuilder |
lushr() |
Generates an instruction to logical shift a long right. |
default CodeBuilder |
lxor() |
Generates an instruction to calculate bitwise XOR of longs. |
default CodeBuilder |
monitorenter() |
Generates an instruction to enter monitor for an object. |
default CodeBuilder |
monitorexit() |
Generates an instruction to exit monitor for an object. |
default CodeBuilder |
multianewarray |
Generates an instruction to create a new multidimensional array. |
default CodeBuilder |
multianewarray |
Generates an instruction to create a new multidimensional array. |
default CodeBuilder |
new_ |
Generates an instruction to create a new object. |
default CodeBuilder |
new_ |
Generates an instruction to create a new object. |
default CodeBuilder |
newarray |
Generates an instruction to create a new array of a primitive type. |
default Label |
newBoundLabel() |
Creates a new label bound at the current position. |
Label |
newLabel() |
Returns a fresh unbound label. |
default CodeBuilder |
nop() |
Generates a do-nothing instruction. |
int |
parameterSlot |
Returns the local variable slot associated with the specified parameter. |
default CodeBuilder |
pop() |
Generates an instruction to pop the top operand stack value. |
default CodeBuilder |
pop2() |
Generates an instruction to pop the top one or two operand stack values. |
default CodeBuilder |
putfield |
Generates an instruction to set field in an object. |
default CodeBuilder |
putfield |
Generates an instruction to set field in an object. |
default CodeBuilder |
putstatic |
Generates an instruction to set static field in a class. |
default CodeBuilder |
putstatic |
Generates an instruction to set static field in a class. |
int |
receiverSlot() |
Returns the local variable slot associated with the receiver. |
default CodeBuilder |
return_() |
Generates an instruction to return void from this method. |
default CodeBuilder |
return_ |
Generates a return instruction. |
default CodeBuilder |
saload() |
Generates an instruction to load from a short array. |
default CodeBuilder |
sastore() |
Generates an instruction to store into a short array. |
default CodeBuilder |
sipush |
|
Label |
startLabel() |
Returns the label associated with the beginning of the current block. |
default CodeBuilder |
storeLocal |
Generates an instruction to store a value to a local variable. |
default CodeBuilder |
swap() |
Generates an instruction to swap the top two operand stack values. |
default CodeBuilder |
tableswitch |
Generates an instruction to access a jump table by index and jump. |
default CodeBuilder |
tableswitch |
Generates an instruction to access a jump table by index and jump. |
default CodeBuilder |
transforming |
Apply a transform to the code built by a handler, directing results to this builder. |
default CodeBuilder |
trying |
Adds a "try-catch" block comprising one try block and zero or more catch blocks. |
accept, constantPool, transform, with
Label newLabel()
labelBinding(Label).Label startLabel()
block(Consumer) or ifThenElse(Consumer, Consumer), the current block will be the entire method body.Label endLabel()
block(Consumer) or ifThenElse(Consumer, Consumer), the current block will be the entire method body.int receiverSlot()
IllegalStateException - if this is a static methodint parameterSlot(int paramNo)
long and double values require two slots.paramNo - the index of the parameterIndexOutOfBoundsException - if the parameter index is out of boundsint allocateLocal(TypeKind typeKind)
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 CodeBuilder.BlockCodeBuilder, at the end of the block, locals are reset to their value at the beginning of the block.
typeKind - the type of the local variabledefault CodeBuilder transforming(CodeTransform transform, Consumer<CodeBuilder> handler)
ClassFileBuilder.transform(CompoundElement, ClassFileTransform), but this does not require the code elements to be viewed as a CodeModel first.transform - the transform to apply to the code built by the handlerhandler - the handler that receives a CodeBuilder to build the codedefault CodeBuilder block(Consumer<CodeBuilder.BlockCodeBuilder> handler)
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, or the cursor position immediately after this call in this builder.
handler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the lexical blockdefault CodeBuilder ifThen(Consumer<CodeBuilder.BlockCodeBuilder> thenHandler)
boolean value on top of the operand stack. Control flow enters the "then" block if the value represents true. The CodeBuilder.BlockCodeBuilder.breakLabel() for the "then" block corresponds to the cursor position immediately after this call in this builder.
thenHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the if
default CodeBuilder ifThen(Opcode opcode, Consumer<CodeBuilder.BlockCodeBuilder> thenHandler)
opcode succeeds. The CodeBuilder.BlockCodeBuilder.breakLabel() for the "then" block corresponds to the cursor position immediately after this call in this builder.
opcode - the operation code for a branch instruction that accepts one or two operands on the stackthenHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the if
IllegalArgumentException - if the operation code is not for a branch instruction that accepts one or two operandsdefault CodeBuilder ifThenElse(Consumer<CodeBuilder.BlockCodeBuilder> thenHandler, Consumer<CodeBuilder.BlockCodeBuilder> elseHandler)
boolean value on top of the operand stack. Control flow enters the "then" block if the value represents true, and enters the "else" block otherwise. The CodeBuilder.BlockCodeBuilder.breakLabel() for each block corresponds to the cursor position immediately after this call in this builder.
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
default CodeBuilder ifThenElse(Opcode opcode, Consumer<CodeBuilder.BlockCodeBuilder> thenHandler, Consumer<CodeBuilder.BlockCodeBuilder> elseHandler)
opcode succeeds, and enters the "else" block otherwise. The CodeBuilder.BlockCodeBuilder.breakLabel() for each block corresponds to the cursor position immediately after this call in this builder.
opcode - the operation code for a branch instruction that accepts one or two operands on the stackthenHandler - 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
IllegalArgumentException - if the operation code is not for a branch instruction that accepts one or two operandsdefault CodeBuilder trying(Consumer<CodeBuilder.BlockCodeBuilder> tryHandler, Consumer<CodeBuilder.CatchBuilder> catchesHandler)
The CodeBuilder.BlockCodeBuilder.breakLabel() for the try block and all catch blocks in the catchesHandler correspond to the cursor position immediately after this call in this builder.
tryHandler - handler that receives a CodeBuilder.BlockCodeBuilder to generate the body of the try block.catchesHandler - a handler that receives a CodeBuilder.CatchBuilder to generate bodies of catch blocksIllegalArgumentException - if the try block is emptydefault CodeBuilder loadLocal(TypeKind tk, int slot)
tk - the load typeslot - the local variable slotIllegalArgumentException - if tk is void or slot is out of rangedefault CodeBuilder storeLocal(TypeKind tk, int slot)
tk - the store typeslot - the local variable slotIllegalArgumentException - if tk is void or slot is out of rangedefault CodeBuilder branch(Opcode op, Label target)
This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set, the opcode has size 3, and target cannot be encoded as a BCI offset in [-32768, 32767].
op - the branch opcodetarget - the branch targetIllegalArgumentException - if op is not of Opcode.Kind.BRANCH
default CodeBuilder return_(TypeKind tk)
tk - the return typedefault CodeBuilder fieldAccess(Opcode opcode, FieldRefEntry ref)
opcode - the field access opcoderef - the field referenceIllegalArgumentException - if opcode is not of Opcode.Kind.FIELD_ACCESS
default CodeBuilder fieldAccess(Opcode opcode, ClassDesc owner, String name, ClassDesc type)
opcode - the field access opcodeowner - the classname - the field nametype - the field typeIllegalArgumentException - if opcode is not of Opcode.Kind.FIELD_ACCESS, or owner is primitivedefault CodeBuilder invoke(Opcode opcode, MemberRefEntry ref)
opcode - the invoke opcoderef - the interface method or method referenceIllegalArgumentException - if opcode is not of Opcode.Kind.INVOKE
default CodeBuilder invoke(Opcode opcode, ClassDesc owner, String name, MethodTypeDesc desc, boolean isInterface)
opcode - the invoke opcodeowner - the classname - the method namedesc - the method typeisInterface - whether the owner class is an interfaceIllegalArgumentException - if opcode is not of Opcode.Kind.INVOKE, or owner is primitivedefault CodeBuilder arrayLoad(TypeKind tk)
tk - the array element typeIllegalArgumentException - if tk is void
default CodeBuilder arrayStore(TypeKind tk)
tk - the array element typeIllegalArgumentException - if tk is void
default CodeBuilder conversion(TypeKind fromType, TypeKind toType)
fromType to toType.fromType - the source typetoType - the target typeIllegalArgumentException - for conversions of void or reference
default CodeBuilder loadConstant(ConstantDesc value)
value - the constant value, may be null
default CodeBuilder loadConstant(int value)
int value onto the operand stack. This is equivalent to loadConstant(Integer.valueOf(value)).value - the int valuedefault CodeBuilder loadConstant(long value)
long value onto the operand stack. This is equivalent to loadConstant(Long.valueOf(value)).value - the long valuedefault CodeBuilder loadConstant(float value)
float value onto the operand stack. This is equivalent to loadConstant(Float.valueOf(value)). All NaN values of the float may or may not be collapsed into a single "canonical" NaN value.
value - the float valuedefault CodeBuilder loadConstant(double value)
double value onto the operand stack. This is equivalent to loadConstant(Double.valueOf(value)). All NaN values of the double may or may not be collapsed into a single "canonical" NaN value.
value - the double valuedefault CodeBuilder nop()
default Label newBoundLabel()
default CodeBuilder labelBinding(Label label)
CodeModel.label - the labeldefault CodeBuilder lineNumber(int line)
This call may be ignored according to ClassFile.LineNumbersOption.
line - the line numberdefault CodeBuilder exceptionCatch(Label start, Label end, Label handler, ClassEntry catchType)
This call may be ignored if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
start - the try block startend - the try block endhandler - the exception handler startcatchType - the catch type, may be null to catch all exceptions and errorsdefault CodeBuilder exceptionCatch(Label start, Label end, Label handler, Optional<ClassEntry> catchType)
This call may be ignored if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
start - the try block startend - the try block endhandler - the exception handler startcatchType - the optional catch type, empty to catch all exceptions and errorsdefault CodeBuilder exceptionCatch(Label start, Label end, Label handler, ClassDesc catchType)
This call may be ignored if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
start - the try block startend - the try block endhandler - the exception handler startcatchType - the catch typeIllegalArgumentException - if catchType is primitivedefault CodeBuilder exceptionCatchAll(Label start, Label end, Label handler)
This call may be ignored if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
start - the try block startend - the try block endhandler - the exception handler startdefault CodeBuilder characterRange(Label startScope, Label endScope, int characterRangeStart, int characterRangeEnd, int flags)
This call may be ignored if ClassFile.DebugElementsOption.DROP_DEBUG is set, or if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
startScope - the start scope of the character rangeendScope - the end scope of the character rangecharacterRangeStart - 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 rangedefault CodeBuilder localVariable(int slot, Utf8Entry nameEntry, Utf8Entry descriptorEntry, Label startScope, Label endScope)
This call may be ignored if ClassFile.DebugElementsOption.DROP_DEBUG is set, or if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
slot - the local variable slotnameEntry - the variable namedescriptorEntry - the variable descriptorstartScope - the start scope of the variableendScope - the end scope of the variableIllegalArgumentException - if slot is out of rangedefault CodeBuilder localVariable(int slot, String name, ClassDesc descriptor, Label startScope, Label endScope)
This call may be ignored if ClassFile.DebugElementsOption.DROP_DEBUG is set, or if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
slot - the local variable slotname - the variable namedescriptor - the variable descriptorstartScope - the start scope of the variableendScope - the end scope of the variableIllegalArgumentException - if slot is out of rangedefault CodeBuilder localVariableType(int slot, Utf8Entry nameEntry, Utf8Entry signatureEntry, Label startScope, Label endScope)
This call may be ignored if ClassFile.DebugElementsOption.DROP_DEBUG is set, or if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
slot - the local variable slotnameEntry - the variable namesignatureEntry - the variable signaturestartScope - the start scope of the variableendScope - the end scope of the variableIllegalArgumentException - if slot is out of rangedefault CodeBuilder localVariableType(int slot, String name, Signature signature, Label startScope, Label endScope)
This call may be ignored if ClassFile.DebugElementsOption.DROP_DEBUG is set, or if any of the argument labels is not bound and ClassFile.DeadLabelsOption.DROP_DEAD_LABELS is set.
slot - the local variable slotname - the variable namesignature - the variable signaturestartScope - the start scope of the variableendScope - the end scope of the variableIllegalArgumentException - if slot is out of rangedefault CodeBuilder aconst_null()
reference onto the operand stack.default CodeBuilder aaload()
reference array.default CodeBuilder aastore()
reference array.default CodeBuilder aload(int slot)
reference from a local variable. This may also generate aload_<N> and wide aload instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder anewarray(ClassEntry classEntry)
reference.classEntry - the component typedefault CodeBuilder anewarray(ClassDesc className)
reference.className - the component typeIllegalArgumentException - if className represents a primitive typedefault CodeBuilder areturn()
reference from this method.default CodeBuilder arraylength()
default CodeBuilder astore(int slot)
reference into a local variable. Such an instruction can also store a returnAddress. This may also generate astore_<N> and wide astore instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder athrow()
default CodeBuilder bipush(int b)
b - the int in the range of byteIllegalArgumentException - if b is out of range of bytedefault CodeBuilder caload()
char array.default CodeBuilder castore()
char array.default CodeBuilder checkcast(ClassEntry type)
ClassCastException if the check fails.type - the object typedefault CodeBuilder checkcast(ClassDesc type)
ClassCastException if the check fails.type - the object typeIllegalArgumentException - if type represents a primitive typedefault CodeBuilder dadd()
doubles.default CodeBuilder daload()
double array.default CodeBuilder dastore()
double array.default CodeBuilder dconst_0()
double constant 0 onto the operand stack.default CodeBuilder dconst_1()
double constant 1 onto the operand stack.default CodeBuilder ddiv()
doubles.default CodeBuilder dload(int slot)
double from a local variable. This may also generate dload_<N> and wide dload instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder dmul()
doubles.default CodeBuilder dneg()
double.default CodeBuilder drem()
double remainder.default CodeBuilder dreturn()
double from this method.default CodeBuilder dstore(int slot)
double into a local variable. This may also generate dstore_<N> and wide dstore instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder dsub()
doubles.default CodeBuilder dup()
default CodeBuilder dup2()
default CodeBuilder dup2_x1()
default CodeBuilder dup2_x2()
default CodeBuilder dup_x1()
default CodeBuilder dup_x2()
default CodeBuilder fadd()
floats.default CodeBuilder faload()
float array.default CodeBuilder fastore()
float array.default CodeBuilder fconst_0()
float constant 0 onto the operand stack.default CodeBuilder fconst_1()
float constant 1 onto the operand stack.default CodeBuilder fconst_2()
float constant 2 onto the operand stack.default CodeBuilder fdiv()
floats.default CodeBuilder fload(int slot)
float from a local variable. This may also generate fload_<N> and wide fload instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder fmul()
floats.default CodeBuilder fneg()
float.default CodeBuilder frem()
floats remainder.default CodeBuilder freturn()
float from this method.default CodeBuilder fstore(int slot)
float into a local variable. This may also generate fstore_<N> and wide fstore instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder fsub()
floats.default CodeBuilder getfield(FieldRefEntry ref)
ref - the field referencedefault CodeBuilder getfield(ClassDesc owner, String name, ClassDesc type)
owner - the owner classname - the field nametype - the field typeIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder getstatic(FieldRefEntry ref)
ref - the field referencedefault CodeBuilder getstatic(ClassDesc owner, String name, ClassDesc type)
owner - the owner classname - the field nametype - the field typeIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder goto_(Label target)
This may also generate goto_w instructions if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set.
goto, which coincides with a reserved keyword of the Java programming language, thus this method is named with an extra _ suffix instead.target - the branch targetdefault CodeBuilder goto_w(Label target)
target - the branch targetdefault CodeBuilder iadd()
ints.default CodeBuilder iaload()
int array.default CodeBuilder iastore()
int array.default CodeBuilder iconst_0()
int constant 0 onto the operand stack.default CodeBuilder iconst_1()
int constant 1 onto the operand stack.default CodeBuilder iconst_2()
int constant 2 onto the operand stack.default CodeBuilder iconst_3()
int constant 3 onto the operand stack.default CodeBuilder iconst_4()
int constant 4 onto the operand stack.default CodeBuilder iconst_5()
int constant 5 onto the operand stack.default CodeBuilder iconst_m1()
int constant -1 onto the operand stack.default CodeBuilder if_acmpeq(Label target)
reference comparison operand1 == operand2 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder if_acmpne(Label target)
reference comparison operand1 != operand2 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder if_icmpeq(Label target)
int comparison operand1 == operand2 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder if_icmpge(Label target)
int comparison operand1 >= operand2 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder if_icmpgt(Label target)
int comparison operand1 > operand2 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder if_icmple(Label target)
int comparison operand1 <= operand2 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder if_icmplt(Label target)
int comparison operand1 < operand2 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder if_icmpne(Label target)
int comparison operand1 != operand2 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder ifnonnull(Label target)
reference is not null. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder ifnull(Label target)
reference is null. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder ifeq(Label target)
int comparison with zero == 0 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder ifge(Label target)
int comparison with zero >= 0 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder ifgt(Label target)
int comparison with zero > 0 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder ifle(Label target)
int comparison with zero <= 0 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder iflt(Label target)
int comparison with zero < 0 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder ifne(Label target)
int comparison with zero != 0 succeeds. This may generate multiple instructions to accomplish the same effect if ClassFile.ShortJumpsOption.FIX_SHORT_JUMPS is set and
target cannot be encoded as a BCI offset in [-32768, 32767].
target - the branch targetdefault CodeBuilder iinc(int slot, int val)
int local variable by a constant. This may also generate wide iinc instructions if slot exceeds 255 or val exceeds the range of byte.
slot - the local variable slotval - the increment valueIllegalArgumentException - if slot or val is out of rangedefault CodeBuilder iload(int slot)
int from a local variable. This may also generate iload_<N> and wide iload instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder imul()
ints.default CodeBuilder ineg()
int.default CodeBuilder instanceOf(ClassEntry target)
boolean result on the operand stack.instanceof, which coincides with a reserved keyword of the Java programming language, thus this method is named with camel case instead.target - the target typedefault CodeBuilder instanceOf(ClassDesc target)
boolean result on the operand stack.instanceof, which coincides with a reserved keyword of the Java programming language, thus this method is named with camel case instead.target - the target typeIllegalArgumentException - if target represents a primitive typedefault CodeBuilder invokedynamic(InvokeDynamicEntry ref)
ref - the dynamic call sitedefault CodeBuilder invokedynamic(DynamicCallSiteDesc ref)
ref - the dynamic call sitedefault CodeBuilder invokeinterface(InterfaceMethodRefEntry ref)
ref - the interface method referencedefault CodeBuilder invokeinterface(ClassDesc owner, String name, MethodTypeDesc type)
owner - the owner interfacename - the method nametype - the method typeIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder invokespecial(InterfaceMethodRefEntry ref)
ref - the interface method referencedefault CodeBuilder invokespecial(MethodRefEntry ref)
ref - the method referencedefault CodeBuilder invokespecial(ClassDesc owner, String name, MethodTypeDesc type)
owner - the owner class, must not be an interfacename - the method nametype - the method typeIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder invokespecial(ClassDesc owner, String name, MethodTypeDesc type, boolean isInterface)
owner - the owner class or interfacename - the method nametype - the method typeisInterface - whether the owner is an interfaceIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder invokestatic(InterfaceMethodRefEntry ref)
ref - the interface method referencedefault CodeBuilder invokestatic(MethodRefEntry ref)
ref - the method referencedefault CodeBuilder invokestatic(ClassDesc owner, String name, MethodTypeDesc type)
owner - the owner class, must not be an interfacename - the method nametype - the method typeIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder invokestatic(ClassDesc owner, String name, MethodTypeDesc type, boolean isInterface)
owner - the owner class or interfacename - the method nametype - the method typeisInterface - whether the owner is an interfaceIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder invokevirtual(MethodRefEntry ref)
ref - the method referencedefault CodeBuilder invokevirtual(ClassDesc owner, String name, MethodTypeDesc type)
owner - the owner class, must not be an interfacename - the method nametype - the method typeIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder irem()
ints remainder.default CodeBuilder ireturn()
int from this method.default CodeBuilder ishl()
int left.default CodeBuilder istore(int slot)
int into a local variable. This may also generate istore_<N> and wide istore instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder isub()
ints.default CodeBuilder lookupswitch(Label defaultTarget, List<SwitchCase> cases)
defaultTarget - the default jump targetcases - the switch casesdefault CodeBuilder ladd()
longs.default CodeBuilder laload()
long array.default CodeBuilder land()
longs.default CodeBuilder lastore()
long array.default CodeBuilder lcmp()
longs.default CodeBuilder lconst_0()
long constant 0 onto the operand stack.default CodeBuilder lconst_1()
long constant 1 onto the operand stack.default CodeBuilder ldc(ConstantDesc value)
loadConstant generates more optimal instructions and should be used for general constants if an ldc instruction is not strictly required.value - the constant valuedefault CodeBuilder ldiv()
longs.default CodeBuilder lload(int slot)
long from a local variable. This may also generate lload_<N> and wide lload instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder lmul()
longs.default CodeBuilder lneg()
long.default CodeBuilder lor()
longs.default CodeBuilder lrem()
longs remainder.default CodeBuilder lreturn()
long from this method.default CodeBuilder lshl()
long left.default CodeBuilder lstore(int slot)
long into a local variable. This may also generate lstore_<N> and wide lstore instructions.
slot - the local variable slotIllegalArgumentException - if slot is out of rangedefault CodeBuilder lsub()
longs.default CodeBuilder lxor()
longs.default CodeBuilder monitorenter()
default CodeBuilder monitorexit()
default CodeBuilder multianewarray(ClassEntry array, int dims)
array - the array typedims - the number of dimensionsIllegalArgumentException - if dims is out of rangedefault CodeBuilder multianewarray(ClassDesc array, int dims)
array - the array typedims - the number of dimensionsIllegalArgumentException - if array represents a primitive type or if dims is out of rangedefault CodeBuilder new_(ClassEntry clazz)
new, which coincides with a reserved keyword of the Java programming language, thus this method is named with an extra _ suffix instead.clazz - the new class typedefault CodeBuilder new_(ClassDesc clazz)
new, which coincides with a reserved keyword of the Java programming language, thus this method is named with an extra _ suffix instead.clazz - the new class typeIllegalArgumentException - if clazz represents a primitive typedefault CodeBuilder newarray(TypeKind typeKind)
typeKind - the primitive array typeIllegalArgumentException - when the typeKind is not a legal primitive array component typedefault CodeBuilder pop()
default CodeBuilder pop2()
default CodeBuilder putfield(FieldRefEntry ref)
ref - the field referencedefault CodeBuilder putfield(ClassDesc owner, String name, ClassDesc type)
owner - the owner classname - the field nametype - the field typeIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder putstatic(FieldRefEntry ref)
ref - the field referencedefault CodeBuilder putstatic(ClassDesc owner, String name, ClassDesc type)
owner - the owner class or interfacename - the field nametype - the field typeIllegalArgumentException - if owner represents a primitive typedefault CodeBuilder return_()
void from this method.return, which coincides with a reserved keyword of the Java programming language, thus this method is named with an extra _ suffix instead.default CodeBuilder saload()
short array.default CodeBuilder sastore()
short array.default CodeBuilder sipush(int s)
int in the range of short, [-32768, 32767], onto the operand stack.s - the int in the range of shortIllegalArgumentException - if s is out of range of shortdefault CodeBuilder swap()
default CodeBuilder tableswitch(int low, int high, Label defaultTarget, List<SwitchCase> cases)
low - the minimum key, inclusivehigh - the maximum key, inclusivedefaultTarget - the default jump targetcases - the switch casesdefault CodeBuilder tableswitch(Label defaultTarget, List<SwitchCase> cases)
cases.defaultTarget - the default jump targetcases - the switch cases
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/classfile/CodeBuilder.html