Interface ClassBuilder
- All Superinterfaces:
ClassFileBuilder<ClassElement, ClassBuilder>
,Consumer<ClassElement>
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 TypeMethodDescriptiontransformField
(FieldModel field, FieldTransform transform) Adds a field by transforming a field from another class.transformMethod
(MethodModel method, MethodTransform transform) Adds a method by transforming a method from another class.default ClassBuilder
Adds a field, with only access flags.withField
(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler) Adds a field.default ClassBuilder
Adds a field, with only access flags.default ClassBuilder
withField
(String name, ClassDesc descriptor, Consumer<? super FieldBuilder> handler) Adds a field.default ClassBuilder
withFlags
(int flags) Sets the access flags of this class.default ClassBuilder
withFlags
(AccessFlag... flags) Sets the access flags of this class.default ClassBuilder
withInterfaces
(ClassEntry... interfaces) Sets the interfaces of this class.default ClassBuilder
withInterfaces
(List<ClassEntry> interfaces) Sets the interfaces of this class.default ClassBuilder
withInterfaceSymbols
(ClassDesc... interfaces) Sets the interfaces of this class.default ClassBuilder
withInterfaceSymbols
(List<ClassDesc> interfaces) Sets the interfaces of this class.withMethod
(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super MethodBuilder> handler) Adds a method.default ClassBuilder
withMethod
(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super MethodBuilder> handler) Adds a method.default ClassBuilder
withMethodBody
(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super CodeBuilder> handler) Adds a method, with only access flags and aCodeModel
.default ClassBuilder
withMethodBody
(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super CodeBuilder> handler) Adds a method, with only access flags and aCodeModel
.default ClassBuilder
withSuperclass
(ClassEntry superclassEntry) Sets the superclass of this class.default ClassBuilder
withSuperclass
(ClassDesc desc) Sets the superclass of this class.default ClassBuilder
withVersion
(int major, int minor) Sets the version of this class.Methods declared in interface java.lang.classfile.ClassFileBuilder
accept, constantPool, transform, with
-
Method Details
-
withVersion
Sets the version of this class.- Parameters:
major
- the major version numberminor
- the minor version number- Returns:
- this builder
- See Also:
-
withFlags
Sets the access flags of this class.- Parameters:
flags
- the access flags, as a bit mask- Returns:
- this builder
- See Also:
-
withFlags
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.CLASS
location- See Also:
-
withSuperclass
Sets the superclass of this class.- Parameters:
superclassEntry
- the superclass- Returns:
- this builder
- See Also:
-
withSuperclass
Sets the superclass of this class.- Parameters:
desc
- the superclass- Returns:
- this builder
- Throws:
IllegalArgumentException
- ifdesc
represents a primitive type- See Also:
-
withInterfaces
Sets the interfaces of this class.- Parameters:
interfaces
- the interfaces- Returns:
- this builder
- See Also:
-
withInterfaces
Sets the interfaces of this class.- Parameters:
interfaces
- the interfaces- Returns:
- this builder
- See Also:
-
withInterfaceSymbols
Sets the interfaces of this class.- Parameters:
interfaces
- the interfaces- Returns:
- this builder
- Throws:
IllegalArgumentException
- if any element ofinterfaces
is primitive- See Also:
-
withInterfaceSymbols
Sets the interfaces of this class.- Parameters:
interfaces
- the interfaces- Returns:
- this builder
- Throws:
IllegalArgumentException
- if any element ofinterfaces
is primitive- See Also:
-
withField
ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler) Adds a field.- Parameters:
name
- the field namedescriptor
- the field descriptor stringhandler
- handler to supply the contents of the field- Returns:
- this builder
- See Also:
-
withField
Adds a field, with only access flags.- Parameters:
name
- the field namedescriptor
- the field descriptor stringflags
- 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) Adds a field.- Parameters:
name
- the field namedescriptor
- the symbolic field descriptorhandler
- handler to supply the contents of the field- Returns:
- this builder
- See Also:
-
withField
Adds a field, with only access flags.- Parameters:
name
- the field namedescriptor
- the symbolic field descriptorflags
- the access flags for this field, as a bit mask- Returns:
- this builder
- See Also:
-
transformField
Adds a field by transforming a field from another class.This method behaves as if:
- Parameters:
field
- the field to be transformedtransform
- 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 forACC_STATIC
flag cannot be modified by thehandler
later, and must be set throughmethodFlags
.- Parameters:
name
- the method namedescriptor
- the method descriptormethodFlags
- the access flags as a bit mask, with theACC_STATIC
bit definitely sethandler
- 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) Adds a method, with only access flags and aCodeModel
. The bit forACC_STATIC
flag cannot be modified by thehandler
later, and must be set throughmethodFlags
.This method behaves as if:
withMethod
(name, descriptor, methodFlags, mb -> mb.withCode
(handler))- Parameters:
name
- the method namedescriptor
- the method descriptormethodFlags
- the access flags as a bit mask, with theACC_STATIC
bit definitely sethandler
- 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) Adds a method. The bit forACC_STATIC
flag cannot be modified by thehandler
, and must be set throughmethodFlags
.- Parameters:
name
- the method namedescriptor
- the method descriptormethodFlags
- the access flags as a bit mask, with theACC_STATIC
bit definitely sethandler
- 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) Adds a method, with only access flags and aCodeModel
. The bit forACC_STATIC
flag cannot be modified by thehandler
, and must be set throughmethodFlags
.This method behaves as if:
withMethod
(name, descriptor, methodFlags, mb -> mb.withCode
(handler))- Parameters:
name
- the method namedescriptor
- the method descriptormethodFlags
- the access flags as a bit mask, with theACC_STATIC
bit definitely sethandler
- handler to supply the contents of the method body- Returns:
- this builder
- See Also:
-
transformMethod
Adds a method by transforming a method from another class. The transform cannot modify theACC_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 transformedtransform
- the transform to apply to the method- Returns:
- this builder
- See Also:
-