0
点赞
收藏
分享

微信扫一扫

java 反射

陆佃 2022-03-10 阅读 209

反射

反射提供的功能

反射入口

获取Class的三种方法

  • Class.forName() (使用较多)
        Class<?> clazz = null;
        try {
            clazz = Class.forName("com.dream.xiaobo.reflect.impl.MyInterfaceImpl");
            System.out.println(clazz);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
  • 类名.class
System.out.println(MyInterfaceImpl.class);
  • 对象.getClass
        MyInterfaceImpl myInterface = new MyInterfaceImpl();
        Class<? extends MyInterfaceImpl> aClass = myInterface.getClass();
        System.out.println(aClass);

获取方法

        Class<?> clazz = null;
        try {
            clazz = Class.forName("com.dream.xiaobo.reflect.impl.MyInterfaceImpl");
            //获取所有的公共方法(本类以及父类、接口中的所有方法  符合的访问修饰符规律的方法)
            Method[] methods = clazz.getMethods();
            for(Method method: methods){
                System.out.println(method);
            }
            System.out.println("-------------");
            //当前类的所有方法、不收访问修饰符的限制
            Method[] declaredMethods = clazz.getDeclaredMethods();
            for (Method declare:declaredMethods){
                System.out.println(declare);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

获取接口

        Class clazz = null;
        try {
            clazz = Class.forName("com.dream.xiaobo.reflect.impl.MyInterfaceImpl");
            Class[] interfaces = clazz.getInterfaces();
            for (Class inter: interfaces){
                System.out.println(inter);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

获取父类

        Class clazz = null;
        try {
            clazz = Class.forName("com.dream.xiaobo.reflect.impl.MyInterfaceImpl");
            Class superclass = clazz.getSuperclass();
            System.out.println(superclass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

获取构造方法

        Class clazz = null;
        try {
            clazz = Class.forName("com.dream.xiaobo.reflect.impl.MyInterfaceImpl");
            Constructor[] constructors = clazz.getConstructors();
            for(Constructor constructor: constructors){
                System.out.println(constructor);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

获取属性

        Class clazz = null;
        try {
            clazz = Class.forName("com.dream.xiaobo.reflect.impl.MyInterfaceImpl");
            //获取共有的属性
            Field[] fields = clazz.getFields();
            for (Field field : fields){
                System.out.println(field);
            }
            //获取本类的属性
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field fie : declaredFields){
                System.out.println(fie);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

获取类的类的对象

Class clazz = null;
        try {
            clazz = Class.forName("com.dream.xiaobo.reflect.impl.MyInterfaceImpl");
            //获取当前反射所代表类(接口)的实例对象
            MyInterfaceImpl o = (MyInterfaceImpl)clazz.newInstance();
            o.getInterface1();
            o.getInterface2();

        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

操作属性和方法

        Class clazz = null;

        try {
            clazz = Class.forName("com.dream.xiaobo.reflect.impl.MyInterfaceImpl");
            //获取到反射类的对象
            MyInterfaceImpl instance = (MyInterfaceImpl) clazz.newInstance();
            //获取到id属性
            Field id = clazz.getDeclaredField("id");

            //屏蔽private修饰符
            id.setAccessible(true);
            //给id赋值
            id.set(instance,1);
            System.out.println(instance.getId());

            System.out.println("---------------");
            //获取到student方法
            Method method = clazz.getDeclaredMethod("student", String.class);

            //屏蔽private修饰符
            method.setAccessible(true);

            //调用方法
            method.invoke(instance,"xiaobo");

            System.out.println("---------------");


            //获取public修饰符的构造方法
            Constructor constructor1 = clazz.getDeclaredConstructor(String.class);
            //调用构造方法
            MyInterfaceImpl xiaobo = (MyInterfaceImpl)constructor1.newInstance("xiaobo");

            System.out.println("---------------");
            
            //获取private修饰符的构造方法
            Constructor constructor2 = clazz.getDeclaredConstructor(Integer.class);
            //屏蔽private修饰符访问权限
            constructor2.setAccessible(true);
            //调用构造方法
            MyInterfaceImpl wyb = (MyInterfaceImpl)constructor2.newInstance(1);

        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

越过泛型检查

            List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);

            Class clazz = list.getClass();
            try {
                Method method = clazz.getMethod("add",Object.class);

                method.invoke(list,"xiaobo");

                System.out.println(list);

简单应用

public static void propertyUtils(Object type,String name,Object value){

        Class<?> clazz = type.getClass();

        try {
            Field field = clazz.getDeclaredField(name);

            field.setAccessible(true);
            field.set(type,value);

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
MyInterfaceImpl myInterface = new MyInterfaceImpl();

            PropertyUtils.propertyUtils(myInterface,"id",1);
            PropertyUtils.propertyUtils(myInterface,"name","xiaobo");
            System.out.println(myInterface.getId());
            System.out.println(myInterface.getName());

            Student student = new Student();
            PropertyUtils.propertyUtils(student,"score",99.99);
            System.out.println(student.getScore());

正确的开始、微小的长进、然后持续、嘿、我是小博、带你一起看我目之所及的世界…

举报

相关推荐

0 条评论