反射概念及详解

发射,是指计算机程序在运行时(Run time)可以访问、检测和修改它本身状态或行为的一种能力

  • 实例化任意一个类的对象
  • 获取任意类的名称、包、属性、方法、注解、类型、类加载器等
  • 获取任意对象的属性,并且能修改对象的属性
  • 调用任意对象的方法
  • 判断任意一个对象所属的类
  1. 新建User类包含
  • 属性:id、name、address
  • 方法:无参say()、有参hello(String name)
  • 注解:@TestAnnotation
  1. 对反射进行ReflectTest

    package com.rangers.reflects;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    /**
     * @Author Rangers
     * @Description
     * @Date 2021-03-09
     **/
    public class ReflectTest {
        public static void main(String[] args) throws Exception {
    
            Class<?> userClazz = Class.forName(User.class.getName());
    
            // 获取类加载器
            ClassLoader classLoader = User.class.getClassLoader();
            System.out.println("获取类加载器:"+classLoader);
    
            // 获取类名
            System.out.println("User 类名:"+userClazz.getName());
    
            // 获取包名
            Package userPackage = userClazz.getPackage();
            System.out.println("User 包名:"+userPackage.getName());
    
            // 获取类型
            String typeName = userClazz.getTypeName();
            System.out.println("User 类型:"+typeName);
    
            // 创建对象
            User user = (User) userClazz.newInstance();
            System.out.println(user);
    
            // 对象是否为某个类型
            boolean instanceFlag = userClazz.isInstance(user);
            System.out.println("是否为某个类型:"+instanceFlag);
    
            // 获取所有属性
            Field[] declaredFields = userClazz.getDeclaredFields();
            for (Field field : declaredFields) {
                System.out.println("User 属性:"+field.getName());
            }
    
            // 获取所有方法
            Method[] declaredMethods = userClazz.getDeclaredMethods();
            for (Method method :declaredMethods) {
                System.out.println("User 方法:"+method.getName());
            }
    
            // 获取注解
            Annotation[] annotations = userClazz.getAnnotations();
            for (Annotation annotation :annotations) {
                System.out.println("User 注解:"+annotation.toString());
            }
    
            // 为属性赋值——使用字段名称
            Field field1 = userClazz.getDeclaredField("address");
            field1.setAccessible(true);
            field1.set(user,"杭州");
            // 获取属性——使用字段名称
            Object address1 = field1.get(user);
            System.out.println("User 属性-字段名:"+address1);
    
            // 为属性赋值——使用set方法
            Method setAddressMethod = userClazz.getDeclaredMethod("setAddress",String.class);
            setAddressMethod.invoke(user,"西安");
            Method getAddressMethod = userClazz.getDeclaredMethod("getAddress");
            Object address2 = getAddressMethod.invoke(user);
            System.out.println("User 属性-set方法:"+address2);
    
            // 执行某个方法-有参
            Method helloMethod = userClazz.getDeclaredMethod("hello", String.class);
            Object helloResult = helloMethod.invoke(user,"rangers");
            System.out.println("User 执行hello方法-有参:"+helloResult);
    
            // 执行某个方法-有参
            Method sayMethod = userClazz.getDeclaredMethod("say");
            Object sayResult = sayMethod.invoke(user);
            System.out.println("User 执行say方法-无参:"+sayResult);
    
        }
    }
    
    
原文地址:https://www.cnblogs.com/rangers-sun/p/14507315.html