APIdia Launch Partner
Top Description Methods
java.lang.classfile

public sealed Interface MethodBuilder

extends ClassFileBuilder<MethodElement, MethodBuilder>
permits ChainedMethodBuilder, TerminalMethodBuilder
Known Direct Subinterfaces
jdk.internal.classfile.impl.TerminalMethodBuilder
Known Direct Implementers
jdk.internal.classfile.impl.ChainedMethodBuilder
Imports
java.lang.constant.MethodTypeDesc, java.lang.reflect.AccessFlag, java.util.function.Consumer, jdk.internal.classfile.impl.AccessFlagsImpl, .ChainedMethodBuilder, .TerminalMethodBuilder

A builder for methods. The main way to obtain a method builder is via ClassBuilder#withMethod(String, MethodTypeDesc, int, Consumer). ClassBuilder#withMethodBody(String, MethodTypeDesc, int, Consumer) is useful if no attribute on the method except Code needs to be configured, skipping the method handler.

Refer to ClassFileBuilder for general guidance and caution around the use of builders for structures in the class file format.

Since
24
Java Virtual Machine Specification
4.6 Methods
See Also
MethodModel, MethodTransform

Method Summary

Modifier and TypeMethod and Description
public MethodBuilder

Returns:

this builder
transformCode
(CodeModel
the method body to be transformed
code
,
CodeTransform
the transform to apply to the method body
transform
)

Build the method body for this method by transforming the body of another method.

public MethodBuilder

Returns:

this builder
withCode
(Consumer<? super CodeBuilder>
a handler receiving a CodeBuilder
code
)

Build the method body for this method.

public default MethodBuilder

Returns:

this builder
withFlags
(int
the access flags, as a bit mask
flags
)

Sets the method access flags.

public default MethodBuilder

Returns:

this builder
withFlags
(AccessFlag...
the access flags, as a bit mask
flags
)

Sets the method access flags.

Inherited from java.lang.classfile.ClassFileBuilder:
acceptconstantPooltransformwith

Method Detail

transformCodeback to summary
public MethodBuilder transformCode(CodeModel code, CodeTransform transform)

Build the method body for this method by transforming the body of another method.

This method behaves as if:

withCode(cob -> cob.transform(code, transform));
withCode(cob -> cob.transform(code, transform));
Parameters
code:CodeModel

the method body to be transformed

transform:CodeTransform

the transform to apply to the method body

Returns:MethodBuilder

this builder

See Also
CodeTransform
withCodeback to summary
public MethodBuilder withCode(Consumer<? super CodeBuilder> code)

Build the method body for this method.

Parameters
code:Consumer<? super CodeBuilder>

a handler receiving a CodeBuilder

Returns:MethodBuilder

this builder

See Also
CodeModel
withFlagsback to summary
public default MethodBuilder withFlags(int flags)

Sets the method access flags. The AccessFlag#STATIC flag cannot be modified after the builder is created.

Parameters
flags:int

the access flags, as a bit mask

Returns:MethodBuilder

this builder

Exceptions
IllegalArgumentException:
if flags is not u2, or the ACC_STATIC flag is modified
See Also
AccessFlags, AccessFlag.Location#METHOD
withFlagsback to summary
public default MethodBuilder withFlags(AccessFlag... flags)

Sets the method access flags. The AccessFlag#STATIC flag cannot be modified after the builder is created.

Parameters
flags:AccessFlag[]

the access flags, as a bit mask

Returns:MethodBuilder

this builder

Exceptions
IllegalArgumentException:
if the ACC_STATIC flag is modified, or if any flag cannot be applied to the AccessFlag.Location#METHOD location
See Also
AccessFlags, AccessFlag.Location#METHOD