ClassFileBuilder<ClassElement, ClassBuilder>, Consumer<ClassElement>public sealed interface ClassBuilder extends ClassFileBuilder<ClassElement, ClassBuilder>
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.
| Modifier and Type | Method | Description |
|---|---|---|
ClassBuilder |
transformField |
Adds a field by transforming a field from another class. |
ClassBuilder |
transformMethod |
Adds a method by transforming a method from another class. |
default ClassBuilder |
withField |
Adds a field, with only access flags. |
ClassBuilder |
withField |
Adds a field. |
default ClassBuilder |
withField |
Adds a field, with only access flags. |
default ClassBuilder |
withField |
Adds a field. |
default ClassBuilder |
withFlags |
Sets the access flags of this class. |
default ClassBuilder |
withFlags |
Sets the access flags of this class. |
default ClassBuilder |
withInterfaces |
Sets the interfaces of this class. |
default ClassBuilder |
withInterfaces |
Sets the interfaces of this class. |
default ClassBuilder |
withInterfaceSymbols |
Sets the interfaces of this class. |
default ClassBuilder |
withInterfaceSymbols |
Sets the interfaces of this class. |
ClassBuilder |
withMethod |
Adds a method. |
default ClassBuilder |
withMethod |
Adds a method. |
default ClassBuilder |
withMethodBody |
Adds a method, with only access flags and a CodeModel. |
default ClassBuilder |
withMethodBody |
Adds a method, with only access flags and a CodeModel. |
default ClassBuilder |
withSuperclass |
Sets the superclass of this class. |
default ClassBuilder |
withSuperclass |
Sets the superclass of this class. |
default ClassBuilder |
withVersion |
Sets the version of this class. |
accept, constantPool, transform, with
default ClassBuilder withVersion(int major, int minor)
major - the major version numberminor - the minor version numberdefault ClassBuilder withFlags(int flags)
flags - the access flags, as a bit maskdefault ClassBuilder withFlags(AccessFlag... flags)
flags - the access flags, as flag enumsIllegalArgumentException - if any flag cannot be applied to the AccessFlag.Location.CLASS locationdefault ClassBuilder withSuperclass(ClassEntry superclassEntry)
superclassEntry - the superclassdefault ClassBuilder withSuperclass(ClassDesc desc)
desc - the superclassIllegalArgumentException - if desc represents a primitive typedefault ClassBuilder withInterfaces(List<ClassEntry> interfaces)
interfaces - the interfacesdefault ClassBuilder withInterfaces(ClassEntry... interfaces)
interfaces - the interfacesdefault ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces)
interfaces - the interfacesIllegalArgumentException - if any element of interfaces is primitivedefault ClassBuilder withInterfaceSymbols(ClassDesc... interfaces)
interfaces - the interfacesIllegalArgumentException - if any element of interfaces is primitiveClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler)
name - the field namedescriptor - the field descriptor stringhandler - handler to supply the contents of the fielddefault ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, int flags)
name - the field namedescriptor - the field descriptor stringflags - the access flags for this field, as a bit maskdefault ClassBuilder withField(String name, ClassDesc descriptor, Consumer<? super FieldBuilder> handler)
name - the field namedescriptor - the symbolic field descriptorhandler - handler to supply the contents of the fielddefault ClassBuilder withField(String name, ClassDesc descriptor, int flags)
name - the field namedescriptor - the symbolic field descriptorflags - the access flags for this field, as a bit maskClassBuilder transformField(FieldModel field, FieldTransform transform)
This method behaves as if:
field - the field to be transformedtransform - the transform to apply to the fieldClassBuilder withMethod(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super MethodBuilder> handler)
ACC_STATIC flag cannot be modified by the handler later, and must be set through methodFlags.name - the method namedescriptor - the method descriptormethodFlags - the access flags as a bit mask, with the
ACC_STATIC bit definitely sethandler - handler to supply the contents of the methoddefault ClassBuilder withMethodBody(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super CodeBuilder> handler)
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))
name - the method namedescriptor - the method descriptormethodFlags - the access flags as a bit mask, with the
ACC_STATIC bit definitely sethandler - handler to supply the contents of the method bodydefault ClassBuilder withMethod(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super MethodBuilder> handler)
ACC_STATIC flag cannot be modified by the handler, and must be set through methodFlags.name - the method namedescriptor - the method descriptormethodFlags - the access flags as a bit mask, with the
ACC_STATIC bit definitely sethandler - handler to supply the contents of the methoddefault ClassBuilder withMethodBody(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super CodeBuilder> handler)
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))
name - the method namedescriptor - the method descriptormethodFlags - the access flags as a bit mask, with the
ACC_STATIC bit definitely sethandler - handler to supply the contents of the method bodyClassBuilder transformMethod(MethodModel method, MethodTransform transform)
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))
method - the method to be transformedtransform - the transform to apply to the method
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/classfile/ClassBuilder.html