类 BytecodeCompiler

java.lang.Object
cn.taketoday.bytecode.BytecodeCompiler
直接已知子类:
SpelCompiler

public abstract class BytecodeCompiler 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 2021/11/29 11:23
作者:
Harry Yang
  • 字段详细资料

  • 构造器详细资料

  • 方法详细资料

    • setGeneratedClassHandler

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

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

      protected final <T> Class<T> loadClass(String name, byte[] classFile)
      Load a class. Makes sure the classloaders aren't used too much because they anchor compiled classes in memory and prevent GC. If you have expressions continually recompiling over time then by replacing the classloader periodically at least some of the older variants can be garbage collected.
      参数:
      name - the name of the class
      classFile - the bytecode for the class
      返回:
      the Class object for the compiled expression
    • compile

      public static <T> Class<T> compile(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)