Class ClassGenerator

java.lang.Object
com.sun.ejb.codegen.ClassGenerator

public final class ClassGenerator extends Object
This class serves to generate classes, because ...

MethodHandles.Lookup class restricts the generated class to use an already existing class as a source of the package and ProtectionDomain.

Proxy.newProxyInstance(ClassLoader, Class[], java.lang.reflect.InvocationHandler) has another requirements, ie. all referred classes must be loadable by used classloader.

Author:
David Matejcek
  • Method Details

    • defineClass

      public static Class<?> defineClass(ClassLoader loader, Class<?> anchorClass, String targetPackageName, String className, byte[] classData)
      Decides which method is suitable to define the new class and then uses it.
      Parameters:
      loader - the classloader instance used to generate the class
      anchorClass - the class used as an "orientation" class. See the MethodHandles.Lookup class for more info.
      targetPackageName - the name is used in decision logic; if the anchor class is from a different package, the MethodHandles.Lookup's method is not usable.
      className - expected binary name or null
      classData - the valid bytes that make up the class data.
      Returns:
      the new generated class
      Throws:
      ClassGenerator.ClassDefinitionException - invalid data, missing dependency, or another error related to the class generation
    • defineClass

      public static Class<?> defineClass(Class<?> anchorClass, String className, byte[] classData)
      Calls the MethodHandles.Lookup's defineClass method to create a new class
      Parameters:
      anchorClass - the class used as an "orientation" class. See the MethodHandles.Lookup class for more info.
      className - expected binary name or null
      classData - the valid bytes that make up the class data.
      Returns:
      the new generated class
      Throws:
      ClassGenerator.ClassDefinitionException - invalid data, missing dependency, or another error related to the class generation
    • defineClass

      public static Class<?> defineClass(ClassLoader loader, String className, byte[] classData) throws ClassGenerator.ClassDefinitionException
      Calls the ClassLoader's protected defineClass method to create a new class
      Parameters:
      loader - the classloader instance used to generate the class
      className - expected binary name or null
      classData - the valid bytes that make up the class data.
      Returns:
      the new generated class
      Throws:
      ClassGenerator.ClassDefinitionException - invalid data, missing dependency, or another error related to the class generation
    • defineClass

      public static Class<?> defineClass(ClassLoader loader, String className, byte[] classData, int offset, int length) throws ClassGenerator.ClassDefinitionException
      Calls the ClassLoader's protected defineClass method to create a new class
      Parameters:
      loader - the classloader instance used to generate the class
      className - expected binary name or null
      classData - the valid bytes that make up the class data.
      offset - The start offset in b of the class data
      length - The length of the class data
      Returns:
      the new generated class
      Throws:
      ClassGenerator.ClassDefinitionException - invalid data, missing dependency, or another error related to the class generation
    • defineClass

      public static Class<?> defineClass(ClassLoader loader, String className, byte[] classData, ProtectionDomain protectionDomain) throws ClassGenerator.ClassDefinitionException
      Calls the ClassLoader's protected defineClass method to create a new class
      Parameters:
      loader - the classloader instance used to generate the class
      className - expected binary name or null
      classData - the valid bytes that make up the class data.
      protectionDomain - The ProtectionDomain of the class
      Returns:
      the new generated class
      Throws:
      ClassGenerator.ClassDefinitionException - invalid data, missing dependency, or another error related to the class generation
    • defineClass

      public static Class<?> defineClass(ClassLoader loader, String className, byte[] classData, int offset, int length, ProtectionDomain protectionDomain) throws ClassGenerator.ClassDefinitionException
      Calls the ClassLoader's protected defineClass method to create a new class
      Parameters:
      loader - the classloader instance used to generate the class
      className - expected binary name or null
      classData - the valid bytes that make up the class data.
      offset - The start offset in b of the class data
      length - The length of the class data
      protectionDomain - The ProtectionDomain of the class
      Returns:
      the new generated class
      Throws:
      ClassGenerator.ClassDefinitionException - invalid data, missing dependency, or another error related to the class generation