类 BytecodeCompiler
- 直接已知子类:
SpelCompiler
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
-
嵌套类概要
嵌套类修饰符和类型类说明private static classA ChildClassLoader will load the generated compiled classes. -
字段概要
字段修饰符和类型字段说明protected BytecodeCompiler.ChildClassLoaderprivate static final intprivate static final Methodprivate static BiConsumer<String,byte[]> private static final ProtectionDomainprivate static final Throwable -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static <T> Class<T>compile(String className, Class<?> neighbor, ClassLoader loader, ProtectionDomain domain, byte[] classFile) Loads a class file by a given class loader.protected final <T> Class<T>Load a class.static Class<?>loadClass(String className, ClassLoader classLoader) private static CodeGenerationExceptionnewException(String className, Throwable ex) static voidsetGeneratedClassHandler(BiConsumer<String, byte[]> handler) static voidsetLoadedClassHandler(Consumer<Class<?>> handler)
-
字段详细资料
-
CLASSES_DEFINED_LIMIT
private static final int CLASSES_DEFINED_LIMIT -
classLoaderDefineClassMethod
-
THROWABLE
-
PROTECTION_DOMAIN
-
loadedClassHandler
-
generatedClassHandler
-
childClassLoader
-
-
构造器详细资料
-
BytecodeCompiler
-
-
方法详细资料
-
setGeneratedClassHandler
-
setLoadedClassHandler
-
loadClass
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 classclassFile- 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.MethodHandleto load a class. Otherwise, or ifneighboris null, this tries to usesun.misc.Unsafeto load a class. Then it tries to use aprotectedmethod injava.lang.ClassLoaderviaPrivilegedAction. 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-UNNAMEDmust 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 ifneighboris 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 -
newException
-