Class ClassDefinitionUtils


  • public final class ClassDefinitionUtils
    extends Object
    Helper class for ProxyObjectInstantiator. We can see the details of a class specification here
    Author:
    Henri Tremblay
    • Method Detail

      • defineClass

        public static <T> Class<T> defineClass​(String className,
                                               byte[] b,
                                               Class<?> neighbor,
                                               ClassLoader loader)
                                        throws Exception
        Define a class in the provided class loader from the array of bytes. Inspired by cglib ReflectUtils.defineClass
        Type Parameters:
        T - type of the class returned
        Parameters:
        className - class name in the format org.objenesis.MyClass
        b - bytes representing the class
        neighbor - a class in the same package as the loaded class
        loader - the class loader where the class will be loaded
        Returns:
        the newly loaded class
        Throws:
        Exception - whenever something goes wrong
      • readClass

        public static byte[] readClass​(String className)
                                throws IOException
        Read the bytes of a class from the classpath
        Parameters:
        className - full class name including the package
        Returns:
        the bytes representing the class
        Throws:
        IllegalArgumentException - if the class is longer than 2500 bytes
        IOException - if we fail to read the class
      • writeClass

        public static void writeClass​(String fileName,
                                      byte[] bytes)
                               throws IOException
        Write all class bytes to a file.
        Parameters:
        fileName - file where the bytes will be written
        bytes - bytes representing the class
        Throws:
        IOException - if we fail to write the class