Interface ClassFileBuilder<E extends ClassFileElement, B extends ClassFileBuilder<E,B>>
- Type Parameters:
E
- the member element typeB
- the self type of this builder
- All Superinterfaces:
Consumer<E>
- All Known Subinterfaces:
ClassBuilder
,CodeBuilder
,CodeBuilder.BlockCodeBuilder
,FieldBuilder
,MethodBuilder
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 ClassFileBuilder
s. 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.Option
s control
whether such elements should be altered or dropped to produce valid
class
files.
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
Integrates the member element into the structure being built.Returns the constant pool builder associated with this builder.default B
transform
(CompoundElement<E> model, ClassFileTransform<?, E, B> transform) Applies a transform to a compound structure, directing results to this builder.Integrates the member element into the structure being built.
-
Method Details
-
accept
Integrates the member element into the structure being built.- Specified by:
accept
in interfaceConsumer<E extends ClassFileElement>
- API Note:
- This method exists to implement
Consumer
; users can usewith(E)
for call chaining. - Parameters:
e
- the member element- Throws:
IllegalArgumentException
- if the member element cannot be represented in theclass
file format
-
with
Integrates the member element into the structure being built.- Parameters:
e
- the member element- Returns:
- this builder
- Throws:
IllegalArgumentException
- if the member element cannot be represented in theclass
file format
-
constantPool
ConstantPoolBuilder constantPool()Returns the constant pool builder associated with this builder.- Returns:
- the constant pool builder associated with this builder
-
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(java.lang.classfile.MethodModel, java.lang.classfile.MethodTransform)
orMethodBuilder.transformCode(java.lang.classfile.CodeModel, java.lang.classfile.CodeTransform)
. However, calling them is fundamentally different from calling this method: those methods call thetransform
on the child builders instead of on itself. For example,classBuilder.transformMethod
callsmethodBuilder.transform
with a new method builder instead of callingclassBuilder.transform
on itself. - Parameters:
model
- the structure to transformtransform
- the transform to apply- Returns:
- this builder
- See Also:
-