美文网首页
java高级编程-反射获取对象示例的四种方式及对比(附源码解析)

java高级编程-反射获取对象示例的四种方式及对比(附源码解析)

作者: yufw | 来源:发表于2020-04-30 21:22 被阅读0次

    反射获取对象示例的方式及个方式对比(附源码解析)

    四种实现方式

    1. 对象.getClass()

      // 对象都有了还要反射干什么。所以一般不用这个
      // 不会初始化对象

    2. Class.forName()
      ​ // 使用最多的方式
      ​ // 会初始化对象

    3. 对象类.class 属性

      // 需要导入类的包,依赖太强
      // 对象都已经存在 肯定 已经初始化

    4. 通过ClassLoader

      // 不会对 对象初始化

    实现源码及各方法对比

        @Test
        public void getClassTest() throws ClassNotFoundException {
    
    
            // 获取Class 对象 的方式
    
            // 1. 对象.getClass();
            // 对象都有了还要反射干什么
            // 不会初始化对象
            People p = new People();
            Class<? extends People> aClass = p.getClass();
            System.out.println(aClass);
    
    
            // 2. Class.forName()
            // 使用最多的方式
            // 会初始化对象
            Class<?> aClass1 = Class.forName("com.studynote.utils.People");
    
            System.out.println(aClass1);
            
            // 可以控制是否 初始化对象 并且可以指定 类加载器
            Class<?> class1 = Class.forName("com.studynote.utils.People", true, this.getClass().getClassLoader());
    
            // 3. 对象类.class 属性
            // 需要导入类的包,依赖太强
            // 对象都已经存在 肯定 已经初始化
            Class<People> pClass = People.class;
            System.out.println(pClass);
    
            // 4. 通过ClassLoader
            // 不会对 对象初始化
            Class<?> aClass2 = ClassLoader.getSystemClassLoader().loadClass("com.studynote.utils.People");
            System.out.println(aClass2);
    
            System.out.println("+++++++++++++++++++++++++++++++++++++++++");
    
            // getClasses() 返回该类中所有 公共 类和接口类的对象数组
            Arrays.stream(aClass1.getClasses()).forEach(System.out::println);
    
            // getDeclaredClasses() 返回该类中所有类和接口类的对象数组
            Arrays.stream(aClass1.getDeclaredClasses()).forEach(System.out::println);
    
    
        }
    
    1. clazz.forName(String name, boolean initialize, ClassLoader loader) 源码
        /**
         * Returns the {@code 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 {@code 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
         * {@code loader} is null, the class is loaded through the bootstrap
         * class loader.  The class is initialized only if the
         * {@code initialize} parameter is {@code true} and if it has
         * not been initialized earlier.
         *
         * <p> If {@code 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 {@code name}. Therefore, this method cannot be used to
         * obtain any of the {@code Class} objects representing primitive
         * types or void.
         *
         * <p> If {@code name} denotes an array class, the component type of
         * the array class is loaded but not initialized.
         *
         * <p> For example, in an instance method the expression:
         *
         * <blockquote>
         *  {@code Class.forName("Foo")}
         * </blockquote>
         *
         * is equivalent to:
         *
         * <blockquote>
         *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
         * </blockquote>
         *
         * Note that this method throws errors related to loading, linking or
         * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
         * Java Language Specification</em>.
         * Note that this method does not check whether the requested class
         * is accessible to its caller.
         *
         * @param name       fully qualified name of the desired class
         * @param initialize if {@code true} the class will be initialized.
         *                   See Section 12.4 of <em>The Java Language Specification</em>.
         * @param loader     class loader from which the class must be loaded
         * @return           class object representing the desired class
         *
         * @exception LinkageError if the linkage fails
         * @exception ExceptionInInitializerError if the initialization provoked
         *            by this method fails
         * @exception ClassNotFoundException if the class cannot be located by
         *            the specified class loader
         * @exception SecurityException
         *            if a security manager is present, and the {@code loader} is
         *            {@code null}, and the caller's class loader is not
         *            {@code null}, and the caller does not have the
         *            {@link RuntimePermission}{@code ("getClassLoader")}
         *
         * @see       java.lang.Class#forName(String)
         * @see       java.lang.ClassLoader
         * @since     1.2
         */
        @CallerSensitive
        public static Class<?> forName(String name, boolean initialize,
                                       ClassLoader loader)
            throws ClassNotFoundException
        {
            Class<?> caller = null;
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) {
                // Reflective call to get caller class is only needed if a security manager
                // is present.  Avoid the overhead of making this call otherwise.
                caller = Reflection.getCallerClass();
                if (loader == null) {
                    ClassLoader ccl = ClassLoader.getClassLoader(caller);
                    if (ccl != null) {
                        sm.checkPermission(
                            SecurityConstants.GET_CLASSLOADER_PERMISSION);
                    }
                }
            }
            return forName0(name, initialize, loader, caller);
        }
    
    2. clazz.forName(String className)源码解析
        @CallerSensitive
        public static Class<?> forName(String className)
                    throws ClassNotFoundException {
            Class<?> caller = Reflection.getCallerClass();
            return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
        }
    
    3.ClassLoader.loadClass(String name) 源码解析
        /**
         * Loads the class with the specified <a href="#binary-name">binary name</a>.
         * This method searches for classes in the same manner as the {@link
         * #loadClass(String, boolean)} method.  It is invoked by the Java virtual
         * machine to resolve class references.  Invoking this method is equivalent
         * to invoking {@link #loadClass(String, boolean) loadClass(name,
         * false)}.
         *
         * @param  name
         *         The <a href="#binary-name">binary name</a> of the class
         *
         * @return  The resulting {@code Class} object
         *
         * @throws  ClassNotFoundException
         *          If the class was not found
         */
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            return loadClass(name, false);
        }
    

    源码地址:https://gitee.com/twelfthLunarMonthFourteen/pub_beauty/tree/hotfix/study-note/src/main/java/com/studynote/reflect

    个人水平有限,如有问题,请各路大神指教留言,评论区讨论,虚心接纳

    如果觉得有帮助,请点赞收藏,谢谢

    相关文章

      网友评论

          本文标题:java高级编程-反射获取对象示例的四种方式及对比(附源码解析)

          本文链接:https://www.haomeiwen.com/subject/vwztghtx.html