Code attributes (method bodies). MethodBuilder#withCode is the basic way to obtain a code builder; ClassBuilder#withMethodBody is a shortcut. There are also derived code
builders from block, which handles code blocks and transforming, 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: DeadCodeOption and ShortJumpsOption
affect the resulting bytecode, and DeadLabelsOption, DebugElementsOption, LineNumbersOption, StackMapsOption, and
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.
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 with.
MethodBuilder#withCode, CodeModel, CodeTransform
| Modifier and Type | Class and Description |
|---|---|
| public static interface | CodeBuilder.
A builder for blocks of code. |
| public static interface | CodeBuilder.
A builder to add catch blocks. |
| Modifier and Type | Method and Description |
|---|---|
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderGenerates an instruction pushing the null object |
| public int | Returns: the local variable slot of a fresh local variablethe type of the local variable typeKind)Returns the local variable slot of a fresh local variable. |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe component type classEntry)Generates an instruction to create a new array of |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe array element type tk)Generates an instruction to store into an array. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderhandler that receives a handlerBlockCodeBuilder to
generate the body of the lexical blockAdds a lexical block to the method being built. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe start scope of the character range startScope, Label the end scope of the character range endScope, int the encoded start of the character range region (inclusive) characterRangeStart, int the encoded end of the character range region (exclusive) characterRangeEnd, int the flags word, indicating the kind of range flags)Declares a character range entry. |
| public default CodeBuilder | Returns: this builderthe object type type)Generates an instruction to check whether an object is of the given type,
throwing a |
| public default CodeBuilder | Returns: this builderthe object type type)Generates an instruction to check whether an object is of the given type,
throwing a |
| public default CodeBuilder | Returns: this builderthe source type fromType, TypeKind the target type toType)Generates instruction(s) to convert |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderGenerates an instruction to duplicate the top one or two operand stack value. |
| public default CodeBuilder | Returns: this builderGenerates an instruction to duplicate the top one or two operand stack values and insert two or three values down. |
| public default CodeBuilder | Returns: this builderGenerates an instruction to duplicate the top one or two operand stack values and insert two, three, or four values down. |
| public default CodeBuilder | Returns: this builderGenerates an instruction to duplicate the top operand stack value and insert two values down. |
| public default CodeBuilder | Returns: this builderGenerates an instruction to duplicate the top operand stack value and insert two or three values down. |
| public Label | Returns: the label associated with the end of the current blockReturns the label associated with the end of the current block. |
| public default CodeBuilder | Returns: this builderthe try block start start, Label the try block end end, Label the exception handler start handler, ClassEntry the catch type, may be catchType)null to catch all exceptions and errorsDeclares an exception table entry. |
| public default CodeBuilder | Returns: this builderthe try block start start, Label the try block end end, Label the exception handler start handler, Optional<ClassEntry> the optional catch type, empty to catch all exceptions and errors catchType)Declares an exception table entry. |
| public default CodeBuilder | Returns: this builderthe try block start start, Label the try block end end, Label the exception handler start handler, ClassDesc the catch type catchType)Declares an exception table entry. |
| public default CodeBuilder | Returns: this builderthe try block start start, Label the try block end end, Label the exception handler start handler)Declares an exception table entry catching all exceptions and errors. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe field access opcode opcode, FieldRefEntry the field reference ref)Generates an instruction to access a field. |
| public default CodeBuilder | Returns: this builderthe field access opcode opcode, ClassDesc the class owner, String the field name name, ClassDesc the field type type)Generates an instruction to access a field. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe field reference ref)Generates an instruction to fetch field from an object. |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe field reference ref)Generates an instruction to get static field from a class or interface. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderhandler that receives a BlockCodeBuilder
to generate the body of the thenHandlerifAdds an "if-then" block that is conditional on the |
| public default CodeBuilder | Returns: this builderthe operation code for a branch instruction that accepts
one or two operands on the stack opcode, Consumer<CodeBuilder.handler that receives a thenHandlerBlockCodeBuilder to
generate the body of the ifAdds an "if-then" block that is conditional on the value(s) on top of the operand stack in accordance with the given opcode. |
| public default CodeBuilder | Returns: this builderhandler that receives a thenHandler,BlockCodeBuilder to
generate the body of the ifhandler that receives a elseHandlerBlockCodeBuilder to
generate the body of the elseAdds an "if-then-else" block that is conditional on the |
| public default CodeBuilder | Returns: this builderthe operation code for a branch instruction that accepts
one or two operands on the stack opcode, Consumer<CodeBuilder.handler that receives a BlockCodeBuilder
to generate the body of the thenHandler,ifhandler that receives a BlockCodeBuilder
to generate the body of the elseHandlerelseAdds an "if-then-else" block that is conditional on the value(s) on top of the operand stack in accordance with the given opcode. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe target type target)Generates an instruction to determine if an object is of the given type,
producing a |
| public default CodeBuilder | Returns: this builderthe target type target)Generates an instruction to determine if an object is of the given type,
producing a |
| public default CodeBuilder | Returns: this builderthe invoke opcode opcode, MemberRefEntry the interface method or method reference ref)Generates an instruction to invoke a method. |
| public default CodeBuilder | Returns: this builderthe invoke opcode opcode, ClassDesc the class owner, String the method name name, MethodTypeDesc the method type desc, boolean whether the owner class is an interface isInterface)Generates an instruction to invoke a method. |
| public default CodeBuilder | Returns: this builderthe dynamic call site ref)Generates an instruction to invoke a dynamically-computed call site. |
| public default CodeBuilder | Returns: this builderthe dynamic call site ref)Generates an instruction to invoke a dynamically-computed call site. |
| public default CodeBuilder | Returns: this builderthe interface method reference ref)Generates an instruction to invoke an interface method. |
| public default CodeBuilder | Returns: this builderthe owner interface owner, String the method name name, MethodTypeDesc the method type type)Generates an instruction to invoke an interface method. |
| public default CodeBuilder | Returns: this builderthe interface method reference ref)Generates an instruction to invoke an instance method in an interface; direct invocation of methods of the current class and its supertypes. |
| public default CodeBuilder | Returns: this builderthe method reference ref)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. |
| public default CodeBuilder | Returns: this builderthe owner class, must not be an interface owner, String the method name name, MethodTypeDesc the method type type)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. |
| public default CodeBuilder | Returns: this builderthe owner class or interface owner, String the method name name, MethodTypeDesc the method type type, boolean whether the owner is an interface isInterface)Generates an instruction to invoke an instance method; direct invocation of instance initialization methods and methods of the current class and its supertypes. |
| public default CodeBuilder | Returns: this builderthe interface method reference ref)Generates an instruction to invoke a class (static) method of an interface. |
| public default CodeBuilder | Returns: this builderthe method reference ref)Generates an instruction to invoke a class (static) method of a class. |
| public default CodeBuilder | Returns: this builderthe owner class, must not be an interface owner, String the method name name, MethodTypeDesc the method type type)Generates an instruction to invoke a class (static) method of a class. |
| public default CodeBuilder | Returns: this builderthe owner class or interface owner, String the method name name, MethodTypeDesc the method type type, boolean whether the owner is an interface isInterface)Generates an instruction to invoke a class (static) method. |
| public default CodeBuilder | Returns: this builderthe method reference ref)Generates an instruction to invoke an instance method; dispatch based on class. |
| public default CodeBuilder | Returns: this builderthe owner class, must not be an interface owner, String the method name name, MethodTypeDesc the method type type)Generates an instruction to invoke an instance method; dispatch based on class. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe constant value value)Generates an instruction pushing an item from the run-time constant pool onto the operand stack. |
| public default CodeBuilder | Returns: this builderthe constant value entry)Generates an instruction pushing an item from the run-time constant pool onto the operand stack. |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe line number line)Declares a source line number beginning at the current position. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe constant value, may be value)nullGenerates an instruction pushing a constant onto the operand stack. |
| public default CodeBuilder | Returns: this builderthe int value value)Generates an instruction pushing a constant |
| public default CodeBuilder | Returns: this builderthe long value value)Generates an instruction pushing a constant |
| public default CodeBuilder | Returns: this builderthe float value value)Generates an instruction pushing a constant |
| public default CodeBuilder | Returns: this builderthe double value value)Generates an instruction pushing a constant |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe local variable slot slot, Utf8Entry the variable name nameEntry, Utf8Entry the variable descriptor descriptorEntry, Label the start scope of the variable startScope, Label the end scope of the variable endScope)Declares a local variable entry. |
| public default CodeBuilder | Returns: this builderthe local variable slot slot, String the variable name name, ClassDesc the variable descriptor descriptor, Label the start scope of the variable startScope, Label the end scope of the variable endScope)Declares a local variable entry. |
| public default CodeBuilder | Returns: this builderthe local variable slot slot, Utf8Entry the variable name nameEntry, Utf8Entry the variable signature signatureEntry, Label the start scope of the variable startScope, Label the end scope of the variable endScope)Declares a local variable type entry. |
| public default CodeBuilder | Returns: this builderthe local variable slot slot, String the variable name name, Signature the variable signature signature, Label the start scope of the variable startScope, Label the end scope of the variable endScope)Declares a local variable type entry. |
| public default CodeBuilder | Returns: this builderthe default jump target defaultTarget, List<SwitchCase> the switch cases cases)Generates an instruction to access a jump table by key match and jump. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe array type array, int the number of dimensions dims)Generates an instruction to create a new multidimensional array. |
| public default CodeBuilder | Returns: this builderthe array type array, int the number of dimensions dims)Generates an instruction to create a new multidimensional array. |
| public default CodeBuilder | Returns: this builderthe new class type clazz)Generates an instruction to create a new object. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default Label | |
| public Label | |
| public default CodeBuilder | |
| public int | Returns: the local variable slot associated with the specified parameterthe index of the parameter paramNo)Returns the local variable slot associated with the specified parameter. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe field reference ref)Generates an instruction to set field in an object. |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe field reference ref)Generates an instruction to set static field in a class. |
| public default CodeBuilder | |
| public int | Returns: the local variable slot associated with the receiverReturns the local variable slot associated with the receiver. |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public default CodeBuilder | |
| public Label | Returns: the label associated with the beginning of the current blockReturns the label associated with the beginning of the current block. |
| public default CodeBuilder | Returns: this builderthe store type tk, int the local variable slot slot)Generates an instruction to store a value to a local variable. |
| public default CodeBuilder | |
| public default CodeBuilder | Returns: this builderthe minimum key, inclusive low, int the maximum key, inclusive high, Label the default jump target defaultTarget, List<SwitchCase> the switch cases cases)Generates an instruction to access a jump table by index and jump. |
| public default CodeBuilder | Returns: this builderthe default jump target defaultTarget, List<SwitchCase> the switch cases cases)Generates an instruction to access a jump table by index and jump. |
| public default CodeBuilder | Returns: this builderthe transform to apply to the code built by the handler transform, Consumer<CodeBuilder> the handler that receives a handler)CodeBuilder to
build the codeApply a transform to the code built by a handler, directing results to this builder. |
| public default CodeBuilder | Returns: this builderhandler that receives a tryHandler,BlockCodeBuilder to
generate the body of the try block.a handler that receives a catchesHandlerCatchBuilder
to generate bodies of catch blocksAdds a "try-catch" block comprising one try block and zero or more catch blocks. |