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

# Interface ClassModel

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

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

Use `ClassFile.parse(byte[])`, which parses the binary data of a `class` file into a model, to obtain a `ClassModel`.

To construct a `class` file, use `ClassFile.build(ClassDesc, Consumer)`. `ClassFile.transformClass(ClassModel, ClassTransform)` allows creating a new class by selectively processing the original class elements and directing the results to a class builder.

A class holds attributes, most of which are accessible as member elements. `BootstrapMethodsAttribute` can only be accessed via explicit attribute reading, as it is modeled as part of the constant pool.

See Java Virtual Machine Specification:
  4.1 The `ClassFile` Structure
Since:
  24
See Also:
  - `ClassFile.parse(byte[])`
  - `ClassTransform`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `ConstantPool`
  `constantPool()`
  Returns the constant pool for this class.
  `List<FieldModel>`
  `fields()`
  Returns the fields of this class.
  `AccessFlags`
  `flags()`
  Returns the access flags.
  `List<ClassEntry>`
  `interfaces()`
  Returns the interfaces implemented by this class.
  `boolean`
  `isModuleInfo()`
  Returns whether this `class` file is a module descriptor.
  `int`
  `majorVersion()`
  Returns the major version of this class.
  `List<MethodModel>`
  `methods()`
  Returns the methods of this class.
  `int`
  `minorVersion()`
  Returns the minor version of this class.
  `Optional<ClassEntry>`
  `superclass()`
  Returns the superclass of this class, if there is one.
  `ClassEntry`
  `thisClass()`
  Returns the constant pool entry describing the name of this class.

  ### 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<ClassElement>`
  `elementList()`
  Returns a `List` containing all member elements in this compound element.
  `default Stream<ClassElement>`
  `elementStream()`
  Returns a `Stream` containing all member elements in this compound element.
  `void`
  `forEach(Consumer<? super ClassElement> consumer)`
  Invokes the provided handler with each member element in this compound element.
  `default Iterator<ClassElement>`
  `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<ClassElement>`
  `spliterator()`
  Creates a `Spliterator` over the elements described by this `Iterable`.

- ## Method Details

  - ### constantPool

    ConstantPool constantPool()
    Returns the constant pool for this class.
    Returns:
      the constant pool for this class
    See Also:
      - `ConstantPoolBuilder.of(ClassModel)`

  - ### flags

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

  - ### thisClass

    ClassEntry thisClass()
    Returns the constant pool entry describing the name of this class.
    Returns:
      the constant pool entry describing the name of this class

  - ### majorVersion

    int majorVersion()
    Returns the major version of this class. It is a `u2` value.
    Returns:
      the major version of this class
    See Also:
      - `ClassFileVersion`

  - ### minorVersion

    int minorVersion()
    Returns the minor version of this class. It is a `u2` value.
    Returns:
      the minor version of this class
    See Also:
      - `ClassFileVersion`

  - ### fields

    List<FieldModel> fields()
    Returns the fields of this class.
    Returns:
      the fields of this class

  - ### methods

    List<MethodModel> methods()
    Returns the methods of this class.
    Returns:
      the methods of this class

  - ### superclass

    Optional<ClassEntry> superclass()
    Returns the superclass of this class, if there is one. This `class` file may have no superclass if this represents a module descriptor or the `Object` class; otherwise, it must have a superclass. If this is an interface, the superclass must be `Object`.
    Returns:
      the superclass of this class, if there is one
    See Also:
      - `Superclass`

  - ### interfaces

    List<ClassEntry> interfaces()
    Returns the interfaces implemented by this class.
    Returns:
      the interfaces implemented by this class
    See Also:
      - `Interfaces`

  - ### isModuleInfo

    boolean isModuleInfo()
    Returns whether this `class` file is a module descriptor.
    Returns:
      whether this `class` file is a module descriptor
    See Also:
      - `ClassFile.buildModule(ModuleAttribute, Consumer)`