Class Transformer

  • All Implemented Interfaces:
    java.util.function.Function<byte[],​byte[]>, UnaryFunction<byte[],​byte[]>

    public class Transformer
    extends java.lang.Object
    implements UnaryFunction<byte[],​byte[]>
    ClassFile enhancer for the tracing facility. This modifies the bytecode for an applicable class, then returns the updated bytecode. Makes extensive use of the ASM library. This is split into two parts. The first part modifies the schema of the class as follows:
    • Adds static fields as required for the SynchronizedHolder<MethodMonitor> instances.
    • Modifies the static initializer to set up the new fields, and register the class with the MethodMonitorRegistry. This also constructs the list of method names, which is needed by the second part.
    • Re-writes all @InfoMethod methods to take two extra parameters at the end of their argument lists.
    • Re-writes all calls to @InfoMethod methods to supply the two extra parameters to all calls.
    • Checks that @InfoMethod methods (which must be private) are only called from MM annotated methods.

    The second part modifies the MM annotated methods as follows:

    • Adds a preamble to set up some local variables, and to call the MethodMonitor.enter method when active.
    • Adds a finally block at the end of the method that handles calling MethodMonitor.exit whenever an exception is thrown or propagated from the body of the method.
    • Modifies all exit point in the method as follows:
      • If the exit point is a return, call MethodMonitor.exit before the return.
      • If the exit point is a throw, call MethodMonitor.exception before the throw.

    Note that the second part could be run in a ClassFileTransformer or ClassLoader if desired, since this design enhances the class files in place for the first part.