E - the member element typeB - the self type of this builderConsumer<E>ClassBuilder, CodeBuilder, CodeBuilder.BlockCodeBuilder, FieldBuilder, MethodBuilderpublic sealed interface ClassFileBuilder<E extends ClassFileElement, B extends ClassFileBuilder<E,B>> extends Consumer<E> permits ClassBuilder, FieldBuilder, MethodBuilder, CodeBuilder
CompoundElement, which accepts the member elements to be integrated into the built structure. Builders are usually passed as an argument to Consumer handlers, such as in ClassFile.build(ClassDesc, Consumer). The handlers should deliver elements to a builder similar to how a CompoundElement traverses its member elements. The basic way a builder accepts elements is through with(E), which supports call chaining. Concrete subtypes of builders usually define extra methods to define elements directly to the builder, such as ClassBuilder.withFlags(int) or CodeBuilder.aload(int).
Whether a member element can appear multiple times in a compound structure affects the behavior of the element in ClassFileBuilders. If an element can appear at most once but multiple instances are supplied to a ClassFileBuilder, the last supplied instance appears on the built structure. If an element appears exactly once but no instance is supplied, an unspecified default value element may be used in that structure.
Due to restrictions of the class file format, certain member elements that can be modeled by the API cannot be represented in the built structure under specific circumstances. Passing such elements to the builder causes IllegalArgumentException. Some ClassFile.Options control whether such elements should be altered or dropped to produce valid
class files.
| 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 B |
transform |
Applies a transform to a compound structure, directing results to this builder. |
B |
with |
Integrates the member element into the structure being built. |
default void accept(E e)
accept in interface Consumer<E extends ClassFileElement>
Consumer; users can use with(E) for call chaining.e - the member elementIllegalArgumentException - if the member element cannot be represented in the class file formatB with(E e)
e - the member elementIllegalArgumentException - if the member element cannot be represented in the class file formatConstantPoolBuilder constantPool()
default B transform(CompoundElement<E> model, ClassFileTransform<?,E,B> transform)
The transform will receive each element of the compound structure, as well as this builder for building the structure. The transform is free to preserve, remove, or replace elements as it sees fit.
A builder can run multiple transforms against different compound structures, integrating member elements of different origins.
ClassBuilder.transformMethod(MethodModel, MethodTransform) or MethodBuilder.transformCode(CodeModel, CodeTransform). However, calling them is fundamentally different from calling this method: those methods call the transform on the child builders instead of on itself. For example, classBuilder.transformMethod calls
methodBuilder.transform with a new method builder instead of calling classBuilder.transform on itself.model - the structure to transformtransform - the transform to apply
© 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/ClassFileBuilder.html