- All Superinterfaces:
ClassfileBuilder<ClassElement,,ClassBuilder> Consumer<ClassElement>
- All Known Implementing Classes:
ChainedClassBuilder,DirectClassBuilder
public sealed interface ClassBuilder
extends ClassfileBuilder<ClassElement,ClassBuilder>
permits ChainedClassBuilder, DirectClassBuilder
A builder for classfiles. Builders are not created directly; they are passed
to handlers by methods such as
Classfile.build(ClassDesc, Consumer)
or to class transforms. The elements of a classfile can be specified
abstractly (by passing a ClassElement to ClassfileBuilder.with(ClassfileElement))
or concretely by calling the various withXxx methods.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionoriginal()Returns theClassModelrepresenting the class being transformed, if this class builder represents the transformation of someClassModel.transformField(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 ClassBuilderAdds a field.default ClassBuilderwithField(String name, ClassDesc descriptor, Consumer<? super FieldBuilder> handler) Adds a field.default ClassBuilderAdds a field.withField(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler) Adds a field.default ClassBuilderwithFlags(int flags) Sets the classfile access flags.default ClassBuilderwithFlags(AccessFlag... flags) Sets the classfile access flags.default ClassBuilderwithInterfaces(List<ClassEntry> interfaces) Sets the interfaces of this class.default ClassBuilderwithInterfaces(ClassEntry... interfaces) Sets the interfaces of this class.default ClassBuilderwithInterfaceSymbols(ClassDesc... interfaces) Sets the interfaces of this class.default ClassBuilderwithInterfaceSymbols(List<ClassDesc> interfaces) Sets the interfaces of this class.default ClassBuilderwithMethod(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super MethodBuilder> handler) Adds a method.withMethod(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super MethodBuilder> handler) Adds a method.default ClassBuilderwithMethodBody(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super CodeBuilder> handler) Adds a method, with only aCodeAttribute.default ClassBuilderwithMethodBody(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super CodeBuilder> handler) Adds a method, with only aCodeattribute.default ClassBuilderwithSuperclass(ClassDesc desc) Sets the superclass of this class.default ClassBuilderwithSuperclass(ClassEntry superclassEntry) Sets the superclass of this class.default ClassBuilderwithVersion(int major, int minor) Sets the classfile version.Methods inherited from interface org.glavo.classfile.ClassfileBuilder
accept, canWriteDirect, constantPool, transform, with
-
Method Details
-
original
Optional<ClassModel> original()Returns theClassModelrepresenting the class being transformed, if this class builder represents the transformation of someClassModel.- Returns:
- the
ClassModelrepresenting the class being transformed, if this class builder represents the transformation of someClassModel
-
withVersion
Sets the classfile version.- Parameters:
major- the major version numberminor- the minor version number- Returns:
- this builder
-
withFlags
Sets the classfile access flags.- Parameters:
flags- the access flags, as a bit mask- Returns:
- this builder
-
withFlags
Sets the classfile access flags.- Parameters:
flags- the access flags- Returns:
- this builder
-
withSuperclass
Sets the superclass of this class.- Parameters:
superclassEntry- the superclass- Returns:
- this builder
-
withSuperclass
Sets the superclass of this class.- Parameters:
desc- the superclass- Returns:
- this builder
-
withInterfaces
Sets the interfaces of this class.- Parameters:
interfaces- the interfaces- Returns:
- this builder
-
withInterfaces
Sets the interfaces of this class.- Parameters:
interfaces- the interfaces- Returns:
- this builder
-
withInterfaceSymbols
Sets the interfaces of this class.- Parameters:
interfaces- the interfaces- Returns:
- this builder
-
withInterfaceSymbols
Sets the interfaces of this class.- Parameters:
interfaces- the interfaces- Returns:
- this builder
-
withField
ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, Consumer<? super FieldBuilder> handler) Adds a field.- Parameters:
name- the name of the fielddescriptor- the field descriptorhandler- handler which receives aFieldBuilderwhich can further define the contents of the field- Returns:
- this builder
-
withField
Adds a field.- Parameters:
name- the name of the fielddescriptor- the field descriptorflags- the access flags for this field- Returns:
- this builder
-
withField
default ClassBuilder withField(String name, ClassDesc descriptor, Consumer<? super FieldBuilder> handler) Adds a field.- Parameters:
name- the name of the fielddescriptor- the field descriptorhandler- handler which receives aFieldBuilderwhich can further define the contents of the field- Returns:
- this builder
-
withField
Adds a field.- Parameters:
name- the name of the fielddescriptor- the field descriptorflags- the access flags for this field- Returns:
- this builder
-
transformField
Adds a field by transforming a field from another class.- Parameters:
field- the field to be transformedtransform- the transform to apply to the field- Returns:
- this builder
- ImplNote:
This method behaves as if:
withField(field.fieldName(), field.fieldType(), b -> b.transformField(field, transform));
-
withMethod
ClassBuilder withMethod(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super MethodBuilder> handler) Adds a method.- Parameters:
name- the name of the methoddescriptor- the method descriptormethodFlags- the access flagshandler- handler which receives aMethodBuilderwhich can further define the contents of the method- Returns:
- this builder
-
withMethodBody
default ClassBuilder withMethodBody(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super CodeBuilder> handler) Adds a method, with only aCodeattribute.- Parameters:
name- the name of the methoddescriptor- the method descriptormethodFlags- the access flagshandler- handler which receives aCodeBuilderwhich can define the contents of the method body- Returns:
- this builder
-
withMethod
default ClassBuilder withMethod(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super MethodBuilder> handler) Adds a method.- Parameters:
name- the name of the methoddescriptor- the method descriptormethodFlags- the access flagshandler- handler which receives aMethodBuilderwhich can further define the contents of the method- Returns:
- this builder
-
withMethodBody
default ClassBuilder withMethodBody(String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super CodeBuilder> handler) Adds a method, with only aCodeAttribute.- Parameters:
name- the name of the methoddescriptor- the method descriptormethodFlags- the access flagshandler- handler which receives aCodeBuilderwhich can define the contents of the method body- Returns:
- this builder
-
transformMethod
Adds a method by transforming a method from another class.- Parameters:
method- the method to be transformedtransform- the transform to apply to the method- Returns:
- this builder
- ImplNote:
This method behaves as if:
withMethod(method.methodName(), method.methodType(), b -> b.transformMethod(method, transform));
-