- java.lang.Object
-
- cn.mcres.karlatemp.mxlib.tools.Toolkit.Reflection
-
-
字段概要
字段 修饰符和类型 字段 说明 static int
LOAD_CLASS_CALLER_CLASSLOADER
static int
LOAD_CLASS_THREAD_CONTENT
-
构造器概要
构造器 构造器 说明 Reflection()
-
方法概要
修饰符和类型 方法 说明 static <T> T
allocObject(Class<T> type)
static <T> T
clone(T object)
Clone a object.static void
copyValues(Object from, Object to)
static Class<?>
defineClass(ClassLoader loader, @org.jetbrains.annotations.NotNull byte[] code, ProtectionDomain protectionDomain)
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain.static Class<?>
defineClass(ClassLoader loader, @NotNull org.objectweb.asm.ClassWriter code, ProtectionDomain protectionDomain)
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain.static Class<?>
defineClass(ClassLoader loader, String name, @org.jetbrains.annotations.NotNull byte[] b, int off, int len, ProtectionDomain protectionDomain)
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain.static Class<?>
defineClass(ClassLoader loader, String name, @org.jetbrains.annotations.NotNull byte[] code, ProtectionDomain protectionDomain)
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain.static Class<?>
forName(String name, boolean initialize, ClassLoader loader)
Returns theClass
object associated with the class or interface with the given string name, using the given class loader.static @NotNull Class<?>
getCallerClass()
获取调用者的Classstatic @Nullable Class<?>
getCallerClass(int point)
获取调用者的Classstatic ClassLoader
getClassLoader(Class<?> clazz)
static Toolkit.Reflection
getInstance()
这只能用来判断使用哪种方法Object.getClass()
static Object
getObjectValue(Object this_, @NotNull Field field)
Get field value with Unsafestatic MethodHandles.Lookup
getRoot()
获取最高权限的Lookupstatic Class<?>
loadClassFrom(String name, @NotNull Collection<ClassLoader> loaders)
Load class from class loadersstatic Class<?>
loadClassLink(Collection<String> types, @Nullable ClassLoader loader)
static @Nullable Class<?>
loadClassWith(String name, int flags)
Load class from class loadersstatic <T extends AccessibleObject>
TsetAccess(T accessibleObject, boolean b)
static void
setObjectValue(Object this_, @NotNull Field field, Object value)
Change object's field value.
-
-
-
方法详细资料
-
getInstance
@Contract(pure=true) public static Toolkit.Reflection getInstance()
这只能用来判断使用哪种方法Object.getClass()
- 返回:
- 使用的实例
-
getRoot
@Contract(pure=true) public static MethodHandles.Lookup getRoot()
获取最高权限的Lookup- 返回:
- 最高权限的Lookup
-
forName
public static Class<?> forName(String name, boolean initialize, ClassLoader loader)
Returns theClass
object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned bygetName
) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameterloader
is null, the class is loaded through the bootstrap class loader. The class is initialized only if theinitialize
parameter istrue
and if it has not been initialized earlier.If
name
denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name isname
. Therefore, this method cannot be used to obtain any of theClass
objects representing primitive types or void.If
name
denotes an array class, the component type of the array class is loaded but not initialized.For example, in an instance method the expression:
Class.forName("Foo")
is equivalent to:
Class.forName("Foo", true, this.getClass().getClassLoader())
Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification. Note that this method does not check whether the requested class is accessible to its caller.
- 参数:
name
- fully qualified name of the desired classinitialize
- iftrue
the class will be initialized. See Section 12.4 of The Java Language Specification.loader
- class loader from which the class must be loaded- 返回:
- class object representing the desired class
- 抛出:
LinkageError
- if the linkage failsExceptionInInitializerError
- if the initialization provoked by this method failsNoClassDefFoundError
- if the class cannot be located by the specified class loaderSecurityException
- if a security manager is present, and theloader
isnull
, and the caller's class loader is notnull
, and the caller does not have theRuntimePermission
("getClassLoader")
- 从以下版本开始:
- 1.2
- 另请参阅:
Class.forName(String)
,Class.forName(String, boolean, ClassLoader)
,ClassLoader
-
loadClassWith
@Contract("null,_ -> null; !null, _ -> !null") @Nullable public static @Nullable Class<?> loadClassWith(String name, int flags) throws ClassNotFoundException
Load class from class loaders- 参数:
name
- The class nameflags
- Loading flags- 返回:
- The class loaded
- 抛出:
ClassNotFoundException
- If class not found- 从以下版本开始:
- 2.2
-
loadClassFrom
@Contract("null,_ -> null;!null,_ -> !null") public static Class<?> loadClassFrom(String name, @NotNull @NotNull Collection<ClassLoader> loaders) throws ClassNotFoundException
Load class from class loaders- 参数:
name
- The name of classloaders
- Loaders- 返回:
- The class loaded
- 抛出:
ClassNotFoundException
- If class not found in any loader- 从以下版本开始:
- 2.2
-
loadClassLink
public static Class<?> loadClassLink(Collection<String> types, @Nullable @Nullable ClassLoader loader) throws ClassNotFoundException
-
setAccess
@Contract("null, _ -> null; !null, _ -> !null") public static <T extends AccessibleObject> T setAccess(T accessibleObject, boolean b)
-
allocObject
public static <T> T allocObject(Class<T> type)
-
defineClass
public static Class<?> defineClass(ClassLoader loader, String name, @NotNull @org.jetbrains.annotations.NotNull byte[] b, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain. The set of certificates for a class is obtained from the
CodeSource
within the ProtectionDomain of the class. Any classes added to that package must contain the same set of certificates or a SecurityException will be thrown. Note that if name is null, this check is not performed. You should always pass in the binary name of the class you are defining as well as the bytes. This ensures that the class you are defining is indeed the class you think it is.The specified name cannot begin with "java.", since all classes in the "java.* packages can only be defined by the bootstrap class loader. If name is not null, it must be equal to the binary name of the class specified by the byte array "b", otherwise a
NoClassDefFoundError
will be thrown.- 参数:
loader
- The loader to be executedname
- The expected binary name of the class, or null if not knownb
- The bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java™ Virtual Machine Specification.off
- The start offset in b of the class datalen
- The length of the class dataprotectionDomain
- The ProtectionDomain of the class- 返回:
- The Class object created from the data, and optional ProtectionDomain.
- 抛出:
ClassFormatError
- If the data did not contain a valid classNoClassDefFoundError
- If name is not equal to the binary name of the class specified by bIndexOutOfBoundsException
- If either off or len is negative, or if off+len is greater than b.length.SecurityException
- If an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates than this class, or if name begins with "java.".
-
defineClass
public static Class<?> defineClass(ClassLoader loader, String name, @NotNull @org.jetbrains.annotations.NotNull byte[] code, ProtectionDomain protectionDomain) throws ClassFormatError
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain. The set of certificates for a class is obtained from the
CodeSource
within the ProtectionDomain of the class. Any classes added to that package must contain the same set of certificates or a SecurityException will be thrown. Note that if name is null, this check is not performed. You should always pass in the binary name of the class you are defining as well as the bytes. This ensures that the class you are defining is indeed the class you think it is.The specified name cannot begin with "java.", since all classes in the "java.* packages can only be defined by the bootstrap class loader. If name is not null, it must be equal to the binary name of the class specified by the byte array "b", otherwise a
NoClassDefFoundError
will be thrown.- 参数:
loader
- The loader to be executedname
- The expected binary name of the class, or null if not knowncode
- The bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java™ Virtual Machine Specification.protectionDomain
- The ProtectionDomain of the class- 返回:
- The Class object created from the data, and optional ProtectionDomain.
- 抛出:
ClassFormatError
- If the data did not contain a valid classNoClassDefFoundError
- If name is not equal to the binary name of the class specified by codeSecurityException
- If an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates than this class, or if name begins with "java.".- 从以下版本开始:
- 2.11
-
defineClass
public static Class<?> defineClass(ClassLoader loader, @NotNull @org.jetbrains.annotations.NotNull byte[] code, ProtectionDomain protectionDomain) throws ClassFormatError
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain. The set of certificates for a class is obtained from the
CodeSource
within the ProtectionDomain of the class. Any classes added to that package must contain the same set of certificates or a SecurityException will be thrown. Note that if name is null, this check is not performed. You should always pass in the binary name of the class you are defining as well as the bytes. This ensures that the class you are defining is indeed the class you think it is.The specified name cannot begin with "java.", since all classes in the "java.* packages can only be defined by the bootstrap class loader. If name is not null, it must be equal to the binary name of the class specified by the byte array "b", otherwise a
NoClassDefFoundError
will be thrown.- 参数:
loader
- The loader to be executedcode
- The bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java™ Virtual Machine Specification.protectionDomain
- The ProtectionDomain of the class- 返回:
- The Class object created from the data, and optional ProtectionDomain.
- 抛出:
ClassFormatError
- If the data did not contain a valid classNoClassDefFoundError
- If name is not equal to the binary name of the class specified by bSecurityException
- If an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates than this class, or if name begins with "java.".- 从以下版本开始:
- 2.11
-
defineClass
public static Class<?> defineClass(ClassLoader loader, @NotNull @NotNull org.objectweb.asm.ClassWriter code, ProtectionDomain protectionDomain) throws ClassFormatError
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain. The set of certificates for a class is obtained from the
CodeSource
within the ProtectionDomain of the class. Any classes added to that package must contain the same set of certificates or a SecurityException will be thrown. Note that if name is null, this check is not performed. You should always pass in the binary name of the class you are defining as well as the bytes. This ensures that the class you are defining is indeed the class you think it is.The specified name cannot begin with "java.", since all classes in the "java.* packages can only be defined by the bootstrap class loader. If name is not null, it must be equal to the binary name of the class specified by the byte array "b", otherwise a
NoClassDefFoundError
will be thrown.- 参数:
loader
- The loader to be executedcode
- The bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java™ Virtual Machine Specification.protectionDomain
- The ProtectionDomain of the class- 返回:
- The Class object created from the data, and optional ProtectionDomain.
- 抛出:
ClassFormatError
- If the data did not contain a valid classNoClassDefFoundError
- If name is not equal to the binary name of the class specified by bSecurityException
- If an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates than this class, or if name begins with "java.".- 从以下版本开始:
- 2.11
-
getCallerClass
@NotNull @Contract(pure=true) public static @NotNull Class<?> getCallerClass()
获取调用者的Class- 返回:
- 获取调用者的Class
- 抛出:
ArrayIndexOutOfBoundsException
- 在程序main入口调用时抛出
-
getCallerClass
@Nullable @Contract(pure=true) public static @Nullable Class<?> getCallerClass(int point)
获取调用者的Class- 参数:
point
- 指定的堆栈位置- 返回:
- 获取调用者的Class
-
clone
@Contract(pure=true, value="null -> null; !null -> !null") public static <T> T clone(T object)
Clone a object.- 类型参数:
T
- The type of object- 参数:
object
- The source object- 返回:
- the cloned object
- 从以下版本开始:
- 2.7
-
getObjectValue
public static Object getObjectValue(Object this_, @NotNull @NotNull Field field)
Get field value with Unsafe- 参数:
this_
- The this object of field.field
- The field of object.- 返回:
- The field value.
- 从以下版本开始:
- 2.7
-
setObjectValue
public static void setObjectValue(Object this_, @NotNull @NotNull Field field, Object value)
Change object's field value.- 参数:
this_
- The this variable.field
- The field need change.value
- The value override.
-
getClassLoader
@Contract(pure=true, value="null -> fail;") public static ClassLoader getClassLoader(Class<?> clazz)
-
-