Interface ClassBuilder
- All Superinterfaces:
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.
- Since:
- 24
- See Also:
Method Summary
| 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. |
Methods declared in interface ClassFileBuilder
accept, constantPool, transform, with
Method Details
withVersion
default ClassBuilder withVersion(int major, int minor)
- Parameters:
-
major- the major version number -
minor- the minor version number - Returns:
- this builder
- See Also:
withFlags
default ClassBuilder withFlags(int flags)
- Parameters:
-
flags- the access flags, as a bit mask - Returns:
- this builder
- See Also:
withFlags
default ClassBuilder withFlags(AccessFlag... flags)
- Parameters:
-
flags- the access flags, as flag enums - Returns:
- this builder
- Throws:
-
IllegalArgumentException- if any flag cannot be applied to theAccessFlag.Location.CLASSlocation - See Also:
withSuperclass
default ClassBuilder withSuperclass(ClassEntry superclassEntry)
- Parameters:
-
superclassEntry- the superclass - Returns:
- this builder
- See Also:
withSuperclass
default ClassBuilder withSuperclass(ClassDesc desc)
- Parameters:
-
desc- the superclass - Returns:
- this builder
- Throws:
-
IllegalArgumentException- ifdescrepresents a primitive type - See Also:
withInterfaces
default ClassBuilder withInterfaces(List<ClassEntry> interfaces)
- Parameters:
-
interfaces- the interfaces - Returns:
- this builder
- See Also:
withInterfaces
default ClassBuilder withInterfaces(ClassEntry... interfaces)
- Parameters:
-
interfaces- the interfaces - Returns:
- this builder
- See Also:
withInterfaceSymbols
default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces)
- Parameters:
-
interfaces- the interfaces - Returns:
- this builder
- Throws:
-
IllegalArgumentException- if any element ofinterfacesis primitive - See Also:
withInterfaceSymbols
default ClassBuilder withInterfaceSymbols(ClassDesc... interfaces)
- Parameters:
-
interfaces- the interfaces - Returns:
- this builder
- Throws:
-
IllegalArgumentException- if any element ofinterfacesis primitive - See Also:
withField
ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler)
- Parameters:
-
name- the field name -
descriptor- the field descriptor string -
handler- handler to supply the contents of the field - Returns:
- this builder
- See Also:
withField
default ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, int 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:
withField
default ClassBuilder withField(String name, ClassDesc descriptor, Consumer<? super FieldBuilder> handler)
- Parameters:
-
name- the field name -
descriptor- the symbolic field descriptor -
handler- handler to supply the contents of the field - Returns:
- this builder
- See Also:
withField
default ClassBuilder withField(String name, ClassDesc descriptor, int 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:
transformField
ClassBuilder transformField(FieldModel field, FieldTransform transform)
This method behaves as if:
- Parameters:
-
field- the field to be transformed -
transform- the transform to apply to the field - Returns:
- this builder
- See Also:
withMethod
ClassBuilder 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.- Parameters:
-
name- the method name -
descriptor- the method descriptor -
methodFlags- the access flags as a bit mask, with theACC_STATICbit definitely set -
handler- handler to supply the contents of the method - Returns:
- this builder
- See Also:
withMethodBody
default 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))
- Parameters:
-
name- the method name -
descriptor- the method descriptor -
methodFlags- the access flags as a bit mask, with theACC_STATICbit definitely set -
handler- handler to supply the contents of the method body - Returns:
- this builder
- See Also:
withMethod
default 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.- Parameters:
-
name- the method name -
descriptor- the method descriptor -
methodFlags- the access flags as a bit mask, with theACC_STATICbit definitely set -
handler- handler to supply the contents of the method - Returns:
- this builder
- See Also:
withMethodBody
default 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))
- Parameters:
-
name- the method name -
descriptor- the method descriptor -
methodFlags- the access flags as a bit mask, with theACC_STATICbit definitely set -
handler- handler to supply the contents of the method body - Returns:
- this builder
- See Also:
transformMethod
ClassBuilder 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))
- Parameters:
-
method- the method to be transformed -
transform- the transform to apply to the method - Returns:
- this builder
- See Also:
© 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