ClassDefinitionUtils
Date: 2015-09-17

org.objenesis.instantiator.basic

## Class ClassDefinitionUtils

- java.lang.Object
- - org.objenesis.instantiator.basic.ClassDefinitionUtils

-     public final class ClassDefinitionUtils
      extends java.lang.Object

  Helper class for ProxyObjectInstantiator. We can see the details of a class specification here

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `ACC_ABSTRACT`
    `static int` | `ACC_ANNOTATION`
    `static int` | `ACC_ENUM`
    `static int` | `ACC_FINAL`
    `static int` | `ACC_INTERFACE`
    `static int` | `ACC_PUBLIC`
    `static int` | `ACC_SUPER`
    `static int` | `ACC_SYNTHETIC`
    `static int` | `CONSTANT_Class`
    `static int` | `CONSTANT_Double`
    `static int` | `CONSTANT_Fieldref`
    `static int` | `CONSTANT_Float`
    `static int` | `CONSTANT_Integer`
    `static int` | `CONSTANT_InterfaceMethodref`
    `static int` | `CONSTANT_InvokeDynamic`
    `static int` | `CONSTANT_Long`
    `static int` | `CONSTANT_MethodHandle`
    `static int` | `CONSTANT_Methodref`
    `static int` | `CONSTANT_MethodType`
    `static int` | `CONSTANT_NameAndType`
    `static int` | `CONSTANT_String`
    `static int` | `CONSTANT_Utf8`
    `static byte[]` | `MAGIC`
    `static byte` | `OPS_aload_0`
    `static byte` | `OPS_areturn`
    `static byte` | `OPS_dup`
    `static byte` | `OPS_invokespecial`
    `static byte` | `OPS_new`
    `static byte` | `OPS_return`
    `static byte[]` | `VERSION`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.lang.String` | `classNameToInternalClassName(java.lang.String className)` Will convert a class name to its name in the class definition format (e.g `org.objenesis.EmptyClass` becomes `org/objenesis/EmptyClass`)
    `static java.lang.String` | `classNameToResource(java.lang.String className)` Will convert a class name to its class loader resource name (e.g `org.objenesis.EmptyClass` becomes `org/objenesis/EmptyClass.class`)
    `static <T> java.lang.Class<T>` | `defineClass(java.lang.String className, byte[] b, java.lang.ClassLoader loader)` Define a class in the provided class loader from the array of bytes.
    `static <T> java.lang.Class<T>` | `getExistingClass(java.lang.ClassLoader classLoader, java.lang.String className)` Check if this class already exists in the class loader and return it if it does
    `static byte[]` | `readClass(java.lang.String className)` Read the bytes of a class from the classpath
    `static void` | `writeClass(java.lang.String fileName, byte[] bytes)` Write all class bytes to a file.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### OPS_aload_0

          public static final byte OPS_aload_0

      See Also:
        Constant Field Values

    - #### OPS_invokespecial

          public static final byte OPS_invokespecial

      See Also:
        Constant Field Values

    - #### OPS_return

          public static final byte OPS_return

      See Also:
        Constant Field Values

    - #### OPS_new

          public static final byte OPS_new

      See Also:
        Constant Field Values

    - #### OPS_dup

          public static final byte OPS_dup

      See Also:
        Constant Field Values

    - #### OPS_areturn

          public static final byte OPS_areturn

      See Also:
        Constant Field Values

    - #### CONSTANT_Utf8

          public static final int CONSTANT_Utf8

      See Also:
        Constant Field Values

    - #### CONSTANT_Integer

          public static final int CONSTANT_Integer

      See Also:
        Constant Field Values

    - #### CONSTANT_Float

          public static final int CONSTANT_Float

      See Also:
        Constant Field Values

    - #### CONSTANT_Long

          public static final int CONSTANT_Long

      See Also:
        Constant Field Values

    - #### CONSTANT_Double

          public static final int CONSTANT_Double

      See Also:
        Constant Field Values

    - #### CONSTANT_Class

          public static final int CONSTANT_Class

      See Also:
        Constant Field Values

    - #### CONSTANT_String

          public static final int CONSTANT_String

      See Also:
        Constant Field Values

    - #### CONSTANT_Fieldref

          public static final int CONSTANT_Fieldref

      See Also:
        Constant Field Values

    - #### CONSTANT_Methodref

          public static final int CONSTANT_Methodref

      See Also:
        Constant Field Values

    - #### CONSTANT_InterfaceMethodref

          public static final int CONSTANT_InterfaceMethodref

      See Also:
        Constant Field Values

    - #### CONSTANT_NameAndType

          public static final int CONSTANT_NameAndType

      See Also:
        Constant Field Values

    - #### CONSTANT_MethodHandle

          public static final int CONSTANT_MethodHandle

      See Also:
        Constant Field Values

    - #### CONSTANT_MethodType

          public static final int CONSTANT_MethodType

      See Also:
        Constant Field Values

    - #### CONSTANT_InvokeDynamic

          public static final int CONSTANT_InvokeDynamic

      See Also:
        Constant Field Values

    - #### ACC_PUBLIC

          public static final int ACC_PUBLIC

      See Also:
        Constant Field Values

    - #### ACC_FINAL

          public static final int ACC_FINAL

      See Also:
        Constant Field Values

    - #### ACC_SUPER

          public static final int ACC_SUPER

      See Also:
        Constant Field Values

    - #### ACC_INTERFACE

          public static final int ACC_INTERFACE

      See Also:
        Constant Field Values

    - #### ACC_ABSTRACT

          public static final int ACC_ABSTRACT

      See Also:
        Constant Field Values

    - #### ACC_SYNTHETIC

          public static final int ACC_SYNTHETIC

      See Also:
        Constant Field Values

    - #### ACC_ANNOTATION

          public static final int ACC_ANNOTATION

      See Also:
        Constant Field Values

    - #### ACC_ENUM

          public static final int ACC_ENUM

      See Also:
        Constant Field Values

    - #### MAGIC

          public static final byte[] MAGIC

    - #### VERSION

          public static final byte[] VERSION

  - ### Method Detail

    - #### defineClass

          public static <T> java.lang.Class<T> defineClass(java.lang.String className,
                                           byte[] b,
                                           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
        `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

    - #### classNameToInternalClassName

          public static java.lang.String classNameToInternalClassName(java.lang.String className)

      Will convert a class name to its name in the class definition format (e.g `org.objenesis.EmptyClass` becomes `org/objenesis/EmptyClass`)
      Parameters:
        `className` - full class name including the package
      Returns:
        the internal name

    - #### classNameToResource

          public static java.lang.String classNameToResource(java.lang.String className)

      Will convert a class name to its class loader resource name (e.g `org.objenesis.EmptyClass` becomes `org/objenesis/EmptyClass.class`)
      Parameters:
        `className` - full class name including the package
      Returns:
        the resource name

    - #### getExistingClass

          public static <T> java.lang.Class<T> getExistingClass(java.lang.ClassLoader classLoader,
                                                java.lang.String className)

      Check if this class already exists in the class loader and return it if it does
      Type Parameters:
        `T` - type of the class returned
      Parameters:
        `classLoader` - Class loader where to search the class
        `className` - Class name with full path
      Returns:
        the class if it already exists or null