APIdia Launch Partner
Top Description Methods
java.lang.classfile

public sealed Interface ClassBuilder

extends ClassFileBuilder<ClassElement, ClassBuilder>
permits ChainedClassBuilder, DirectClassBuilder
Known Direct Implementers
jdk.internal.classfile.impl.ChainedClassBuilder, jdk.internal.classfile.impl.DirectClassBuilder
Imports
java.lang.classfile.constantpool.ClassEntry, .ConstantPoolBuilder, .Utf8Entry, java.lang.constant.ClassDesc, .MethodTypeDesc, java.lang.reflect.AccessFlag, java.util.Arrays, .List, java.util.function.Consumer, jdk.internal.classfile.impl.AccessFlagsImpl, .ChainedClassBuilder, .DirectClassBuilder, .Util

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 TypeMethod and Description
public ClassBuilder

Returns:

this builder
transformField
(FieldModel
the field to be transformed
field
,
FieldTransform
the transform to apply to the field
transform
)

Adds a field by transforming a field from another class.

public ClassBuilder

Returns:

this builder
transformMethod
(MethodModel
the method to be transformed
method
,
MethodTransform
the transform to apply to the method
transform
)

Adds a method by transforming a method from another class.

public ClassBuilder

Returns:

this builder
withField
(Utf8Entry
the field name
name
,
Utf8Entry
the field descriptor string
descriptor
,
Consumer<? super FieldBuilder>
handler to supply the contents of the field
handler
)

Adds a field.

public default ClassBuilder

Returns:

this builder
withField
(Utf8Entry
the field name
name
,
Utf8Entry
the field descriptor string
descriptor
,
int
the access flags for this field, as a bit mask
flags
)

Adds a field, with only access flags.

public default ClassBuilder

Returns:

this builder
withField
(String
the field name
name
,
ClassDesc
the symbolic field descriptor
descriptor
,
Consumer<? super FieldBuilder>
handler to supply the contents of the field
handler
)

Adds a field.

public default ClassBuilder

Returns:

this builder
withField
(String
the field name
name
,
ClassDesc
the symbolic field descriptor
descriptor
,
int
the access flags for this field, as a bit mask
flags
)

Adds a field, with only access flags.

public default ClassBuilder

Returns:

this builder
withFlags
(int
the access flags, as a bit mask
flags
)

Sets the access flags of this class.

public default ClassBuilder

Returns:

this builder
withFlags
(AccessFlag...
the access flags, as flag enums
flags
)

Sets the access flags of this class.

public default ClassBuilder

Returns:

this builder
withInterfaces
(List<ClassEntry>
the interfaces
interfaces
)

Sets the interfaces of this class.

public default ClassBuilder

Returns:

this builder
withInterfaces
(ClassEntry...
the interfaces
interfaces
)

Sets the interfaces of this class.

public default ClassBuilder

Returns:

this builder
withInterfaceSymbols
(List<ClassDesc>
the interfaces
interfaces
)

Sets the interfaces of this class.

public default ClassBuilder

Returns:

this builder
withInterfaceSymbols
(ClassDesc...
the interfaces
interfaces
)

Sets the interfaces of this class.

public ClassBuilder

Returns:

this builder
withMethod
(Utf8Entry
the method name
name
,
Utf8Entry
the method descriptor
descriptor
,
int
the access flags as a bit mask, with the ACC_STATIC bit definitely set
methodFlags
,
Consumer<? super MethodBuilder>
handler to supply the contents of the method
handler
)

Adds a method.

public default ClassBuilder

Returns:

this builder
withMethod
(String
the method name
name
,
MethodTypeDesc
the method descriptor
descriptor
,
int
the access flags as a bit mask, with the ACC_STATIC bit definitely set
methodFlags
,
Consumer<? super MethodBuilder>
handler to supply the contents of the method
handler
)

Adds a method.

public default ClassBuilder

Returns:

this builder
withMethodBody
(Utf8Entry
the method name
name
,
Utf8Entry
the method descriptor
descriptor
,
int
the access flags as a bit mask, with the ACC_STATIC bit definitely set
methodFlags
,
Consumer<? super CodeBuilder>
handler to supply the contents of the method body
handler
)

Adds a method, with only access flags and a CodeModel.

public default ClassBuilder

Returns:

this builder
withMethodBody
(String
the method name
name
,
MethodTypeDesc
the method descriptor
descriptor
,
int
the access flags as a bit mask, with the ACC_STATIC bit definitely set
methodFlags
,
Consumer<? super CodeBuilder>
handler to supply the contents of the method body
handler
)

Adds a method, with only access flags and a CodeModel.

public default ClassBuilder

Returns:

this builder
withSuperclass
(ClassEntry
the superclass
superclassEntry
)

Sets the superclass of this class.

public default ClassBuilder

Returns:

this builder
withSuperclass
(ClassDesc
the superclass
desc
)

Sets the superclass of this class.

public default ClassBuilder

Returns:

this builder
withVersion
(int
the major version number
major
,
int
the minor version number
minor
)

Sets the version of this class.

Inherited from java.lang.classfile.ClassFileBuilder:
acceptconstantPooltransformwith

Method Detail

transformFieldback to summary
public 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))
withField(field.fieldName(), field.fieldType(),
          fb -> fb.transform(field, transform))
Parameters
field:FieldModel

the field to be transformed

transform:FieldTransform

the transform to apply to the field

Returns:ClassBuilder

this builder

See Also
FieldTransform
transformMethodback to summary
public 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))
withMethod(method.methodName(), method.methodType(), method.flags().flagMask(),
           mb -> mb.transform(method, transform))
