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

# Interface EnclosingMethodAttribute

All Superinterfaces:
  `AttributePREVIEW<EnclosingMethodAttributePREVIEW>`, `ClassElementPREVIEW`, `ClassFileElementPREVIEW`, `WritableElementPREVIEW<EnclosingMethodAttributePREVIEW>`

public sealed interface EnclosingMethodAttribute extends AttributePREVIEW<EnclosingMethodAttributePREVIEW>, ClassElementPREVIEW
`EnclosingMethodAttribute` is a preview API of the Java platform.
Programs can only use `EnclosingMethodAttribute` when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
Models the `EnclosingMethod` attribute 4.7.7, which can appear on classes, and indicates that the class is a local or anonymous class. Delivered as a `ClassElement`PREVIEW when traversing the elements of a `ClassModel`PREVIEW.

The attribute does not permit multiple instances in a given location. Subsequent occurrence of the attribute takes precedence during the attributed element build or transformation.

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

Since:
  22

- ## Method Summary

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

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

  `attributeMapper, attributeName`

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

  `writeTo`

- ## Method Details

  - ### enclosingClass

    ClassEntryPREVIEW enclosingClass()
    Returns the innermost class that encloses the declaration of the current class.
    Returns:
      the innermost class that encloses the declaration of the current class

  - ### enclosingMethod

    Optional<NameAndTypeEntryPREVIEW> enclosingMethod()
    Returns the name and type of the enclosing method, if the class is immediately enclosed by a method or constructor.
    Returns:
      the name and type of the enclosing method, if the class is immediately enclosed by a method or constructor

  - ### enclosingMethodName

    default Optional<Utf8EntryPREVIEW> enclosingMethodName()
    Returns the name of the enclosing method, if the class is immediately enclosed by a method or constructor.
    Returns:
      the name of the enclosing method, if the class is immediately enclosed by a method or constructor

  - ### enclosingMethodType

    default Optional<Utf8EntryPREVIEW> enclosingMethodType()
    Returns the type of the enclosing method, if the class is immediately enclosed by a method or constructor.
    Returns:
      the type of the enclosing method, if the class is immediately enclosed by a method or constructor

  - ### enclosingMethodTypeSymbol

    default Optional<MethodTypeDesc> enclosingMethodTypeSymbol()
    Returns the type of the enclosing method, if the class is immediately enclosed by a method or constructor.
    Returns:
      the type of the enclosing method, if the class is immediately enclosed by a method or constructor

  - ### of

    static EnclosingMethodAttributePREVIEW of(ClassEntryPREVIEW className, Optional<NameAndTypeEntryPREVIEW> method)
    Returns an `EnclosingMethod` attribute.
    Parameters:
      `className` - the class name
      `method` - the name and type of the enclosing method or `empty` if the class is not immediately enclosed by a method or constructor
    Returns:
      an `EnclosingMethod` attribute

  - ### of

    static EnclosingMethodAttributePREVIEW 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 `empty` if the class is not immediately enclosed by a method or constructor
      `methodType` - the type of the enclosing method or `empty` if the class is not immediately enclosed by a method or constructor
    Returns:
      an `EnclosingMethod` attribute
    Throws:
      `IllegalArgumentException` - if `className` represents a primitive type