Package Summary Overview Summary

Module java.base
Package java.lang.classfilePREVIEW

# Interface ClassBuilder

All Superinterfaces:
  `ClassFileBuilderPREVIEW<ClassElementPREVIEW, ClassBuilderPREVIEW>` , `Consumer<ClassElementPREVIEW>`

public sealed interface ClassBuilder extends ClassFileBuilderPREVIEW<ClassElementPREVIEW, ClassBuilderPREVIEW>
`ClassBuilder` is a preview API of the Java platform.
Programs can only use `ClassBuilder` when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
A builder for classfiles. Builders are not created directly; they are passed to handlers by methods such as `ClassFile.build(ClassDesc, Consumer)` PREVIEW or to class transforms. The elements of a classfile can be specified abstractly (by passing a `ClassElement`PREVIEW to `ClassFileBuilder.with(ClassFileElement)`PREVIEW) or concretely by calling the various `withXxx` methods.

Since:
  22
See Also:
  - `ClassTransform`PREVIEW

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Optional <ClassModelPREVIEW>`
  `original()`
  Returns the `ClassModel`PREVIEW representing the class being transformed, if this class builder represents the transformation of some `ClassModel`PREVIEW.
  `ClassBuilderPREVIEW`
  `transformField (FieldModelPREVIEW field, FieldTransformPREVIEW transform)`
  Adds a field by transforming a field from another class.
  `ClassBuilderPREVIEW`
  `transformMethod (MethodModelPREVIEW method, MethodTransformPREVIEW transform)`
  Adds a method by transforming a method from another class.
  `default ClassBuilderPREVIEW`
  `withField (Utf8EntryPREVIEW name, Utf8EntryPREVIEW descriptor, int flags)`
  Adds a field.
  `ClassBuilderPREVIEW`
  `withField (Utf8EntryPREVIEW name, Utf8EntryPREVIEW descriptor, Consumer<? super FieldBuilderPREVIEW> handler)`
  Adds a field.
  `default ClassBuilderPREVIEW`
  `withField (String name, ClassDesc descriptor, int flags)`
  Adds a field.
  `default ClassBuilderPREVIEW`
  `withField (String name, ClassDesc descriptor, Consumer<? super FieldBuilderPREVIEW> handler)`
  Adds a field.
  `default ClassBuilderPREVIEW`
  `withFlags (int flags)`
  Sets the classfile access flags.
  `default ClassBuilderPREVIEW`
  `withFlags (AccessFlag... flags)`
  Sets the classfile access flags.
  `default ClassBuilderPREVIEW`
  `withInterfaces (ClassEntryPREVIEW... interfaces)`
  Sets the interfaces of this class.
  `default ClassBuilderPREVIEW`
  `withInterfaces (List<ClassEntryPREVIEW> interfaces)`
  Sets the interfaces of this class.
  `default ClassBuilderPREVIEW`
  `withInterfaceSymbols (ClassDesc... interfaces)`
  Sets the interfaces of this class.
  `default ClassBuilderPREVIEW`
  `withInterfaceSymbols (List<ClassDesc> interfaces)`
  Sets the interfaces of this class.
  `ClassBuilderPREVIEW`
  `withMethod (Utf8EntryPREVIEW name, Utf8EntryPREVIEW descriptor, int methodFlags, Consumer<? super MethodBuilderPREVIEW> handler)`
  Adds a method.
  `default ClassBuilderPREVIEW`
  `withMethod (String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super MethodBuilderPREVIEW> handler)`
  Adds a method.
  `default ClassBuilderPREVIEW`
  `withMethodBody (Utf8EntryPREVIEW name, Utf8EntryPREVIEW descriptor, int methodFlags, Consumer<? super CodeBuilderPREVIEW> handler)`
  Adds a method, with only a `Code` attribute.
  `default ClassBuilderPREVIEW`
  `withMethodBody (String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super CodeBuilderPREVIEW> handler)`
  Adds a method, with only a `CodeAttribute`PREVIEW.
  `default ClassBuilderPREVIEW`
  `withSuperclass (ClassEntryPREVIEW superclassEntry)`
  Sets the superclass of this class.
  `default ClassBuilderPREVIEW`
  `withSuperclass (ClassDesc desc)`
  Sets the superclass of this class.
  `default ClassBuilderPREVIEW`
  `withVersion (int major, int minor)`
  Sets the classfile version.

  ### Methods declared in interface java.lang.classfile.ClassFileBuilderPREVIEW

  `accept, canWriteDirect, constantPool, transform, with`

  ### Methods declared in interface java.util.function.Consumer

  `andThen`

- ## Method Details

  - ### original

    Optional<ClassModelPREVIEW> original()
    Returns the `ClassModel`PREVIEW representing the class being transformed, if this class builder represents the transformation of some `ClassModel`PREVIEW.
    Returns:
      the `ClassModel`PREVIEW representing the class being transformed, if this class builder represents the transformation of some `ClassModel`PREVIEW

  - ### withVersion

    default ClassBuilderPREVIEW 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 ClassBuilderPREVIEW withFlags (int flags)
    Sets the classfile access flags.
    Parameters:
      `flags` - the access flags, as a bit mask
    Returns:
      this builder

  - ### withFlags

    default ClassBuilderPREVIEW withFlags (AccessFlag... flags)
    Sets the classfile access flags.
    Parameters:
      `flags` - the access flags
    Returns:
      this builder

  - ### withSuperclass

    default ClassBuilderPREVIEW withSuperclass (ClassEntryPREVIEW superclassEntry)
    Sets the superclass of this class.
    Parameters:
      `superclassEntry` - the superclass
    Returns:
      this builder

  - ### withSuperclass

    default ClassBuilderPREVIEW 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 ClassBuilderPREVIEW withInterfaces (List<ClassEntryPREVIEW> interfaces)
    Sets the interfaces of this class.
    Parameters:
      `interfaces` - the interfaces
    Returns:
      this builder

  - ### withInterfaces

    default ClassBuilderPREVIEW withInterfaces (ClassEntryPREVIEW... interfaces)
    Sets the interfaces of this class.
    Parameters:
      `interfaces` - the interfaces
    Returns:
      this builder

  - ### withInterfaceSymbols

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

  - ### withInterfaceSymbols

    default ClassBuilderPREVIEW withInterfaceSymbols (ClassDesc... interfaces)
    Sets the interfaces of this class.
    Parameters:
      `interfaces` - the interfaces
    Returns:
      this builder

  - ### withField

    ClassBuilderPREVIEW withField (Utf8EntryPREVIEW name, Utf8EntryPREVIEW descriptor, Consumer<? super FieldBuilderPREVIEW> handler)
    Adds a field.
    Parameters:
      `name` - the name of the field
      `descriptor` - the field descriptor
      `handler` - handler which receives a `FieldBuilder`PREVIEW which can further define the contents of the field
    Returns:
      this builder

  - ### withField

    default ClassBuilderPREVIEW withField (Utf8EntryPREVIEW name, Utf8EntryPREVIEW 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 ClassBuilderPREVIEW withField (String name, ClassDesc descriptor, Consumer<? super FieldBuilderPREVIEW> handler)
    Adds a field.
    Parameters:
      `name` - the name of the field
      `descriptor` - the field descriptor
      `handler` - handler which receives a `FieldBuilder`PREVIEW which can further define the contents of the field
    Returns:
      this builder

  - ### withField

    default ClassBuilderPREVIEW 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

    ClassBuilderPREVIEW transformField (FieldModelPREVIEW field, FieldTransformPREVIEW 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

    ClassBuilderPREVIEW withMethod (Utf8EntryPREVIEW name, Utf8EntryPREVIEW descriptor, int methodFlags, Consumer<? super MethodBuilderPREVIEW> 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`PREVIEW which can further define the contents of the method
    Returns:
      this builder

  - ### withMethodBody

    default ClassBuilderPREVIEW withMethodBody (Utf8EntryPREVIEW name, Utf8EntryPREVIEW descriptor, int methodFlags, Consumer<? super CodeBuilderPREVIEW> 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`PREVIEW which can define the contents of the method body
    Returns:
      this builder

  - ### withMethod

    default ClassBuilderPREVIEW withMethod (String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super MethodBuilderPREVIEW> 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`PREVIEW which can further define the contents of the method
    Returns:
      this builder

  - ### withMethodBody

    default ClassBuilderPREVIEW withMethodBody (String name, MethodTypeDesc descriptor, int methodFlags, Consumer<? super CodeBuilderPREVIEW> handler)
    Adds a method, with only a `CodeAttribute`PREVIEW.
    Parameters:
      `name` - the name of the method
      `descriptor` - the method descriptor
      `methodFlags` - the access flags
      `handler` - handler which receives a `CodeBuilder`PREVIEW which can define the contents of the method body
    Returns:
      this builder

  - ### transformMethod

    ClassBuilderPREVIEW transformMethod (MethodModelPREVIEW method, MethodTransformPREVIEW 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