Interface ClassBuilder

All Superinterfaces:
ClassFileBuilder<ClassElement, ClassBuilder>, Consumer<ClassElement>

public sealed interface ClassBuilder extends ClassFileBuilder<ClassElement, ClassBuilder>
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.
Since:
24
See Also:
  • Method Details

    • withVersion

      default ClassBuilder withVersion(int major, int minor)
      Sets the classfile version.
      Parameters:
      major - the major version number
      minor - the minor version number
      Returns:
      this builder
    • withFlags

      default ClassBuilder withFlags(int flags)
      Sets the classfile access flags.
      Parameters:
      flags - the access flags, as a bit mask
      Returns:
      this builder
    • withFlags

      default ClassBuilder withFlags(AccessFlag... flags)
      Sets the classfile access flags.
      Parameters:
      flags - the access flags
      Returns:
      this builder
    • withSuperclass

      default ClassBuilder withSuperclass(ClassEntry superclassEntry)
      Sets the superclass of this class.
      Parameters:
      superclassEntry - the superclass
      Returns:
      this builder
    • withSuperclass

      default ClassBuilder withSuperclass(ClassDesc desc)
      Sets the superclass of this class.
      Parameters:
      desc - the superclass
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if desc represents a primitive type
    • withInterfaces

      default ClassBuilder withInterfaces(List<ClassEntry> interfaces)
      Sets the interfaces of this class.
      Parameters:
      interfaces - the interfaces
      Returns:
      this builder
    • withInterfaces

      default ClassBuilder withInterfaces(ClassEntry... interfaces)
      Sets the interfaces of this class.
      Parameters:
      interfaces - the interfaces
      Returns:
      this builder
    • withInterfaceSymbols

      default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces)
      Sets the interfaces of this class.
      Parameters:
      interfaces - the interfaces
      Returns:
      this builder
    • withInterfaceSymbols

      default ClassBuilder withInterfaceSymbols(ClassDesc... interfaces)
      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 field
      descriptor - the field descriptor
      handler - handler which receives a FieldBuilder which can further define the contents of the field
      Returns:
      this builder
    • withField

      default ClassBuilder withField(Utf8Entry name, Utf8Entry descriptor, int flags)
      Adds a field.
      Parameters:
      name - the name of the field
      descriptor - the field descriptor
      flags - 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 field
      descriptor - the field descriptor
      handler - handler which receives a FieldBuilder which can further define the contents of the field
      Returns:
      this builder
    • withField

      default ClassBuilder withField(String name, ClassDesc descriptor, int flags)
      Adds a field.
      Parameters:
      name - the name of the field
      descriptor - the field descriptor
      flags - the access flags for this field
      Returns:
      this builder
    • transformField

      ClassBuilder transformField(FieldModel field, FieldTransform transform)
      Adds a field by transforming a field from another class.
      Implementation Note:

      This method behaves as if:

          withField(field.fieldName(), field.fieldType(),
                     b -> b.transformField(field, transform));
      
      Parameters:
      field - the field to be transformed
      transform - the transform to apply to the field
      Returns:
      this builder
    • withMethod

      ClassBuilder withMethod(Utf8Entry name, Utf8Entry descriptor, int methodFlags, Consumer<? super MethodBuilder> handler)
      Adds a method.
      Parameters:
      name - the name of the method
      descriptor - the method descriptor
      methodFlags - the access flags
      handler - handler which receives a MethodBuilder which 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 a Code attribute.
      Parameters:
      name - the name of the method
      descriptor - the method descriptor
      methodFlags - the access flags
      handler - handler which receives a CodeBuilder which 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 method
      descriptor - the method descriptor
      methodFlags - the access flags
      handler - handler which receives a MethodBuilder which 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 a CodeAttribute.
      Parameters:
      name - the name of the method
      descriptor - the method descriptor
      methodFlags - the access flags
      handler - handler which receives a CodeBuilder which can define the contents of the method body
      Returns:
      this builder
    • transformMethod

      ClassBuilder transformMethod(MethodModel method, MethodTransform transform)
      Adds a method by transforming a method from another class.
      Implementation Note:

      This method behaves as if:

          withMethod(method.methodName(), method.methodType(),
                     b -> b.transformMethod(method, transform));
      
      Parameters:
      method - the method to be transformed
      transform - the transform to apply to the method
      Returns:
      this builder