模块 mxlib.api

类 Toolkit.Reflection

  • 直接已知子类:
    UFRF
    封闭类:
    Toolkit

    public abstract static class Toolkit.Reflection
    extends Object
    反射工具
    • 字段详细资料

      • LOAD_CLASS_THREAD_CONTENT

        public static final int LOAD_CLASS_THREAD_CONTENT
        另请参阅:
        常量字段值
      • LOAD_CLASS_CALLER_CLASSLOADER

        public static final int LOAD_CLASS_CALLER_CLASSLOADER
        另请参阅:
        常量字段值
    • 构造器详细资料

      • Reflection

        public Reflection()
    • 方法详细资料

      • getRoot

        @Contract(pure=true)
        public static MethodHandles.Lookup getRoot()
        获取最高权限的Lookup
        返回:
        最高权限的Lookup
      • forName

        public static Class<?> forName​(String name,
                                       boolean initialize,
                                       ClassLoader loader)
        Returns the Class 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 by getName) 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 parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true 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 is name. Therefore, this method cannot be used to obtain any of the Class 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 class
        initialize - if true 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 fails
        ExceptionInInitializerError - if the initialization provoked by this method fails
        NoClassDefFoundError - if the class cannot be located by the specified class loader
        SecurityException - if a security manager is present, and the loader is null, and the caller's class loader is not null, and the caller does not have the RuntimePermission("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 name
        flags - 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 class
        loaders - Loaders
        返回:
        The class loaded
        抛出:
        ClassNotFoundException - If class not found in any loader
        从以下版本开始:
        2.2
      • 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 executed
        name - The expected binary name of the class, or null if not known
        b - 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 data
        len - The length of the class data
        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 class
        NoClassDefFoundError - If name is not equal to the binary name of the class specified by b
        IndexOutOfBoundsException - 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 executed
        name - The expected binary name of the class, or null if not known
        code - 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 class
        NoClassDefFoundError - If name is not equal to the binary name of the class specified by code
        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.".
        从以下版本开始:
        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 executed
        code - 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 class
        NoClassDefFoundError - If name is not equal to the binary name of the class specified by b
        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.".
        从以下版本开始:
        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 executed
        code - 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 class
        NoClassDefFoundError - If name is not equal to the binary name of the class specified by b
        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.".
        从以下版本开始:
        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.
      • copyValues

        public static void copyValues​(Object from,
                                      Object to)
      • getClassLoader

        @Contract(pure=true,
                  value="null -> fail;")
        public static ClassLoader getClassLoader​(Class<?> clazz)