类 DefineClassHelper

java.lang.Object
cn.taketoday.util.DefineClassHelper

public class DefineClassHelper extends Object
Helper class for invoking ClassLoader.defineClass(String, byte[], int, int).

This first tries to use java.lang.invoke.MethodHandle to load a class. Otherwise, or if neighbor is null, this tries to use sun.misc.Unsafe to load a class. Then it tries to use a protected method in java.lang.ClassLoader via PrivilegedAction. Since the latter approach is not available any longer by default in Java 9 or later, the JVM argument --add-opens java.base/java.lang=ALL-UNNAMED must be given to the JVM.

 --add-opens java.base/java.lang=ALL-UNNAMED
 --add-opens java.base/java.util=ALL-UNNAMED
 --add-opens java.base/java.lang.reflect=ALL-UNNAMED
 
从以下版本开始:
4.0
作者:
TODAY 2021/11/8 9:44
  • 字段详细资料

  • 构造器详细资料

    • DefineClassHelper

      public DefineClassHelper()
  • 方法详细资料

    • setGeneratedClassHandler

      public static void setGeneratedClassHandler(@Nullable BiConsumer<String,byte[]> handler)
    • setLoadedClassHandler

      public static void setLoadedClassHandler(@Nullable Consumer<Class<?>> handler)
    • defineClass

      public static Class<?> defineClass(Class<?> neighbor, byte[] bcode) throws ReflectionException
      Loads a class file by java.lang.invoke.MethodHandles.Lookup. It is obtained by using neighbor.
      参数:
      neighbor - a class belonging to the same package that the loaded class belogns to.
      bcode - the bytecode.
      抛出:
      ReflectionException
    • defineClass

      public static Class<?> defineClass(String className, byte[] b, ClassLoader loader) throws CodeGenerationException
      抛出:
      CodeGenerationException
    • defineClass

      public static Class<?> defineClass(String className, byte[] b, ClassLoader loader, ProtectionDomain protectionDomain) throws CodeGenerationException
      抛出:
      CodeGenerationException
    • defineClass

      public static Class<?> defineClass(String className, @Nullable Class<?> neighbor, ClassLoader loader, @Nullable ProtectionDomain domain, byte[] classFile) throws CodeGenerationException
      Loads a class file by a given class loader.

      This first tries to use java.lang.invoke.MethodHandle to load a class. Otherwise, or if neighbor is null, this tries to use sun.misc.Unsafe to load a class. Then it tries to use a protected method in java.lang.ClassLoader via PrivilegedAction. Since the latter approach is not available any longer by default in Java 9 or later, the JVM argument --add-opens java.base/java.lang=ALL-UNNAMED must be given to the JVM.

       --add-opens java.base/java.lang=ALL-UNNAMED
       --add-opens java.base/java.util=ALL-UNNAMED
       --add-opens java.base/java.lang.reflect=ALL-UNNAMED
       
      参数:
      className - the name of the loaded class.
      neighbor - the class contained in the same package as the loaded class.
      loader - the class loader. It can be null if neighbor is not null and the JVM is Java 11 or later.
      domain - if it is null, a default domain is used.
      classFile - the bytecode for the loaded class.
      抛出:
      CodeGenerationException
    • loadClass

      public static Class<?> loadClass(String className, ClassLoader classLoader) throws ClassNotFoundException
      抛出:
      ClassNotFoundException
    • newException

      private static CodeGenerationException newException(String className, Throwable ex)