APIdia Launch Partner
Top Description Methods
java.lang.classfile.attribute

public sealed Interface EnclosingMethodAttribute

extends Attribute<EnclosingMethodAttribute>, ClassElement
permits BoundEnclosingMethodAttribute, UnboundEnclosingMethodAttribute
Known Direct Implementers
jdk.internal.classfile.impl.BoundAttribute.BoundEnclosingMethodAttribute, jdk.internal.classfile.impl.UnboundAttribute.UnboundEnclosingMethodAttribute
Imports
java.lang.classfile.Attribute, .AttributeMapper, .AttributeMapper.AttributeStability, .Attributes, .ClassElement, .ClassFile, java.lang.classfile.constantpool.ClassEntry, .NameAndTypeEntry, .Utf8Entry, java.lang.constant.ClassDesc, .ConstantDescs, .MethodTypeDesc, java.util.Optional, jdk.internal.classfile.impl.BoundAttribute, .TemporaryConstantPool, .UnboundAttribute, .Util

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.

Since
24
Java Virtual Machine Specification
4.7.7 The EnclosingMethod Attribute
See Also
Attributes#enclosingMethod()

Method Summary

Modifier and TypeMethod and Description
public ClassEntry

Returns:

the class that encloses the declaration of the current class
enclosingClass
()

Returns the class that encloses the declaration of the current class.

public Optional<NameAndTypeEntry>

Returns:

the name and type of the enclosing method, if the class is immediately enclosed by exactly one method or constructor
enclosingMethod
()

Returns the name and type of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.

public default Optional<Utf8Entry>

Returns:

the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor
enclosingMethodName
()

Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.

public default Optional<Utf8Entry>

Returns:

the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor
enclosingMethodType
()

Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.

public default Optional<MethodTypeDesc>

Returns:

the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor
enclosingMethodTypeSymbol
()

Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.

public static EnclosingMethodAttribute

Returns:

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

Returns an EnclosingMethod attribute.

public static EnclosingMethodAttribute

Returns:

an EnclosingMethod attribute
of
(ClassDesc
the class name
className
,
Optional<String>
the name of the enclosing method or Optional.empty() if the class is not immediately enclosed by exactly one method or constructor
methodName
,
Optional<MethodTypeDesc>
the type of the enclosing method or Optional.empty() if the class is not immediately enclosed by exactly one method or constructor
methodType
)

Returns an EnclosingMethod attribute.

Inherited from java.lang.classfile.Attribute:
attributeMapperattributeName

Method Detail

enclosingClassback to summary
public 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:ClassEntry

the class that encloses the declaration of the current class

See Also
Class#getEnclosingClass()
enclosingMethodback to summary
public 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:Optional<NameAndTypeEntry>

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()
enclosingMethodNameback to summary
public default Optional<Utf8Entry> enclosingMethodName()

Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.

Returns:Optional<Utf8Entry>

the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor

See Also
enclosingMethod()
enclosingMethodTypeback to summary
public default Optional<Utf8Entry> enclosingMethodType()

Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.

Returns:Optional<Utf8Entry>

the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor

See Also
enclosingMethod()
enclosingMethodTypeSymbolback to summary
public default Optional<MethodTypeDesc> enclosingMethodTypeSymbol()

Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.

Returns:Optional<MethodTypeDesc>

the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor

See Also
enclosingMethod()
ofback to summary
public static EnclosingMethodAttribute of(ClassEntry className, Optional<NameAndTypeEntry> method)

Returns an EnclosingMethod attribute.

Parameters
className:ClassEntry

the class name

method:Optional<NameAndTypeEntry>

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:EnclosingMethodAttribute

an EnclosingMethod attribute

ofback to summary
public static EnclosingMethodAttribute of(ClassDesc className, Optional<String> methodName, Optional<MethodTypeDesc> methodType)

Returns an EnclosingMethod attribute.

Parameters
className:ClassDesc

the class name

methodName:Optional<String>

the name of the enclosing method or Optional.empty() if the class is not immediately enclosed by exactly one method or constructor

methodType:Optional<MethodTypeDesc>

the type of the enclosing method or Optional.empty() if the class is not immediately enclosed by exactly one method or constructor

Returns:EnclosingMethodAttribute

an EnclosingMethod attribute

Exceptions
IllegalArgumentException:
if className represents a primitive type