zoukankan      html  css  js  c++  java
  • java 反射的学习实践2

    Java 程序语言的后门-反射机制

    //通知dousil做事
    method.invoke(object,args);
    //通过反射,将h作为参数,实例化代理类,返回代理实例
    return cons.newInstance(new Object[]{h})
    //将接口数组赋值一份
    final  Class<?> intfs = interfaces.clone()

    感谢作者幸苦整理:本文内容都参考来自:https://www.cnblogs.com/zhangchengzi/p/9723250.html

    我只是一个技术的搬运工

    1. 反射是什么

      反射是什么,其实是java语言的一种机制,我的理解是java的后门,在java中一切皆是对象,那么类也是对象。类的对象是什么,类的对象是:java.lang.Class。那么这个对象是怎么创建的。我们都知道,要得到一个类的对象,最基本的方法是new关键字。但是对于一个特殊的对象是不能用class来创建的。

    /*
         * 私有化构造方法,只有java 虚拟机才能创建类对象
         * Private constructor. Only the Java Virtual Machine creates Class objects.
         * This constructor is not used and prevents the default constructor being
         * generated.
         */
        private Class(ClassLoader loader) {
            // Initialize final field for classLoader.  The initialization value of non-null
            // prevents future JIT optimizations from assuming this final field is null.
            classLoader = loader;
        }

    Class类中只有一个私有的构造方法,其实类对象是java虚拟机(jvm)在加载class文件的时候自动在虚拟机中给我们创建的。

      这里就涉及到了另一个机制:类加载机制

      简单描述一下类加载机制:就是虚拟及将class文件加载到虚拟及内存中,最终可以被虚拟机值及使用的java类型。虚拟机会将class文件中的信息按照所需的格式放在方法取中,同时会在内存(堆)中放一个

    java.lang.Class对象。

      那么怎么通过对象来实现反射呢?为了方便理解,我们举一个有取的例子。

    2. 一个有趣的故事:

      

    有一天一个同事养了一只狗狗哈士奇,在作者面前大肆炫耀,说他的狗狗多么萌,多么威武,多么听话......,作者听完心生向往,提出去看一看。但是这个同事高傲的抬起头说了三个字:想的美。

      竟然不给我看!!!作者一气之下,就走了java程序的后门-反射。你不让我看,我偏偏要看。

      哈士奇类的定义:

    package com.zcz.reflecttest;
    
    public class HaShiQi implements Dog {
        public String color = "黑色";
        private String name = "富贵";
        
        //私有化构造
        private HaShiQi() {};
        @Override
        public void eat() {
            // TODO Auto-generated method stub
            System.out.println(name + " 去吃狗粮");
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println(name + " 去跑着玩儿");
        }
        
        private void dance() {
            System.out.println(name + " 来跳一支舞");
        }
        
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "名字:"+name+";颜色:"+color;
        }
    }
    package com.zcz.reflecttest;
    
    public interface Dog {
        public void eat();
        public void run();
    }

    从代码中可以看到,我的同时不仅仅没有告诉作者哈士奇竟然后跳舞,甚至连哈士奇的名字也没告诉我,甚至构造器都私有化了。那么接下来,走后门通过反射看狗狗

    package com.zcz.reflecttest;
    
    /**
     * 通过反射查看同事狗狗的信息
     * @author zhangchengzi
     *
     */
    public class LookLook {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //获取类对象,除了这份方法外还有另外两种方法
            Class clazz = HaShiQi.class;
            
        }
    
    }
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            // TODO Auto-generated method stub
            //获取类对象,除了这份方法外还有另外两种方法
            Class clazz = HaShiQi.class;
            
            System.out.println("——————— 获取所有公有的属性  —————————");
            Field[] fields = clazz.getFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }    
        }
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            // TODO Auto-generated method stub
            //获取类对象,除了这份方法外还有另外两种方法
            Class clazz = HaShiQi.class;
            
            System.out.println("——————— 获取所有公有的属性  —————————");
            Field[] fields = clazz.getFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }
            
            System.out.println("——————— 获取所有的属性  —————————");
            fields = clazz.getDeclaredFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }
        }
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            // TODO Auto-generated method stub
            //获取类对象,除了这份方法外还有另外两种方法
            Class clazz = HaShiQi.class;
            
            System.out.println("——————— 获取所有公有的属性  —————————");
            Field[] fields = clazz.getFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }
            
            System.out.println("——————— 获取所有的属性  —————————");
            fields = clazz.getDeclaredFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }
            System.out.println("——————— 获取所有公有的方法  —————————");
            Method[] methods = clazz.getMethods();
            for(Method method : methods) {
                System.out.println(method);
            }
        
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            // TODO Auto-generated method stub
            //获取类对象,除了这份方法外还有另外两种方法
            Class clazz = HaShiQi.class;
            
            System.out.println("——————— 获取所有公有的属性  —————————");
            Field[] fields = clazz.getFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }
            
            System.out.println("——————— 获取所有的属性  —————————");
            fields = clazz.getDeclaredFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }
            System.out.println("——————— 获取所有公有的方法  —————————");
            Method[] methods = clazz.getMethods();
            for(Method method : methods) {
                System.out.println(method);
            }
            System.out.println("——————— 获取所有类中声明的方法  —————————");
            methods = clazz.getDeclaredMethods();
            for(Method method : methods) {
                System.out.println(method);
            }
        }
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            // TODO Auto-generated method stub
            //获取类对象,除了这份方法外还有另外两种方法
            Class clazz = HaShiQi.class;
            
            System.out.println("——————— 获取所有公有的属性  —————————");
            Field[] fields = clazz.getFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }
            
            System.out.println("——————— 获取所有的属性  —————————");
            fields = clazz.getDeclaredFields();
            for(Field field : fields) {
                field.setAccessible(true);
                System.out.println(field);
            }
            System.out.println("——————— 获取所有公有的方法  —————————");
            Method[] methods = clazz.getMethods();
            for(Method method : methods) {
                System.out.println(method);
            }
            System.out.println("——————— 获取所有类中声明的方法  —————————");
            methods = clazz.getDeclaredMethods();
            for(Method method : methods) {
                System.out.println(method);
            }
            System.out.println("——————— 获取所有公有的构造器  —————————");
            Constructor[] constructors = clazz.getConstructors();
            for(Constructor constructor : constructors) {
                System.out.println(constructor);
            }
            
        }

    3. 我们通过构造器,将狗狗的所有的属性,方法都浏览了以便,

  • 相关阅读:
    C#编程总结 字符转码
    获取 苹果UDID 序列号
    SVN :This XML file does not appear to have any style information associated with it.
    oracle 实时查询最耗CPU资源的SQL语句
    Oracle 优化和性能调整
    weblogic管理脚本
    weblogic状态监控脚本
    oracle性能检测sql语句
    Oracle动态性能表-V$SESSION_WAIT,V$SESSION_EVENT
    linux HBA 卡驱动安装
  • 原文地址:https://www.cnblogs.com/dousil/p/12859203.html
Copyright © 2011-2022 走看看