APIdia Launch Partner
Top Description Methods
java.lang.classfile

public sealed Interface ClassFileBuilder<E extends ClassFileElement, B extends ClassFileBuilder<E, B>>

extends Consumer<E extends ClassFileElement>
permits ClassBuilder, FieldBuilder, MethodBuilder, CodeBuilder
Known Direct Subinterfaces
java.lang.classfile.CodeBuilder, java.lang.classfile.FieldBuilder, java.lang.classfile.MethodBuilder, java.lang.classfile.ClassBuilder
Type Parameters
<E>
the member element type
<B>
the self type of this builder
Imports
java.lang.classfile.constantpool.ConstantPoolBuilder, java.lang.constant.ClassDesc, java.util.function.Consumer, jdk.internal.classfile.impl.TransformImpl

A builder for a 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, 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.

Since
24
See Also
CompoundElement, ClassFileTransform

Method Summary

Modifier and TypeMethod and Description
public default void
accept(E
the member element
e
)

Implements java.util.function.Consumer.accept.

Integrates the member element into the structure being built.

public ConstantPoolBuilder

Returns:

the constant pool builder associated with this builder
constantPool
()

Returns the constant pool builder associated with this builder.

public default B

Returns:

this builder
transform
(CompoundElement<E>
the structure to transform
model
,
ClassFileTransform<?, E, B>
the transform to apply
transform
)

Applies a transform to a compound structure, directing results to this builder.

public B

Returns:

this builder
with
(E
the member element
e
)

Integrates the member element into the structure being built.

Inherited from java.util.function.Consumer:
andThen

Method Detail

acceptback to summary
public default void accept(E e)

Implements java.util.function.Consumer.accept.

Integrates the member element into the structure being built.

API Note

This method exists to implement Consumer; users can use with for call chaining.

Parameters
e:E

the member element

Annotations
@Override
Exceptions
IllegalArgumentException:
if the member element cannot be represented in the class file format
constantPoolback to summary
public ConstantPoolBuilder constantPool()

Returns the constant pool builder associated with this builder.

Returns:ConstantPoolBuilder

the constant pool builder associated with this builder

transformback to summary
public default B transform(CompoundElement<E> model, ClassFileTransform<?, E, B> transform)

Applies a transform to a compound structure, directing results to this builder.

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.

API Note

Many subinterfaces have methods like ClassBuilder#transformMethod or MethodBuilder#transformCode. 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.

Parameters
model:CompoundElement<E>

the structure to transform

transform:ClassFileTransform<?, E, B>

the transform to apply

Returns:B

this builder

See Also
ClassFileTransform
withback to summary
public B with(E e)

Integrates the member element into the structure being built.

Parameters
e:E

the member element

Returns:B

this builder

Exceptions
IllegalArgumentException:
if the member element cannot be represented in the class file format