org.springframework.instrument.classloading

Class ReflectiveLoadTimeWeaver

  • java.lang.Object
    • org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver
  • All Implemented Interfaces:
    LoadTimeWeaver


    public class ReflectiveLoadTimeWeaver
    extends java.lang.Object
    implements LoadTimeWeaver
    LoadTimeWeaver which uses reflection to delegate to an underlying ClassLoader with well-known transformation hooks. The underlying ClassLoader is expected to support the following weaving methods (as defined in the LoadTimeWeaver interface):
    • public void addTransformer(java.lang.instrument.ClassFileTransformer): for registering the given ClassFileTransformer on this ClassLoader
    • public ClassLoader getThrowawayClassLoader(): for obtaining a throwaway class loader for this ClassLoader (optional; ReflectiveLoadTimeWeaver will fall back to a SimpleThrowawayClassLoader if that method isn't available)

    Please note that the above methods must reside in a class that is publicly accessible, although the class itself does not have to be visible to the application's class loader.

    The reflective nature of this LoadTimeWeaver is particularly useful when the underlying ClassLoader implementation is loaded in a different class loader itself (such as the application server's class loader which is not visible to the web application). There is no direct API dependency between this LoadTimeWeaver adapter and the underlying ClassLoader, just a 'loose' method contract.

    This is the LoadTimeWeaver to use e.g. with the Resin application server version 3.1+.

    Since:
    2.0
    Author:
    Costin Leau, Juergen Hoeller
    See Also:
    addTransformer(java.lang.instrument.ClassFileTransformer), getThrowawayClassLoader(), SimpleThrowawayClassLoader
    • Constructor Summary

      Constructors 
      Constructor and Description
      ReflectiveLoadTimeWeaver()
      Create a new ReflectiveLoadTimeWeaver for the current context class loader, which needs to support the required weaving methods.
      ReflectiveLoadTimeWeaver(java.lang.ClassLoader classLoader)
      Create a new SimpleLoadTimeWeaver for the given class loader.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void addTransformer(java.lang.instrument.ClassFileTransformer transformer)
      Add a ClassFileTransformer to be applied by this LoadTimeWeaver.
      java.lang.ClassLoader getInstrumentableClassLoader()
      Return a ClassLoader that supports instrumentation through AspectJ-style load-time weaving based on user-defined ClassFileTransformers.
      java.lang.ClassLoader getThrowawayClassLoader()
      Return a throwaway ClassLoader, enabling classes to be loaded and inspected without affecting the parent ClassLoader.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ReflectiveLoadTimeWeaver

        public ReflectiveLoadTimeWeaver()
        Create a new ReflectiveLoadTimeWeaver for the current context class loader, which needs to support the required weaving methods.
      • ReflectiveLoadTimeWeaver

        public ReflectiveLoadTimeWeaver(@Nullable
                                        java.lang.ClassLoader classLoader)
        Create a new SimpleLoadTimeWeaver for the given class loader.
        Parameters:
        classLoader - the ClassLoader to delegate to for weaving (must support the required weaving methods).
        Throws:
        java.lang.IllegalStateException - if the supplied ClassLoader does not support the required weaving methods
    • Method Detail

      • addTransformer

        public void addTransformer(java.lang.instrument.ClassFileTransformer transformer)
        Description copied from interface: LoadTimeWeaver
        Add a ClassFileTransformer to be applied by this LoadTimeWeaver.
        Specified by:
        addTransformer in interface LoadTimeWeaver
        Parameters:
        transformer - the ClassFileTransformer to add
      • getInstrumentableClassLoader

        public java.lang.ClassLoader getInstrumentableClassLoader()
        Description copied from interface: LoadTimeWeaver
        Return a ClassLoader that supports instrumentation through AspectJ-style load-time weaving based on user-defined ClassFileTransformers.

        May be the current ClassLoader, or a ClassLoader created by this LoadTimeWeaver instance.

        Specified by:
        getInstrumentableClassLoader in interface LoadTimeWeaver
        Returns:
        the ClassLoader which will expose instrumented classes according to the registered transformers
      • getThrowawayClassLoader

        public java.lang.ClassLoader getThrowawayClassLoader()
        Description copied from interface: LoadTimeWeaver
        Return a throwaway ClassLoader, enabling classes to be loaded and inspected without affecting the parent ClassLoader.

        Should not return the same instance of the ClassLoader returned from an invocation of LoadTimeWeaver.getInstrumentableClassLoader().

        Specified by:
        getThrowawayClassLoader in interface LoadTimeWeaver
        Returns:
        a temporary throwaway ClassLoader; should return a new instance for each call, with no existing state