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

# Interface MethodModel

All Superinterfaces:
  `AttributedElement, ClassElement, ClassFileElement, CompoundElement<MethodElement>, Iterable<MethodElement>`

public sealed interface MethodModel extends CompoundElement<MethodElement>, AttributedElement, ClassElement
Models a method. A method can be viewed as a composition of `MethodElement`s, or by random access via accessor methods if only specific parts of the method is needed.

Methods can be obtained from `ClassModel.methods()`, or in the traversal of member elements of a class.

`ClassBuilder.withMethod(String, MethodTypeDesc, int, Consumer)` is the main way to construct methods. `ClassBuilder.transformMethod(MethodModel, MethodTransform)` allows creating a new method by selectively processing the original method elements and directing the results to a method builder.

All method attributes are accessible as member elements.

See Java Virtual Machine Specification:
  4.6 Methods
Since:
  24
See Also:
  - `ClassModel.methods()`
  - `MethodTransform`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Optional<CodeModel>`
  `code()`
  Returns the body of this method, if there is one.
  `AccessFlags`
  `flags()`
  Returns the access flags.
  `Utf8Entry`
  `methodName()`
  Returns the name of this method.
  `Utf8Entry`
  `methodType()`
  Returns the method descriptor string of this method.
  `default MethodTypeDesc`
  `methodTypeSymbol()`
  Returns the method type, as a symbolic descriptor.
  `Optional<ClassModel>`
  `parent()`
  Returns the class model this method is a member of, if known.

  ### Methods declared in interface AttributedElement

  `attributes, findAttribute, findAttributes`
  Modifier and Type
  Method
  Description
  `List<Attribute<?>>`
  `attributes()`
  Returns the attributes of this structure.
  `default <T extends Attribute<T>>
  Optional<T>`
  `findAttribute(AttributeMapper<T> attr)`
  Finds an attribute by name.
  `default <T extends Attribute<T>>
  List<T>`
  `findAttributes(AttributeMapper<T> attr)`
  Finds attributes by name.

  ### Methods declared in interface CompoundElement

  `elementList, elementStream, forEach, iterator, toDebugString`
  Modifier and Type
  Method
  Description
  `default List<MethodElement>`
  `elementList()`
  Returns a `List` containing all member elements in this compound element.
  `default Stream<MethodElement>`
  `elementStream()`
  Returns a `Stream` containing all member elements in this compound element.
  `void`
  `forEach(Consumer<? super MethodElement> consumer)`
  Invokes the provided handler with each member element in this compound element.
  `default Iterator<MethodElement>`
  `iterator()`
  Returns an `Iterator` describing all member elements in this compound element.
  `default String`
  `toDebugString()`
  Returns a text representation of the compound element and its contents for debugging purposes.

  ### Methods declared in interface Iterable

  `spliterator`
  Modifier and Type
  Method
  Description
  `default Spliterator<MethodElement>`
  `spliterator()`
  Creates a `Spliterator` over the elements described by this `Iterable`.

- ## Method Details

  - ### flags

    AccessFlags flags()
    Returns the access flags.
    Returns:
      the access flags
    See Also:
      - `AccessFlag.Location.METHOD`

  - ### parent

    Optional<ClassModel> parent()
    Returns the class model this method is a member of, if known.
    Returns:
      the class model this method is a member of, if known

  - ### methodName

    Utf8Entry methodName()
    Returns the name of this method.
    Returns:
      the name of this method

  - ### methodType

    Utf8Entry methodType()
    Returns the method descriptor string of this method.
    Returns:
      the method descriptor string of this method

  - ### methodTypeSymbol

    default MethodTypeDesc methodTypeSymbol()
    Returns the method type, as a symbolic descriptor.
    Returns:
      the method type, as a symbolic descriptor

  - ### code

    Optional<CodeModel> code()
    Returns the body of this method, if there is one.
    Returns:
      the body of this method, if there is one