• Java反射初探123456789


    牛逼的框架都反射,不要问我为什么,因为我也不知道

    可能是因为生成了class文件没法实例化,所以只能反射创建对象,但是在spring中,ioc就是反射实现的控制反转

    看Spring4.x企业级开发实战  自己写的  第一个是Person类

    package com.smart.reflect;
    
    /**
     * @program: chapter3
     * @description: Entity
     * @author: Sunshine
     * @create: 2020-12-25 15:34
     */
    public class Person {
        private String name;
        private String nipples;
        private int hight;
    
        public Person() {
        }
    
        public Person(String name, String nipples, int hight) {
            this.name = name;
            this.nipples = nipples;
            this.hight = hight;
        }
    
        public void introduce () {
            System.out.println("name:"+ name+"; nipples:" + nipples +";hight:"+ hight );
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getNipples() {
            return nipples;
        }
    
        public void setNipples(String nipples) {
            this.nipples = nipples;
        }
    
        public int getHight() {
            return hight;
        }
    
        public void setHight(int hight) {
            this.hight = hight;
        }
    }

    package com.smart.reflect;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    
    /**
     * @program: chapter3
     * @description: 测试反射
     * @author: Sunshine
     * @create: 2020-12-25 15:39
     */
    public class ReflectTest {
        public static Person initByDefaultConst() throws  Throwable {
            //1 类加载器获取对象
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            Class clazz = loader.loadClass("com.smart.reflect.Person");
            //2 获取类的默认构造器对象 ,通过它实例化Person
            Constructor cons = clazz.getDeclaredConstructor((Class<?>[]) null);
            Person person = (Person) cons.newInstance();
    
            //3 通过反射方法设置(属性)
    
            Method setName = clazz.getMethod("setName", String.class);
          // 这里才是真正的射入属性 当然得有前面的铺垫,要不直接射不太行,你说呢? setName.invoke(person,
    "兔宝宝"); Method setNipples = clazz.getMethod("setNipples", String.class); setNipples.invoke(person, "pink"); Method setHignt = clazz.getMethod("setHight", int.class); setHignt.invoke(person, 170); return person; } public static void main(String[] args) { try { Person p = initByDefaultConst(); p.introduce(); } catch (Throwable throwable) { throwable.printStackTrace(); } } }
    红色的方法对应的jdk源码
    
    /**
         * Returns a {@code Constructor} object that reflects the specified
         * constructor of the class or interface represented by this
         * {@code Class} object.  The {@code parameterTypes} parameter is
         * an array of {@code Class} objects that identify the constructor's
         * formal parameter types, in declared order.
         *
         * If this {@code Class} object represents an inner class
         * declared in a non-static context, the formal parameter types
         * include the explicit enclosing instance as the first parameter.
         *
         * @param parameterTypes the parameter array
         * @return  The {@code Constructor} object for the constructor with the
         *          specified parameter list
         * @throws  NoSuchMethodException if a matching method is not found.
         * @throws  SecurityException
         *          If a security manager, <i>s</i>, is present and any of the
         *          following conditions is met:
         *
         *          <ul>
         *
         *          <li> the caller's class loader is not the same as the
         *          class loader of this class and invocation of
         *          {@link SecurityManager#checkPermission
         *          s.checkPermission} method with
         *          {@code RuntimePermission("accessDeclaredMembers")}
         *          denies access to the declared constructor
         *
         *          <li> the caller's class loader is not the same as or an
         *          ancestor of the class loader for the current class and
         *          invocation of {@link SecurityManager#checkPackageAccess
         *          s.checkPackageAccess()} denies access to the package
         *          of this class
         *
         *          </ul>
         *
         * @since JDK1.1
         */
        @CallerSensitive
        public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
            throws NoSuchMethodException, SecurityException {
            checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
            return getConstructor0(parameterTypes, Member.DECLARED);
        }

    其实就是获取当前的这个Person类的默认的构造函数,了解的差不多就行了,别逮到一个类就往死点jdk   深入是深入了   出不来不是白给么?

    下面这个图片就是借花献佛以下,下面也说了   newInsrtance()就是实例化对象用的

     类装载器 CLassLoader

    懒得打字  直接看图 

    类装载找类字节,虚机建类象组件
    类加载装虚机中,需要通过下几步
    
    查找导入类文件,校验准备和解析
    校验类文件数据,是否准确不掺假
    准备阶段很关键,要给静态配空间
    解析阶段可以选,符号引用变直引
    
    初始化阶段不拉胯,静态系列初始化




                JVM

    ClassLoader      ExtClassLoader    AppClassLoader

      

     ClassLoader loader = Thread.currentThread().getContextClassLoader();
            System.out.println("current Loader" + loader);
            System.out.println("baba Loader" + loader.getParent());
            System.out.println("yeye Loader" + loader.getParent().getParent());




    "C:Program FilesJavajdk1.8.0_152injava.exe" "-javaagent:E:ideaIntelliJ IDEA 2020.2.4libidea_rt.jar=57015:E:ideaIntelliJ IDEA 2020.2.4in" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_152jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_152jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_152jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_152jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_152jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_152jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_152jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_152jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_152jrelibext ashorn.jar;C:Program FilesJavajdk1.8.0_152jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_152jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_152jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_152jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_152jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_152jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_152jrelibjce.jar;C:Program FilesJavajdk1.8.0_152jrelibjfr.jar;C:Program FilesJavajdk1.8.0_152jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_152jrelibjsse.jar;C:Program FilesJavajdk1.8.0_152jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_152jrelibplugin.jar;C:Program FilesJavajdk1.8.0_152jrelib esources.jar;C:Program FilesJavajdk1.8.0_152jrelib t.jar;C:UsersAdministratorDesktopspring4.xwangpanchapter3 argetclasses;D:ToolsForDev epositoryorgspringframeworkootspring-boot-starter-web1.3.3.RELEASEspring-boot-starter-web-1.3.3.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot-starter1.3.3.RELEASEspring-boot-starter-1.3.3.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot1.3.3.RELEASEspring-boot-1.3.3.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot-autoconfigure1.3.3.RELEASEspring-boot-autoconfigure-1.3.3.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot-starter-logging1.3.3.RELEASEspring-boot-starter-logging-1.3.3.RELEASE.jar;D:ToolsForDev epositorychqoslogbacklogback-classic1.1.5logback-classic-1.1.5.jar;D:ToolsForDev epositorychqoslogbacklogback-core1.1.5logback-core-1.1.5.jar;D:ToolsForDev epositoryorgslf4jslf4j-api1.7.16slf4j-api-1.7.16.jar;D:ToolsForDev epositoryorgslf4jjcl-over-slf4j1.7.16jcl-over-slf4j-1.7.16.jar;D:ToolsForDev epositoryorgslf4jjul-to-slf4j1.7.16jul-to-slf4j-1.7.16.jar;D:ToolsForDev epositoryorgslf4jlog4j-over-slf4j1.7.16log4j-over-slf4j-1.7.16.jar;D:ToolsForDev epositoryorgyamlsnakeyaml1.16snakeyaml-1.16.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot-starter-tomcat1.3.3.RELEASEspring-boot-starter-tomcat-1.3.3.RELEASE.jar;D:ToolsForDev epositoryorgapache omcatembed omcat-embed-logging-juli8.0.32 omcat-embed-logging-juli-8.0.32.jar;D:ToolsForDev epositoryorgapache omcatembed omcat-embed-websocket8.0.32 omcat-embed-websocket-8.0.32.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot-starter-validation1.3.3.RELEASEspring-boot-starter-validation-1.3.3.RELEASE.jar;D:ToolsForDev epositoryorghibernatehibernate-validator5.2.4.Finalhibernate-validator-5.2.4.Final.jar;D:ToolsForDev epositoryjavaxvalidationvalidation-api1.1.0.Finalvalidation-api-1.1.0.Final.jar;D:ToolsForDev epositoryorgjbossloggingjboss-logging3.3.0.Finaljboss-logging-3.3.0.Final.jar;D:ToolsForDev epositorycomfasterxmlclassmate1.1.0classmate-1.1.0.jar;D:ToolsForDev epositorycomfasterxmljacksoncorejackson-databind2.6.5jackson-databind-2.6.5.jar;D:ToolsForDev epositorycomfasterxmljacksoncorejackson-annotations2.6.5jackson-annotations-2.6.5.jar;D:ToolsForDev epositorycomfasterxmljacksoncorejackson-core2.6.5jackson-core-2.6.5.jar;D:ToolsForDev epositoryorgspringframeworkspring-web4.2.5.RELEASEspring-web-4.2.5.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkspring-aop4.2.5.RELEASEspring-aop-4.2.5.RELEASE.jar;D:ToolsForDev epositoryaopallianceaopalliance1.0aopalliance-1.0.jar;D:ToolsForDev epositoryorgspringframeworkspring-beans4.2.5.RELEASEspring-beans-4.2.5.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkspring-context4.2.5.RELEASEspring-context-4.2.5.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkspring-webmvc4.2.5.RELEASEspring-webmvc-4.2.5.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkspring-expression4.2.5.RELEASEspring-expression-4.2.5.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot-starter-jdbc1.3.3.RELEASEspring-boot-starter-jdbc-1.3.3.RELEASE.jar;D:ToolsForDev epositoryorgapache omcat omcat-jdbc8.0.32 omcat-jdbc-8.0.32.jar;D:ToolsForDev epositoryorgapache omcat omcat-juli8.0.32 omcat-juli-8.0.32.jar;D:ToolsForDev epositoryorgspringframeworkspring-jdbc4.2.5.RELEASEspring-jdbc-4.2.5.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkspring-tx4.2.5.RELEASEspring-tx-4.2.5.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot-starter-actuator1.3.3.RELEASEspring-boot-starter-actuator-1.3.3.RELEASE.jar;D:ToolsForDev epositoryorgspringframeworkootspring-boot-actuator1.3.3.RELEASEspring-boot-actuator-1.3.3.RELEASE.jar;D:ToolsForDev epositorymysqlmysql-connector-java5.1.38mysql-connector-java-5.1.38.jar;D:ToolsForDev epositoryorgapache omcatembed omcat-embed-core8.0.32 omcat-embed-core-8.0.32.jar;D:ToolsForDev epositoryorgapache omcatembed omcat-embed-el8.0.32 omcat-embed-el-8.0.32.jar;D:ToolsForDev epositoryjavaxservletjstl1.2jstl-1.2.jar;D:ToolsForDev epositoryorgspringframeworkspring-core4.2.5.RELEASEspring-core-4.2.5.RELEASE.jar" com.smart.reflect.ReflectTest name:兔宝宝; nipples:pink;hight:170

    当前加载器 current Loadersun.misc.Launcher$AppClassLoader@18b4aac2
    当前加载器的爹
    baba Loadersun.misc.Launcher$ExtClassLoader@33c7353a
    当前加载器的爷爷
    yeye Loadernull
    Process finished with exit code 0

     

     

     这个费点劲   还得启动 
    用这个省事

    package com.smart.utils;
    
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.security.CodeSource;
    import java.security.ProtectionDomain;
    
    /**
     * tools to find which jar does the class come from 
     * @author : chenxh
     * @date: 16-04-0
     */
    public class ClassLocationUtils {
    
        /**
         * find the location of the class come from
         * @param cls
         * @return
         */
        public static String where(final Class cls) {
            if (cls == null)throw new IllegalArgumentException("null input: cls");
            URL result = null;
            final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
            final ProtectionDomain pd = cls.getProtectionDomain();
            if (pd != null) {
                final CodeSource cs = pd.getCodeSource();
                if (cs != null) result = cs.getLocation();
                if (result != null) {
                    if ("file".equals(result.getProtocol())) {
                        try {
                            if (result.toExternalForm().endsWith(".jar") ||
                                    result.toExternalForm().endsWith(".zip"))
                                result = new URL("jar:".concat(result.toExternalForm())
                                        .concat("!/").concat(clsAsResource));
                            else if (new File(result.getFile()).isDirectory())
                                result = new URL(result, clsAsResource);
                        }
                        catch (MalformedURLException ignore) {}
                    }
                }
            }
            if (result == null) {
                final ClassLoader clsLoader = cls.getClassLoader();
                result = clsLoader != null ?
                        clsLoader.getResource(clsAsResource) :
                        ClassLoader.getSystemResource(clsAsResource);
            }
            return result.toString();
        }
    
    }

    自己试了一下   嘿  您猜怎么着   还真好使

    file:/C:/Users/Administrator/Desktop/spring4.x/wangpan/chapter3/target/classes/com/smart/reflect/Person.class

     

     

    新鲜刺激的东西永远都有,玩之前掂量掂量自己几斤几两
  • 相关阅读:
    【原】webpack--loaders,主要解释为什么需要loaders和注意事项
    【原】通过npm script运行webpack的原理
    原生js深拷贝函数
    git add 添加错文件的撤销方法
    item2 快捷键
    sudo su 和sudo -s的区别
    nvm常用命令
    【雅思】【口语】Describe a product you bought and felt happy
    【雅思】【口语】Help others
    【雅思】【口语】
  • 原文地址:https://www.cnblogs.com/banxianer/p/14190177.html
Copyright © 2020-2023  润新知