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:
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 | Modifier and Type | Method | Description |
|---|---|---|
default void |
accept |
Integrates the member element into the structure being built. |
ConstantPoolBuilder |
constantPool() |
Returns the constant pool builder associated with this builder. |
default ClassBuilder |
transform |
Applies a transform to a compound structure, directing results to this builder. |
ClassBuilder |
with |
Integrates the member element into the structure being built. |
Methods declared in interface Consumer
andThen | Modifier and Type | Method | Description |
|---|---|---|
default Consumer |
andThen |
Returns a composed Consumer that performs, in sequence, this operation followed by the after operation. |
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
- Throws:
-
IllegalArgumentException- ifmajororminoris notu2;minormay be-1to indicate 65535 - See Also:
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
- Throws:
-
IllegalArgumentException- ifflagsis notu2 - See Also:
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 theAccessFlag.Location.CLASSlocation - See Also:
withSuperclass
default ClassBuilder withSuperclass(ClassEntry superclassEntry)
Sets the superclass of this class.
- Parameters:
-
superclassEntry- the superclass - Returns:
- this builder
- See Also:
withSuperclass
default ClassBuilder withSuperclass(ClassDesc desc)
Sets the superclass of this class.
- Parameters:
-
desc- the superclass - Returns:
- this builder
- Throws:
-
IllegalArgumentException- ifdescrepresents a primitive type - See Also:
withInterfaces
default ClassBuilder withInterfaces(List<ClassEntry> interfaces)
Sets the interfaces of this class.
- Parameters:
-
interfaces- the interfaces - Returns:
- this builder
- Throws:
-
IllegalArgumentException- if the number of interfaces exceeds the limit ofu2 - See Also:
withInterfaces
default ClassBuilder withInterfaces(ClassEntry... interfaces)
Sets the interfaces of this class.
- Parameters:
-
interfaces- the interfaces - Returns:
- this builder
- Throws:
-
IllegalArgumentException- if the number of interfaces exceeds the limit ofu2 - See Also:
withInterfaceSymbols
default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces)
Sets the interfaces of this class.
- Parameters:
-
interfaces- the interfaces - Returns:
- this builder
- Throws:
-
IllegalArgumentException- if any ofinterfacesis primitive, or if the number of interfaces exceeds the limit ofu2 - See Also:
withInterfaceSymbols
default ClassBuilder withInterfaceSymbols(ClassDesc... interfaces)
Sets the interfaces of this class.
- Parameters:
-
interfaces- the interfaces - Returns:
- this builder
- Throws:
-
IllegalArgumentException- if any ofinterfacesis primitive, or if the number of interfaces exceeds the limit ofu2 - See Also:
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:
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
- Throws:
-
IllegalArgumentException- ifflagsis notu2 - See Also:
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:
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
- Throws:
-
IllegalArgumentException- ifflagsis notu2 - See Also:
transformField
ClassBuilder transformField(FieldModel field, FieldTransform transform)
Adds a field by transforming a field from another class.
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)
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 theACC_STATICbit definitely set -
handler- handler to supply the contents of the method - Returns:
- this builder
- Throws:
-
IllegalArgumentException- ifmethodFlagsis notu2 - See Also:
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 theACC_STATICbit definitely set -
handler- handler to supply the contents of the method body - Returns:
- this builder
- Throws:
-
IllegalArgumentException- ifmethodFlagsis notu2 - See Also:
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 theACC_STATICbit definitely set -
handler- handler to supply the contents of the method - Returns:
- this builder
- Throws:
-
IllegalArgumentException- ifmethodFlagsis notu2 - See Also:
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 theACC_STATICbit definitely set -
handler- handler to supply the contents of the method body - Returns:
- this builder
- Throws:
-
IllegalArgumentException- ifmethodFlagsis notu2 - See Also:
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:
© 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.