Class MyClassLoader


  • public class MyClassLoader
    extends java.lang.ClassLoader
    Class loader that is needed to load generated classes.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean _cfgUseParentLoader
      Flag that determines if we should first try to load new class using parent class loader or not; this may be done to try to force access to protected/package-access properties.
      private static java.util.concurrent.ConcurrentHashMap<java.lang.String,​java.lang.Object> parentParallelLockMap  
      private static java.nio.charset.Charset UTF8  
    • Constructor Summary

      Constructors 
      Constructor Description
      MyClassLoader​(java.lang.ClassLoader parent, boolean tryToUseParent)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean canAddClassInPackageOf​(java.lang.Class<?> cls)
      Helper method called to check whether it is acceptable to create a new class in package that given class is part of.
      (package private) java.lang.Class<?> defineClassOnParent​(java.lang.ClassLoader parentClassLoader, java.lang.String className, byte[] byteCode, int offset, int length)  
      private java.lang.Class<?> findLoadedClassOnParent​(java.lang.ClassLoader parentClassLoader, java.lang.String className)  
      private java.lang.Object getParentClassLoadingLock​(java.lang.ClassLoader parentClassLoader, java.lang.String className)
      Get the class loading lock for the parent class loader for loading the named class.
      java.lang.Class<?> loadAndResolve​(ClassName className, byte[] byteCode)  
      private java.lang.Class<?> loadAndResolveUsingParentClassloader​(ClassName className, byte[] byteCode)
      Attempt to load (and resolve) the class using the parent class loader (if it is configured and present).
      static int replaceName​(byte[] byteCode, java.lang.String from, java.lang.String to)  
      private void resolveClassOnParent​(java.lang.ClassLoader parentClassLoader, java.lang.Class<?> clazz)  
      • Methods inherited from class java.lang.ClassLoader

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

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

      • UTF8

        private static final java.nio.charset.Charset UTF8
      • parentParallelLockMap

        private static final java.util.concurrent.ConcurrentHashMap<java.lang.String,​java.lang.Object> parentParallelLockMap
      • _cfgUseParentLoader

        protected final boolean _cfgUseParentLoader
        Flag that determines if we should first try to load new class using parent class loader or not; this may be done to try to force access to protected/package-access properties.
    • Constructor Detail

      • MyClassLoader

        public MyClassLoader​(java.lang.ClassLoader parent,
                             boolean tryToUseParent)
    • Method Detail

      • canAddClassInPackageOf

        public static boolean canAddClassInPackageOf​(java.lang.Class<?> cls)
        Helper method called to check whether it is acceptable to create a new class in package that given class is part of. This is used to prevent certain class of failures, related to access limitations: for example, we can not add classes in sealed packages, or core Java packages (java.*).
        Since:
        2.2.1
      • loadAndResolve

        public java.lang.Class<?> loadAndResolve​(ClassName className,
                                                 byte[] byteCode)
                                          throws java.lang.IllegalArgumentException
        Parameters:
        className - Interface or abstract class that class to load should extend or implement
        Throws:
        java.lang.IllegalArgumentException
      • loadAndResolveUsingParentClassloader

        private java.lang.Class<?> loadAndResolveUsingParentClassloader​(ClassName className,
                                                                        byte[] byteCode)
        Attempt to load (and resolve) the class using the parent class loader (if it is configured and present). This method will return null if the parent classloader is not configured or cannot be retrieved.
        Parameters:
        className - Interface or abstract class that class to load should extend or implement
        byteCode - the generated bytecode for the class to load
        Returns:
        the loaded class, or null if the class could not be loaded on the parent classloader.
      • getParentClassLoadingLock

        private java.lang.Object getParentClassLoadingLock​(java.lang.ClassLoader parentClassLoader,
                                                           java.lang.String className)
        Get the class loading lock for the parent class loader for loading the named class. This is effectively the same implementation as ClassLoader#getClassLoadingLock, but using our static parentParallelLockMap and keying off of the parent ClassLoader instance as well as the class name to load.
        Parameters:
        parentClassLoader - The parent ClassLoader
        className - The name of the to-be-loaded class
      • findLoadedClassOnParent

        private java.lang.Class<?> findLoadedClassOnParent​(java.lang.ClassLoader parentClassLoader,
                                                           java.lang.String className)
      • defineClassOnParent

        java.lang.Class<?> defineClassOnParent​(java.lang.ClassLoader parentClassLoader,
                                               java.lang.String className,
                                               byte[] byteCode,
                                               int offset,
                                               int length)
      • resolveClassOnParent

        private void resolveClassOnParent​(java.lang.ClassLoader parentClassLoader,
                                          java.lang.Class<?> clazz)
      • replaceName

        public static int replaceName​(byte[] byteCode,
                                      java.lang.String from,
                                      java.lang.String to)