Class ClassDefinitionUtils


  • public final class ClassDefinitionUtils
    extends java.lang.Object
    Helper class for ProxyObjectInstantiator. We can see the details of a class specification here
    • Method Detail

      • defineClass

        public static <T> java.lang.Class<T> defineClass​(java.lang.String className,
                                                         byte[] b,
                                                         java.lang.Class<?> neighbor,
                                                         java.lang.ClassLoader loader)
                                                  throws java.lang.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:
        java.lang.Exception - whenever something goes wrong
      • readClass

        public static byte[] readClass​(java.lang.String className)
                                throws java.io.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:
        java.lang.IllegalArgumentException - if the class is longer than 2500 bytes
        java.io.IOException - if we fail to read the class
      • writeClass

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