类 DefineClassHelper
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
-
字段概要
字段修饰符和类型字段说明private static final Methodprivate static BiConsumer<String,byte[]> private static final ProtectionDomainprivate static final Throwable -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static Class<?>defineClass(Class<?> neighbor, byte[] bcode) Loads a class file byjava.lang.invoke.MethodHandles.Lookup.static Class<?>defineClass(String className, byte[] b, ClassLoader loader) static Class<?>defineClass(String className, byte[] b, ClassLoader loader, ProtectionDomain protectionDomain) static Class<?>defineClass(String className, Class<?> neighbor, ClassLoader loader, ProtectionDomain domain, byte[] classFile) Loads a class file by a given class loader.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)
-
字段详细资料
-
classLoaderDefineClassMethod
-
THROWABLE
-
PROTECTION_DOMAIN
-
loadedClassHandler
-
generatedClassHandler
-
-
构造器详细资料
-
DefineClassHelper
public DefineClassHelper()
-
-
方法详细资料
-
setGeneratedClassHandler
-
setLoadedClassHandler
-
defineClass
Loads a class file byjava.lang.invoke.MethodHandles.Lookup. It is obtained by usingneighbor.- 参数:
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 -
defineClass
public static Class<?> defineClass(String className, byte[] b, ClassLoader loader, ProtectionDomain protectionDomain) throws 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.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
-