Interface ClassFileTransform<C extends ClassFileTransform<C,E,B>, E extends ClassFileElement, B extends ClassFileBuilder<E,B>>
- Type Parameters:
C
- the transform typeE
- the element typeB
- the builder type
- All Known Subinterfaces:
ClassTransform
,CodeTransform
,FieldTransform
,MethodTransform
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.)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Transform an element by taking the appropriate actions on the builder.Chain this transform with another; elements presented to the builder of this transform will become the input to the next transform.default void
Take any final action during transformation of a classfile entity.default void
Take any preliminary action during transformation of a classfile entity.
-
Method Details
-
accept
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 toClassFileBuilder.with(ClassFileElement)
. If the element is to be dropped, no action is required.- Parameters:
builder
- the builder for the new entityelement
- the element
-
atEnd
Take any final action during transformation of a classfile entity. Called after all elements of the class are presented toaccept(ClassFileBuilder, ClassFileElement)
.- Implementation Requirements:
- The default implementation does nothing.
- Parameters:
builder
- the builder for the new entity
-
atStart
Take any preliminary action during transformation of a classfile entity. Called before any elements of the class are presented toaccept(ClassFileBuilder, ClassFileElement)
.- Implementation Requirements:
- The default implementation does nothing.
- Parameters:
builder
- the builder for the new entity
-
andThen
-