ClassTransform (Java SE 23 & JDK 23)
Description: declaration: module: java.base, package: java.lang.classfile, interface: ClassTransform
Keywords: java.lang.classfile.ClassTransform interface

# Interface ClassTransform

All Superinterfaces:
  `ClassFileTransformPREVIEW<ClassTransformPREVIEW,ClassElementPREVIEW,ClassBuilderPREVIEW>`

All Known Subinterfaces:
  `ClassRemapperPREVIEW`

Functional Interface:
  This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public non-sealed interface ClassTransform extends ClassFileTransformPREVIEW<ClassTransformPREVIEW,ClassElementPREVIEW,ClassBuilderPREVIEW>
`ClassTransform` is a preview API of the Java platform.
Programs can only use `ClassTransform` when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
A transformation on streams of `ClassElement`PREVIEW.

Since:
  22
See Also:
  - `ClassFileTransform`PREVIEW

- ## Nested Class Summary

  ### Nested classes/interfaces declared in interface java.lang.classfile.ClassFileTransformPREVIEW

  `ClassFileTransform.ResolvedTransformPREVIEW<E extends ClassFileElementPREVIEW>`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final ClassTransformPREVIEW`
  `ACCEPT_ALL`
  A class transform that sends all elements to the builder.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `default ClassTransformPREVIEW`
  `andThen(ClassTransformPREVIEW t)`
  Chain this transform with another; elements presented to the builder of this transform will become the input to the next transform.
  `static ClassTransformPREVIEW`
  `dropping(Predicate<ClassElementPREVIEW> filter)`
  Create a class transform that passes each element through to the builder, except for those that the supplied `Predicate` is true for.
  `static ClassTransformPREVIEW`
  `endHandler(Consumer<ClassBuilderPREVIEW> finisher)`
  Create a class transform that passes each element through to the builder, and calls the specified function when transformation is complete.
  `static ClassTransformPREVIEW`
  `ofStateful(Supplier<ClassTransformPREVIEW> supplier)`
  Create a stateful class transform from a `Supplier`.
  `default ClassFileTransform.ResolvedTransformPREVIEW<ClassElementPREVIEW>`
  `resolve(ClassBuilderPREVIEW builder)`
  Bind a transform to a builder.
  `static ClassTransformPREVIEW`
  `transformingFields(FieldTransformPREVIEW xform)`
  Create a class transform that transforms `FieldModel`PREVIEW elements with the supplied field transform.
  `static ClassTransformPREVIEW`
  `transformingMethodBodies(CodeTransformPREVIEW xform)`
  Create a class transform that transforms the `CodeAttribute`PREVIEW (method body) of `MethodModel`PREVIEW elements with the supplied code transform.
  `static ClassTransformPREVIEW`
  `transformingMethodBodies(Predicate<MethodModelPREVIEW> filter, CodeTransformPREVIEW xform)`
  Create a class transform that transforms the `CodeAttribute`PREVIEW (method body) of `MethodModel`PREVIEW elements with the supplied code transform.
  `static ClassTransformPREVIEW`
  `transformingMethods(MethodTransformPREVIEW xform)`
  Create a class transform that transforms `MethodModel`PREVIEW elements with the supplied method transform.
  `static ClassTransformPREVIEW`
  `transformingMethods(Predicate<MethodModelPREVIEW> filter, MethodTransformPREVIEW xform)`
  Create a class transform that transforms `MethodModel`PREVIEW elements with the supplied method transform.

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

  `accept, atEnd, atStart`

- ## Field Details

  - ### ACCEPT_ALL

    static final ClassTransformPREVIEW ACCEPT_ALL
    A class transform that sends all elements to the builder.

- ## Method Details

  - ### ofStateful

    static ClassTransformPREVIEW ofStateful(Supplier<ClassTransformPREVIEW> supplier)
    Create a stateful class transform from a `Supplier`. The supplier will be invoked for each transformation.
    Parameters:
      `supplier` - a `Supplier` that produces a fresh transform object for each traversal
    Returns:
      the stateful class transform

  - ### endHandler

    static ClassTransformPREVIEW endHandler(Consumer<ClassBuilderPREVIEW> finisher)
    Create a class transform that passes each element through to the builder, and calls the specified function when transformation is complete.
    Parameters:
      `finisher` - the function to call when transformation is complete
    Returns:
      the class transform

  - ### dropping

    static ClassTransformPREVIEW dropping(Predicate<ClassElementPREVIEW> filter)
    Create a class transform that passes each element through to the builder, except for those that the supplied `Predicate` is true for.
    Parameters:
      `filter` - the predicate that determines which elements to drop
    Returns:
      the class transform

  - ### transformingMethods

    static ClassTransformPREVIEW transformingMethods(Predicate<MethodModelPREVIEW> filter, MethodTransformPREVIEW xform)
    Create a class transform that transforms `MethodModel`PREVIEW elements with the supplied method transform.
    Parameters:
      `filter` - a predicate that determines which methods to transform
      `xform` - the method transform
    Returns:
      the class transform

  - ### transformingMethods

    static ClassTransformPREVIEW transformingMethods(MethodTransformPREVIEW xform)
    Create a class transform that transforms `MethodModel`PREVIEW elements with the supplied method transform.
    Parameters:
      `xform` - the method transform
    Returns:
      the class transform

  - ### transformingMethodBodies

    static ClassTransformPREVIEW transformingMethodBodies(Predicate<MethodModelPREVIEW> filter, CodeTransformPREVIEW xform)
    Create a class transform that transforms the `CodeAttribute`PREVIEW (method body) of `MethodModel`PREVIEW elements with the supplied code transform.
    Parameters:
      `filter` - a predicate that determines which methods to transform
      `xform` - the code transform
    Returns:
      the class transform

  - ### transformingMethodBodies

    static ClassTransformPREVIEW transformingMethodBodies(CodeTransformPREVIEW xform)
    Create a class transform that transforms the `CodeAttribute`PREVIEW (method body) of `MethodModel`PREVIEW elements with the supplied code transform.
    Parameters:
      `xform` - the code transform
    Returns:
      the class transform

  - ### transformingFields

    static ClassTransformPREVIEW transformingFields(FieldTransformPREVIEW xform)
    Create a class transform that transforms `FieldModel`PREVIEW elements with the supplied field transform.
    Parameters:
      `xform` - the field transform
    Returns:
      the class transform

  - ### andThen

    default ClassTransformPREVIEW andThen(ClassTransformPREVIEW t)
    Description copied from interface: `ClassFileTransform`
    Chain this transform with another; elements presented to the builder of this transform will become the input to the next transform.
    Specified by:
      `andThen` in interface `ClassFileTransformPREVIEW<ClassTransformPREVIEW,ClassElementPREVIEW,ClassBuilderPREVIEW>`
    Implementation Requirements:
      The default implementation returns this class transform chained with another class transform from the argument. Chaining of two transforms requires to involve a chained builder serving as a target builder for this transform and also as a source of elements for the downstream transform.
    Parameters:
      `t` - the downstream transform
    Returns:
      the chained transform

  - ### resolve

    default ClassFileTransform.ResolvedTransformPREVIEW<ClassElementPREVIEW> resolve(ClassBuilderPREVIEW builder)
    Description copied from interface: `ClassFileTransform`
    Bind a transform to a builder. If the transform is chained, intermediate builders are created for each chain link. If the transform is stateful (see, e.g., `ofStateful(Supplier)`), the supplier is invoked to get a fresh transform object.

    This method is a low-level method that should rarely be used by user code; most of the time, user code should prefer `ClassFileBuilder.transform(CompoundElement, ClassFileTransform)`PREVIEW, which resolves the transform and executes it on the current builder.

    Specified by:
      `resolve` in interface `ClassFileTransformPREVIEW<ClassTransformPREVIEW,ClassElementPREVIEW,ClassBuilderPREVIEW>`
    Implementation Requirements:
      The default implementation returns a resolved transform bound to the given class builder.
    Parameters:
      `builder` - the builder to bind to
    Returns:
      the bound result