# Interface ClassBuilder

All Superinterfaces:
  `ClassFileBuilder<ClassElement, ClassBuilder>, Consumer<ClassElement>`

public sealed interface ClassBuilder extends ClassFileBuilder<ClassElement, ClassBuilder>
A builder for a `class` file. `ClassFile` provides different `build` methods that accept handlers to configure such a builder; `ClassFile.build(ClassDesc, Consumer)` suffices for basic usage, while `ClassFile.build(ClassEntry, ConstantPoolBuilder, Consumer)` allows fine-grained control over the constant pool.

Refer to `ClassFileBuilder` for general guidance and caution around the use of builders for structures in the `class` file format.

Since:
  24
See Also:
  - `ClassFile.build(ClassEntry, ConstantPoolBuilder, Consumer)`
  - `ClassModel`
  - `ClassTransform`

## Method Summary

Modifier and Type
Method
Description
`ClassBuilder`
`transformField(FieldModel field, FieldTransform transform)`
Adds a field by transforming a field from another class.
`ClassBuilder`
`transformMethod(MethodModel method, MethodTransform transform)`
Adds a method by transforming a method from another class.
`default ClassBuilder`
`withField(Utf8Entry name, Utf8Entry descriptor, int flags)`
Adds a field, with only access flags.
`ClassBuilder`
`withField(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler)`
Adds a field.
`default ClassBuilder`
`withField(String name, ClassDesc descriptor, int flags)`
Adds a field, with only access flags.
`default ClassBuilder`
`withField(String name, ClassDesc descriptor, Consumer<? super FieldBuilder> handler)`
Adds a field.
`default ClassBuilder`
`withFlags(int flags)`
Sets the access flags of this class.
`default ClassBuilder`
`withFlags(AccessFlag... flags)`
Sets the access flags of this class.
`default ClassBuilder`
`withInterfaces(ClassEntry... interfaces)`
Sets the interfaces of this class.
`default ClassBuilder`
`withInterfaces(List<ClassEntry> interfaces)`
Sets the interfaces of this class.
`default ClassBuilder`
`withInterfaceSymbols(ClassDesc... interfaces)`
Sets the interfaces of this class.
`default ClassBuilder`
`withInterfaceSymbols(List<ClassDesc> interfaces)`
Sets the interfaces of this class.
`ClassBuilder`
`withMethod(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super MethodBuilder> handler)`
Adds a method.
`default ClassBuilder`
`withMethod(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super MethodBuilder> handler)`
Adds a method.
`default ClassBuilder`
`withMethodBody(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super CodeBuilder> handler)`
Adds a method, with only access flags and a `CodeModel`.
`default ClassBuilder`
`withMethodBody(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super CodeBuilder> handler)`
Adds a method, with only access flags and a `CodeModel`.
`default ClassBuilder`
`withSuperclass(ClassEntry superclassEntry)`
Sets the superclass of this class.
`default ClassBuilder`
`withSuperclass(ClassDesc desc)`
Sets the superclass of this class.
`default ClassBuilder`
`withVersion(int major, int minor)`
Sets the version of this class.

### Methods declared in interface ClassFileBuilder

`accept, constantPool, transform, with`

### Methods declared in interface Consumer

`andThen`

## Method Details

### withVersion

default ClassBuilder withVersion(int major, int minor)
Sets the version of this class.

Parameters:
  `major` - the major version number
  `minor` - the minor version number
Returns:
  this builder
See Also:
  - `ClassFileVersion`

### withFlags

default ClassBuilder withFlags(int flags)
Sets the access flags of this class.

Parameters:
  `flags` - the access flags, as a bit mask
Returns:
  this builder
See Also:
  - `AccessFlags`
  - `AccessFlag.Location.CLASS`

### withFlags

default ClassBuilder withFlags(AccessFlag... flags)
Sets the access flags of this class.

Parameters:
  `flags` - the access flags, as flag enums
Returns:
  this builder
Throws:
  `IllegalArgumentException` - if any flag cannot be applied to the `AccessFlag.Location.CLASS` location
See Also:
  - `AccessFlags`
  - `AccessFlag.Location.CLASS`

### withSuperclass

default ClassBuilder withSuperclass(ClassEntry superclassEntry)
Sets the superclass of this class.

Parameters:
  `superclassEntry` - the superclass
Returns:
  this builder
See Also:
  - `Superclass`

### withSuperclass

default ClassBuilder withSuperclass(ClassDesc desc)
Sets the superclass of this class.

Parameters:
  `desc` - the superclass
Returns:
  this builder
Throws:
  `IllegalArgumentException` - if `desc` represents a primitive type
See Also:
  - `Superclass`

### withInterfaces

default ClassBuilder withInterfaces(List<ClassEntry> interfaces)
Sets the interfaces of this class.

Parameters:
  `interfaces` - the interfaces
Returns:
  this builder
See Also:
  - `Interfaces`

### withInterfaces

default ClassBuilder withInterfaces(ClassEntry... interfaces)
Sets the interfaces of this class.

Parameters:
  `interfaces` - the interfaces
