MethodBuilder (Java SE 23 & JDK 23)
Description: declaration: module: java.base, package: java.lang.classfile, interface: MethodBuilder
Keywords: java.lang.classfile.MethodBuilder interface

# Interface MethodBuilder

All Superinterfaces:
  `ClassFileBuilderPREVIEW<MethodElementPREVIEW,MethodBuilderPREVIEW>`, `Consumer<MethodElementPREVIEW>`

public sealed interface MethodBuilder extends ClassFileBuilderPREVIEW<MethodElementPREVIEW,MethodBuilderPREVIEW>
`MethodBuilder` is a preview API of the Java platform.
Programs can only use `MethodBuilder` when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
A builder for methods. Builders are not created directly; they are passed to handlers by methods such as `ClassBuilder.withMethod(Utf8Entry, Utf8Entry, int, Consumer)`PREVIEW or to method transforms. The elements of a method can be specified abstractly (by passing a `MethodElement`PREVIEW to `ClassFileBuilder.with(ClassFileElement)`PREVIEW or concretely by calling the various `withXxx` methods.

Since:
  22
See Also:
  - `MethodTransform`PREVIEW

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Optional<MethodModelPREVIEW>`
  `original()`
  Returns the `MethodModel`PREVIEW representing the method being transformed, if this method builder represents the transformation of some `MethodModel`PREVIEW.
  `MethodBuilderPREVIEW`
  `transformCode(CodeModelPREVIEW code, CodeTransformPREVIEW transform)`
  Build the method body for this method by transforming the body of another method.
  `MethodBuilderPREVIEW`
  `withCode(Consumer<? super CodeBuilderPREVIEW> code)`
  Build the method body for this method.
  `default MethodBuilderPREVIEW`
  `withFlags(int flags)`
  Sets the method access flags.
  `default MethodBuilderPREVIEW`
  `withFlags(AccessFlag... flags)`
  Sets the method access flags.

  ### Methods declared in interface java.lang.classfile.ClassFileBuilderPREVIEW

  `accept, canWriteDirect, constantPool, transform, with`

  ### Methods declared in interface java.util.function.Consumer

  `andThen`

- ## Method Details

  - ### original

    Optional<MethodModelPREVIEW> original()
    Returns the `MethodModel`PREVIEW representing the method being transformed, if this method builder represents the transformation of some `MethodModel`PREVIEW.
    Returns:
      the `MethodModel`PREVIEW representing the method being transformed, if this method builder represents the transformation of some `MethodModel`PREVIEW

  - ### withFlags

    default MethodBuilderPREVIEW withFlags(int flags)
    Sets the method access flags.
    Parameters:
      `flags` - the access flags, as a bit mask
    Returns:
      this builder

  - ### withFlags

    default MethodBuilderPREVIEW withFlags(AccessFlag... flags)
    Sets the method access flags.
    Parameters:
      `flags` - the access flags, as a bit mask
    Returns:
      this builder

  - ### withCode

    MethodBuilderPREVIEW withCode(Consumer<? super CodeBuilderPREVIEW> code)
    Build the method body for this method.
    Parameters:
      `code` - a handler receiving a `CodeBuilder`PREVIEW
    Returns:
      this builder

  - ### transformCode

    MethodBuilderPREVIEW transformCode(CodeModelPREVIEW code, CodeTransformPREVIEW transform)
    Build the method body for this method by transforming the body of another method.
    Implementation Note:

      This method behaves as if:

              withCode(b -> b.transformCode(code, transform));

    Parameters:
      `code` - the method body to be transformed
      `transform` - the transform to apply to the method body
    Returns:
      this builder