Package com.tonic.builder
Class ClassBuilder
- java.lang.Object
-
- com.tonic.builder.ClassBuilder
-
- All Implemented Interfaces:
AccessFlags
public class ClassBuilder extends Object implements AccessFlags
Fluent builder that assembles aClassFilefrom scratch - version, access, hierarchy, fields, methods, annotations, and bootstrap methods.
-
-
Field Summary
-
Fields inherited from interface com.tonic.type.AccessFlags
ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_MANDATED, ACC_MODULE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ALOAD, ARETURN, ASTORE, DLOAD, DRETURN, DSTORE, FLOAD, FRETURN, FSTORE, ILOAD, IRETURN, ISTORE, LLOAD, LRETURN, LSTORE, RETURN, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6, V1_7, V1_8, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V9
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ClassBuilderaccess(int... flags)Replaces the access flags with the given flags ORed onto ACC_SUPER.FieldBuilderaddField(int access, String name, String descriptor)Opens a field on this class; callFieldBuilder.end()to return here.MethodBuilderaddMethod(int access, String name, String descriptor)Opens a method on this class; callMethodBuilder.end()to return here.AnnotationBuilder<ClassBuilder>annotate(String type)Opens an annotation on this class; callAnnotationBuilder.end()to return here.ClassFilebuild()Assembles the accumulated specification into aClassFile.static ClassBuildercreate(String className)Starts a new class with public access,java/lang/Objectsuperclass, and Java 11 version.ClassBuilderinterfaces(String... interfaceNames)Adds implemented interfaces.ClassBuildersuperClass(String superName)Sets the superclass.byte[]toByteArray()Serializes the class to class-file bytes, building it first if needed.ClassBuilderversion(int major, int minor)Sets the class-file version.
-
-
-
Method Detail
-
create
public static ClassBuilder create(String className)
Starts a new class with public access,java/lang/Objectsuperclass, and Java 11 version.- Parameters:
className- the internal name of the class to build- Returns:
- a new builder
-
version
public ClassBuilder version(int major, int minor)
Sets the class-file version.- Parameters:
major- the major versionminor- the minor version- Returns:
- this builder
-
access
public ClassBuilder access(int... flags)
Replaces the access flags with the given flags ORed onto ACC_SUPER.- Parameters:
flags- the access flags to combine- Returns:
- this builder
-
superClass
public ClassBuilder superClass(String superName)
Sets the superclass.- Parameters:
superName- the superclass internal name- Returns:
- this builder
-
interfaces
public ClassBuilder interfaces(String... interfaceNames)
Adds implemented interfaces.- Parameters:
interfaceNames- the interfaces' internal names- Returns:
- this builder
-
addField
public FieldBuilder addField(int access, String name, String descriptor)
Opens a field on this class; callFieldBuilder.end()to return here.- Parameters:
access- the field's access flagsname- the field namedescriptor- the field descriptor- Returns:
- the nested field builder
-
addMethod
public MethodBuilder addMethod(int access, String name, String descriptor)
Opens a method on this class; callMethodBuilder.end()to return here.- Parameters:
access- the method's access flagsname- the method namedescriptor- the method descriptor- Returns:
- the nested method builder
-
annotate
public AnnotationBuilder<ClassBuilder> annotate(String type)
Opens an annotation on this class; callAnnotationBuilder.end()to return here.- Parameters:
type- the annotation's type descriptor- Returns:
- the nested annotation builder
-
build
public ClassFile build()
Assembles the accumulated specification into aClassFile.- Returns:
- the built class file
- Throws:
RuntimeException- if assembling the class fails
-
toByteArray
public byte[] toByteArray()
Serializes the class to class-file bytes, building it first if needed.- Returns:
- the serialized class-file bytes
- Throws:
RuntimeException- if building or serializing the class fails
-
-