Interface FieldValueTransformer


public interface FieldValueTransformer
A transformer for a field value that can be registered using Feature.BeforeAnalysisAccess.registerFieldValueTransformer(java.lang.reflect.Field, org.graalvm.nativeimage.hosted.FieldValueTransformer).

At image build time, the field value transformer provides the value of the field for the image heap. Without a transformer, the value of the field in the image heap is the same as the hosted value in the image generator. A field value transformer allows to intercept the value. A field value transformer can, for example, reset a field to the default null/0 value, replace a filled collection with a new empty collection, or provide any kind of new value for a field.

Only one field value transformer can be registered for each field.

A field value transformer can be registered for fields of classes that are initialized at image run time. That allows constant folding of final fields even though the declaring class is not initialized at image build time.

A field value transformer must be registered before the field is seen as reachable by the static analysis. It is generally safe to register a transformer in Feature.beforeAnalysis(org.graalvm.nativeimage.hosted.Feature.BeforeAnalysisAccess) before the static analysis is started, in a type reachability handler for the declaring class of the field, or a subtype reachability handler for a super-type of the declaring class of the field.

Since:
22.3
  • Method Summary

    Modifier and Type
    Method
    Description
    transform(Object receiver, Object originalValue)
    Transforms the field value for the provided receiver.
  • Method Details

    • transform

      Object transform(Object receiver, Object originalValue)
      Transforms the field value for the provided receiver. The receiver is null for static fields. The original value of the field, i.e., the hosted value of the field in the image generator, is also provided as an argument.

      The type of the returned object must be assignable to the declared type of the field. If the field has a primitive type, the return value must be a boxed value, and must not be null.

      Since:
      22.3