Parameters
method:MethodModel

the method to be transformed

transform:MethodTransform

the transform to apply to the method

Returns:ClassBuilder

this builder

See Also
MethodTransform
withFieldback to summary
public ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler)

Adds a field.

Parameters
name:Utf8Entry

the field name

descriptor:Utf8Entry

the field descriptor string

handler:Consumer<? super FieldBuilder>

handler to supply the contents of the field

Returns:ClassBuilder

this builder

See Also
FieldModel
withFieldback to summary
public default ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, int flags)

Adds a field, with only access flags.

Parameters
name:Utf8Entry

the field name

descriptor:Utf8Entry

the field descriptor string

flags:int

the access flags for this field, as a bit mask

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if flags is not u2
See Also
FieldModel, FieldBuilder#withFlags(int)
withFieldback to summary
public default ClassBuilder withField(String name, ClassDesc descriptor, Consumer<? super FieldBuilder> handler)

Adds a field.

Parameters
name:String

the field name

descriptor:ClassDesc

the symbolic field descriptor

handler:Consumer<? super FieldBuilder>

handler to supply the contents of the field

Returns:ClassBuilder

this builder

See Also
FieldModel
withFieldback to summary
public default ClassBuilder withField(String name, ClassDesc descriptor, int flags)

Adds a field, with only access flags.

Parameters
name:String

the field name

descriptor:ClassDesc

the symbolic field descriptor

flags:int

the access flags for this field, as a bit mask

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if flags is not u2
See Also
FieldModel, FieldBuilder#withFlags(int)
withFlagsback to summary
public default ClassBuilder withFlags(int flags)

Sets the access flags of this class.

Parameters
flags:int

the access flags, as a bit mask

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if flags is not u2
See Also
AccessFlags, AccessFlag.Location#CLASS
withFlagsback to summary
public default ClassBuilder withFlags(AccessFlag... flags)

Sets the access flags of this class.

Parameters
flags:AccessFlag[]

the access flags, as flag enums

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if any flag cannot be applied to the AccessFlag.Location#CLASS location
See Also
AccessFlags, AccessFlag.Location#CLASS
withInterfacesback to summary
public default ClassBuilder withInterfaces(List<ClassEntry> interfaces)

Sets the interfaces of this class.

Parameters
interfaces:List<ClassEntry>

the interfaces

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if the number of interfaces exceeds the limit of u2
See Also
Interfaces
withInterfacesback to summary
public default ClassBuilder withInterfaces(ClassEntry... interfaces)

Sets the interfaces of this class.

Parameters
interfaces:ClassEntry[]

the interfaces

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if the number of interfaces exceeds the limit of u2
See Also
Interfaces
withInterfaceSymbolsback to summary
public default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces)

Sets the interfaces of this class.

Parameters
interfaces:List<ClassDesc>

the interfaces

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if any of interfaces is primitive, or if the number of interfaces exceeds the limit of u2
See Also
Interfaces
withInterfaceSymbolsback to summary
public default ClassBuilder withInterfaceSymbols(ClassDesc... interfaces)

Sets the interfaces of this class.

Parameters
interfaces:ClassDesc[]

the interfaces

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if any of interfaces is primitive, or if the number of interfaces exceeds the limit of u2
See Also
Interfaces
withMethodback to summary
public 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:Utf8Entry

the method name

descriptor:Utf8Entry

the method descriptor

methodFlags:int

the access flags as a bit mask, with the ACC_STATIC bit definitely set

handler:Consumer<? super MethodBuilder>

handler to supply the contents of the method

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if methodFlags is not u2
See Also
MethodModel
withMethodback to summary
public 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:String

the method name

descriptor:MethodTypeDesc

the method descriptor

methodFlags:int

the access flags as a bit mask, with the ACC_STATIC bit definitely set

handler:Consumer<? super MethodBuilder>

handler to supply the contents of the method

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if methodFlags is not u2
See Also
MethodModel
withMethodBodyback to summary
public 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))
withMethod(name, descriptor, methodFlags, mb -> mb.withCode(handler))
Parameters
name:Utf8Entry

the method name

descriptor:Utf8Entry

the method descriptor

methodFlags:int

the access flags as a bit mask, with the ACC_STATIC bit definitely set

handler:Consumer<? super CodeBuilder>

handler to supply the contents of the method body

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if methodFlags is not u2
See Also
MethodModel
withMethodBodyback to summary
public 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))
withMethod(name, descriptor, methodFlags, mb -> mb.withCode(handler))
Parameters
name:String

the method name

descriptor:MethodTypeDesc

the method descriptor

methodFlags:int

the access flags as a bit mask, with the ACC_STATIC bit definitely set

handler:Consumer<? super CodeBuilder>

handler to supply the contents of the method body

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if methodFlags is not u2
See Also
MethodModel
withSuperclassback to summary
public default ClassBuilder withSuperclass(ClassEntry superclassEntry)

Sets the superclass of this class.

Parameters
superclassEntry:ClassEntry

the superclass

Returns:ClassBuilder

this builder

See Also
Superclass
withSuperclassback to summary
public default ClassBuilder withSuperclass(ClassDesc desc)

Sets the superclass of this class.

Parameters
desc:ClassDesc

the superclass

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if desc represents a primitive type
See Also
Superclass
withVersionback to summary
public default ClassBuilder withVersion(int major, int minor)

Sets the version of this class.

Parameters
major:int

the major version number

minor:int

the minor version number

Returns:ClassBuilder

this builder

Exceptions
IllegalArgumentException:
if major or minor is not u2; minor may be -1 to indicate 65535
See Also
ClassFileVersion