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

# Interface EnclosingMethodAttribute

All Superinterfaces:
  `Attribute<EnclosingMethodAttribute>, ClassElement, ClassFileElement`

public sealed interface EnclosingMethodAttribute extends Attribute<EnclosingMethodAttribute>, ClassElement
Models the `EnclosingMethod` attribute (JVMS 4.7.7), which indicates that this class is a local or anonymous class, and indicates the enclosing method or constructor of this class if this class is enclosed in exactly one method or constructor.

This attribute only appears on classes, and does not permit multiple instances in a class. It has a data dependency on the constant pool.

The attribute was introduced in the Java SE Platform version 5.0, major version 49.

See Java Virtual Machine Specification:
  4.7.7 The `EnclosingMethod` Attribute
Since:
  24
See Also:
  - `Attributes.enclosingMethod()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `ClassEntry`
  `enclosingClass()`
  Returns the class that encloses the declaration of the current class.
  `Optional<NameAndTypeEntry>`
  `enclosingMethod()`
  Returns the name and type of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
  `default Optional<Utf8Entry>`
  `enclosingMethodName()`
  Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
  `default Optional<Utf8Entry>`
  `enclosingMethodType()`
  Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
  `default Optional<MethodTypeDesc>`
  `enclosingMethodTypeSymbol()`
  Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
  `static EnclosingMethodAttribute`
  `of(ClassEntry className, Optional<NameAndTypeEntry> method)`
  Returns an `EnclosingMethod` attribute.
  `static EnclosingMethodAttribute`
  `of(ClassDesc className, Optional<String> methodName, Optional<MethodTypeDesc> methodType)`
  Returns an `EnclosingMethod` attribute.

  ### Methods declared in interface Attribute

  `attributeMapper, attributeName`
  Modifier and Type
  Method
  Description
  `AttributeMapper<EnclosingMethodAttribute>`
  `attributeMapper()`
  Returns the `AttributeMapper` associated with this attribute.
  `Utf8Entry`
  `attributeName()`
  Returns the name of the attribute.

- ## Method Details

  - ### enclosingClass

    ClassEntry enclosingClass()
    Returns the class that encloses the declaration of the current class. If the `enclosingMethod()` is present, this is the declaring class of that enclosing method or constructor.
    Returns:
      the class that encloses the declaration of the current class
    See Also:
      - `Class.getEnclosingClass()`

  - ### enclosingMethod

    Optional<NameAndTypeEntry> enclosingMethod()
    Returns the name and type of the enclosing method, if the class is immediately enclosed by exactly one method or constructor. This may be empty if the anonymous or local class appears in a field initializer (JLS 8.3.2), an instance initializer (JLS 8.6), or a static initializer (JLS 8.7). As a result, this never describes a class initialization method "<clinit>".
    Returns:
      the name and type of the enclosing method, if the class is immediately enclosed by exactly one method or constructor
    See Also:
      - `Class.getEnclosingMethod()`
      - `Class.getEnclosingConstructor()`

  - ### enclosingMethodName

    default Optional<Utf8Entry> enclosingMethodName()
    Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
    Returns:
      the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor
    See Also:
      - `enclosingMethod()`

  - ### enclosingMethodType

    default Optional<Utf8Entry> enclosingMethodType()
    Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
    Returns:
      the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor
    See Also:
      - `enclosingMethod()`

  - ### enclosingMethodTypeSymbol

    default Optional<MethodTypeDesc> enclosingMethodTypeSymbol()
    Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
    Returns:
      the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor
    See Also:
      - `enclosingMethod()`

  - ### of

    static EnclosingMethodAttribute of(ClassEntry className, Optional<NameAndTypeEntry> method)
    Returns an `EnclosingMethod` attribute.
    Parameters:
      `className` - the class name
      `method` - the name and type of the enclosing method or `Optional.empty()` if the class is not immediately enclosed by exactly one method or constructor
    Returns:
      an `EnclosingMethod` attribute

  - ### of

    static EnclosingMethodAttribute of(ClassDesc className, Optional<String> methodName, Optional<MethodTypeDesc> methodType)
    Returns an `EnclosingMethod` attribute.
    Parameters:
      `className` - the class name
      `methodName` - the name of the enclosing method or `Optional.empty()` if the class is not immediately enclosed by exactly one method or constructor
      `methodType` - the type of the enclosing method or `Optional.empty()` if the class is not immediately enclosed by exactly one method or constructor
    Returns:
      an `EnclosingMethod` attribute
    Throws:
      `IllegalArgumentException` - if `className` represents a primitive type