Interface ClassFileTransform<C extends ClassFileTransform<C,E,B>, E extends ClassFileElement, B extends ClassFileBuilder<E,B>>

Type Parameters:
C - the transform type
E - the element type
B - the builder type
All Known Subinterfaces:
ClassTransform, CodeTransform, FieldTransform, MethodTransform

public sealed interface ClassFileTransform<C extends ClassFileTransform<C,E,B>, E extends ClassFileElement, B extends ClassFileBuilder<E,B>> permits ClassTransform, FieldTransform, MethodTransform, CodeTransform
A transformation on streams of elements. Transforms are used during transformation of classfile entities; a transform is provided to a method like ClassFile.transformClass(ClassModel, ClassTransform), and the elements of the class, along with a builder, are presented to the transform.

The subtypes of ClassFileTransform (e.g., ClassTransform) are functional interfaces that accept an element and a corresponding builder. Since any element can be reproduced on the builder via ClassFileBuilder.with(ClassFileElement), a transform can easily leave elements in place, remove them, replace them, or augment them with other elements. This enables localized transforms to be represented concisely.

Transforms also have an atEnd(ClassFileBuilder) method, for which the default implementation does nothing, so that a transform can perform additional building after the stream of elements is exhausted.

Transforms can be chained together via the andThen(ClassFileTransform) method, so that the output of one becomes the input to another. This allows smaller units of transformation to be captured and reused.

Some transforms are stateful; for example, a transform that injects an annotation on a class may watch for the RuntimeVisibleAnnotationsAttribute element and transform it if found, but if it is not found, will generate a RuntimeVisibleAnnotationsAttribute element containing the injected annotation from the atEnd(ClassFileBuilder) handler. To do this, the transform must accumulate some state during the traversal so that the end handler knows what to do. If such a transform is to be reused, its state must be reset for each traversal; this will happen automatically if the transform is created with ClassTransform.ofStateful(Supplier) (or corresponding methods for other classfile locations.)

Sealed Class Hierarchy Graph:
Sealed class hierarchy graph for ClassFileTransformSealed class hierarchy graph for ClassFileTransform
Since:
24
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    accept(B builder, E element)
    Transform an element by taking the appropriate actions on the builder.
    andThen(C next)
    Chain this transform with another; elements presented to the builder of this transform will become the input to the next transform.
    default void
    atEnd(B builder)
    Take any final action during transformation of a classfile entity.
    default void
    atStart(B builder)
    Take any preliminary action during transformation of a classfile entity.
  • Method Details

    • accept

      void accept(B builder, E element)
      Transform an element by taking the appropriate actions on the builder. Used when transforming a classfile entity (class, method, field, method body.) If no transformation is desired, the element can be presented to ClassFileBuilder.with(ClassFileElement). If the element is to be dropped, no action is required.
      Parameters:
      builder - the builder for the new entity
      element - the element
    • atEnd

      default void atEnd(B builder)
      Take any final action during transformation of a classfile entity. Called after all elements of the class are presented to accept(ClassFileBuilder, ClassFileElement).
      Implementation Requirements:
      The default implementation does nothing.
      Parameters:
      builder - the builder for the new entity
    • atStart

      default void atStart(B builder)
      Take any preliminary action during transformation of a classfile entity. Called before any elements of the class are presented to accept(ClassFileBuilder, ClassFileElement).
      Implementation Requirements:
      The default implementation does nothing.
      Parameters:
      builder - the builder for the new entity
    • andThen

      C andThen(C next)
      Chain this transform with another; elements presented to the builder of this transform will become the input to the next transform.
      Parameters:
      next - the downstream transform
      Returns:
      the chained transform