Returns:
  this builder
See Also:
  - `Interfaces`

### withInterfaceSymbols

default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces)
Sets the interfaces of this class.

Parameters:
  `interfaces` - the interfaces
Returns:
  this builder
Throws:
  `IllegalArgumentException` - if any element of `interfaces` is primitive
See Also:
  - `Interfaces`

### withInterfaceSymbols

default ClassBuilder withInterfaceSymbols(ClassDesc... interfaces)
Sets the interfaces of this class.

Parameters:
  `interfaces` - the interfaces
Returns:
  this builder
Throws:
  `IllegalArgumentException` - if any element of `interfaces` is primitive
See Also:
  - `Interfaces`

### withField

ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler)
Adds a field.

Parameters:
  `name` - the field name
  `descriptor` - the field descriptor string
  `handler` - handler to supply the contents of the field
Returns:
  this builder
See Also:
  - `FieldModel`

### withField

default ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, int flags)
Adds a field, with only access flags.

Parameters:
  `name` - the field name
  `descriptor` - the field descriptor string
  `flags` - the access flags for this field, as a bit mask
Returns:
  this builder
See Also:
  - `FieldModel`
  - `FieldBuilder.withFlags(int)`

### withField

default ClassBuilder withField(String name, ClassDesc descriptor, Consumer<? super FieldBuilder> handler)
Adds a field.

Parameters:
  `name` - the field name
  `descriptor` - the symbolic field descriptor
  `handler` - handler to supply the contents of the field
Returns:
  this builder
See Also:
  - `FieldModel`

### withField

default ClassBuilder withField(String name, ClassDesc descriptor, int flags)
Adds a field, with only access flags.

Parameters:
  `name` - the field name
  `descriptor` - the symbolic field descriptor
  `flags` - the access flags for this field, as a bit mask
Returns:
  this builder
See Also:
  - `FieldModel`
  - `FieldBuilder.withFlags(int)`

### transformField

ClassBuilder transformField(FieldModel field, FieldTransform transform)
Adds a field by transforming a field from another class.

This method behaves as if:

    withField(field.fieldName(), field.fieldType(),
              fb -> fb.transform(field, transform))

Parameters:
  `field` - the field to be transformed
  `transform` - the transform to apply to the field
Returns:
  this builder
See Also:
  - `FieldTransform`

### withMethod

ClassBuilder withMethod(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super MethodBuilder> handler)
Adds a method. The bit for `ACC_STATIC` flag cannot be modified by the `handler` later, and must be set through `methodFlags`.

Parameters:
  `name` - the method name
  `descriptor` - the method descriptor
  `methodFlags` - the access flags as a bit mask, with the `ACC_STATIC` bit definitely set
  `handler` - handler to supply the contents of the method
Returns:
  this builder
See Also:
  - `MethodModel`

### withMethodBody

default ClassBuilder withMethodBody(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super CodeBuilder> handler)
Adds a method, with only access flags and a `CodeModel`. The bit for `ACC_STATIC` flag cannot be modified by the `handler` later, and must be set through `methodFlags`.

This method behaves as if:

    withMethod(name, descriptor, methodFlags, mb -> mb.withCode(handler))

Parameters:
  `name` - the method name
  `descriptor` - the method descriptor
  `methodFlags` - the access flags as a bit mask, with the `ACC_STATIC` bit definitely set
  `handler` - handler to supply the contents of the method body
Returns:
  this builder
See Also:
  - `MethodModel`

### withMethod

default ClassBuilder withMethod(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super MethodBuilder> handler)
Adds a method. The bit for `ACC_STATIC` flag cannot be modified by the `handler`, and must be set through `methodFlags`.

Parameters:
  `name` - the method name
  `descriptor` - the method descriptor
  `methodFlags` - the access flags as a bit mask, with the `ACC_STATIC` bit definitely set
  `handler` - handler to supply the contents of the method
Returns:
  this builder
See Also:
  - `MethodModel`

### withMethodBody

default ClassBuilder withMethodBody(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super CodeBuilder> handler)
Adds a method, with only access flags and a `CodeModel`. The bit for `ACC_STATIC` flag cannot be modified by the `handler`, and must be set through `methodFlags`.

This method behaves as if:

    withMethod(name, descriptor, methodFlags, mb -> mb.withCode(handler))

Parameters:
  `name` - the method name
  `descriptor` - the method descriptor
  `methodFlags` - the access flags as a bit mask, with the `ACC_STATIC` bit definitely set
  `handler` - handler to supply the contents of the method body
Returns:
  this builder
See Also:
  - `MethodModel`

### transformMethod

ClassBuilder transformMethod(MethodModel method, MethodTransform transform)
Adds a method by transforming a method from another class. The transform cannot modify the `ACC_STATIC` flag of the original method.

This method behaves as if:

    withMethod(method.methodName(), method.methodType(), method.flags().flagMask(),
               mb -> mb.transform(method, transform))

Parameters:
  `method` - the method to be transformed
  `transform` - the transform to apply to the method
Returns:
  this builder
See Also:
  - `MethodTransform`