org.springframework.core

Class OverridingClassLoader

  • Direct Known Subclasses:
    SimpleInstrumentableClassLoader, SimpleThrowawayClassLoader


    public class OverridingClassLoader
    extends DecoratingClassLoader
    ClassLoader that does not always delegate to the parent loader as normal class loaders do. This enables, for example, instrumentation to be forced in the overriding ClassLoader, or a "throwaway" class loading behavior where selected application classes are temporarily loaded in the overriding ClassLoader for introspection purposes before eventually loading an instrumented version of the class in the given parent ClassLoader.
    Since:
    2.0.1
    Author:
    Rod Johnson, Juergen Hoeller
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      static java.lang.String[] DEFAULT_EXCLUDED_PACKAGES
      Packages that are excluded by default.
    • Constructor Summary

      Constructors 
      Constructor and Description
      OverridingClassLoader(java.lang.ClassLoader parent)
      Create a new OverridingClassLoader for the given ClassLoader.
      OverridingClassLoader(java.lang.ClassLoader parent, java.lang.ClassLoader overrideDelegate)
      Create a new OverridingClassLoader for the given ClassLoader.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      protected boolean isEligibleForOverriding(java.lang.String className)
      Determine whether the specified class is eligible for overriding by this class loader.
      protected byte[] loadBytesForClass(java.lang.String name)
      Load the defining bytes for the given class, to be turned into a Class object through a ClassLoader.defineClass(byte[], int, int) call.
      java.lang.Class<?> loadClass(java.lang.String name) 
      protected java.lang.Class<?> loadClass(java.lang.String name, boolean resolve) 
      protected java.lang.Class<?> loadClassForOverriding(java.lang.String name)
      Load the specified class for overriding purposes in this ClassLoader.
      protected java.io.InputStream openStreamForClass(java.lang.String name)
      Open an InputStream for the specified class.
      protected byte[] transformIfNecessary(java.lang.String name, byte[] bytes)
      Transformation hook to be implemented by subclasses.
      • Methods inherited from class java.lang.ClassLoader

        clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_EXCLUDED_PACKAGES

        public static final java.lang.String[] DEFAULT_EXCLUDED_PACKAGES
        Packages that are excluded by default.
    • Constructor Detail

      • OverridingClassLoader

        public OverridingClassLoader(@Nullable
                                     java.lang.ClassLoader parent)
        Create a new OverridingClassLoader for the given ClassLoader.
        Parameters:
        parent - the ClassLoader to build an overriding ClassLoader for
      • OverridingClassLoader

        public OverridingClassLoader(@Nullable
                                     java.lang.ClassLoader parent,
                                     @Nullable
                                     java.lang.ClassLoader overrideDelegate)
        Create a new OverridingClassLoader for the given ClassLoader.
        Parameters:
        parent - the ClassLoader to build an overriding ClassLoader for
        overrideDelegate - the ClassLoader to delegate to for overriding
        Since:
        4.3
    • Method Detail

      • loadClass

        public java.lang.Class<?> loadClass(java.lang.String name)
                                     throws java.lang.ClassNotFoundException
        Overrides:
        loadClass in class java.lang.ClassLoader
        Throws:
        java.lang.ClassNotFoundException
      • loadClass

        protected java.lang.Class<?> loadClass(java.lang.String name,
                                               boolean resolve)
                                        throws java.lang.ClassNotFoundException
        Overrides:
        loadClass in class java.lang.ClassLoader
        Throws:
        java.lang.ClassNotFoundException
      • isEligibleForOverriding

        protected boolean isEligibleForOverriding(java.lang.String className)
        Determine whether the specified class is eligible for overriding by this class loader.
        Parameters:
        className - the class name to check
        Returns:
        whether the specified class is eligible
        See Also:
        DecoratingClassLoader.isExcluded(java.lang.String)
      • loadClassForOverriding

        @Nullable
        protected java.lang.Class<?> loadClassForOverriding(java.lang.String name)
                                                               throws java.lang.ClassNotFoundException
        Load the specified class for overriding purposes in this ClassLoader.

        The default implementation delegates to ClassLoader.findLoadedClass(java.lang.String), loadBytesForClass(java.lang.String) and ClassLoader.defineClass(byte[], int, int).

        Parameters:
        name - the name of the class
        Returns:
        the Class object, or null if no class defined for that name
        Throws:
        java.lang.ClassNotFoundException - if the class for the given name couldn't be loaded
      • loadBytesForClass

        @Nullable
        protected byte[] loadBytesForClass(java.lang.String name)
                                              throws java.lang.ClassNotFoundException
        Load the defining bytes for the given class, to be turned into a Class object through a ClassLoader.defineClass(byte[], int, int) call.

        The default implementation delegates to openStreamForClass(java.lang.String) and transformIfNecessary(java.lang.String, byte[]).

        Parameters:
        name - the name of the class
        Returns:
        the byte content (with transformers already applied), or null if no class defined for that name
        Throws:
        java.lang.ClassNotFoundException - if the class for the given name couldn't be loaded
      • openStreamForClass

        @Nullable
        protected java.io.InputStream openStreamForClass(java.lang.String name)
        Open an InputStream for the specified class.

        The default implementation loads a standard class file through the parent ClassLoader's getResourceAsStream method.

        Parameters:
        name - the name of the class
        Returns:
        the InputStream containing the byte code for the specified class
      • transformIfNecessary

        protected byte[] transformIfNecessary(java.lang.String name,
                                              byte[] bytes)
        Transformation hook to be implemented by subclasses.

        The default implementation simply returns the given bytes as-is.

        Parameters:
        name - the fully-qualified name of the class being transformed
        bytes - the raw bytes of the class
        Returns:
        the transformed bytes (never null; same as the input bytes if the transformation produced no changes)