1. 内省(Introspector)概念

    ​ 内省Introspector 是Java提供的操作 JavaBean 的 API,用来访问某个属性的 getter/setter 方法。对于一个标准的 JavaBean 来说,它包括属性、get 方法和 set 方法,这是一个约定俗成的规范。为此 sun 提供了 Introspector 工具包,来使开发者更好或者更灵活的操作 JavaBean。

    例如:User类中有个name属性,那我们可以通过getName/setName来获取/设置name的值,内省就是通过Java提供的API访问属性的getName()/setName()方法。

  2. 内省与反射的区别

    ​ 在计算机科学中,内省是指计算机程序在运行时(Run time)检查对象(Object)类型的一种能力,通常也可以称作运行时类型检查。 不应该将内省和反射混淆。相对于内省,反射更进一步,是指计算机程序在运行时(Run time)可以访问、检测和修改它本身状态或行为的一种能力。

  3. 内省常用类说明

    • Introspector 类提供了的 getBeanInfo()方法获取BeanInfo对象,可以拿到一个 JavaBean 的所有信息
    • BeanInfo 通过getPropertyDescriptors() 方法和 getMethodDescriptors()方法可以获取到PropertyDescriptors、MethodDescriptors对象
    • MethodDescriptor 类可以获得方法的元信息,比如方法名,参数个数,参数字段类型等
      • getMethod()获取方法的Method对象
      • getParameters() 获取方法的所有参数Parameter列表
      • getParameterTypes()获取方法的参数ParameterType列表
    • PropertyDescriptor 类的主要方法
      • getPropertyType(),获得属性的Class对象
      • getReadMethod()/getWriteMethod(),获得用于读取/写入属性值的方法
        • setReadMethod(Method readMethod)/setWriteMethod(Method writeMethod),设置用于读取/写入属性值的方法
  4. 代码演示

    • MethodDescriptor 详解

      // 使用Introspector获取BeanInfo对象
      BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
      // 使用BeanInfo对象获取到MethodDescriptor列表
      MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
      for (MethodDescriptor methodDescriptor : methodDescriptors) {
      // 获取方法Method对象
      Method method = methodDescriptor.getMethod();
      // 获取方法名称
      String methodName = method.getName(); // 参数类型列表
      List<String> parameterTypeNames = new ArrayList<>();
      Class<?>[] parameterTypes = method.getParameterTypes();
      if (parameterTypes!=null && parameterTypes.length>0){
      Arrays.stream(parameterTypes).forEach(x-> {
      String name = x.getName();
      parameterTypeNames.add(name);
      });
      } // 获取方法参数Parameter数组
      Parameter[] parameters = method.getParameters();
      // 参数名称列表
      List<String> parameterNames = new ArrayList<>();
      if (parameters!=null && parameters.length>0){
      parameterNames = Arrays.stream(parameters).map(Parameter::getName).collect(Collectors.toList());
      } System.out.println("方法名称:"+methodName +" 参数类型列表:"+parameterTypeNames+" 参数名称列表:"+parameterNames);
    • PropertyDescriptors详解

      // 使用Introspector获取BeanInfo对象
      BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
      // 使用BeanInfo对象获取到PropertyDescriptor列表
      PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
      for (PropertyDescriptor propertyDescriptor :propertyDescriptors) {
      // 获取属性名称
      String name = propertyDescriptor.getName();
      if ("class".equals(name)){
      continue;
      }
      // 获取setter方法
      Method writeMethod = propertyDescriptor.getWriteMethod();
      // 获取getter方法
      Method readMethod = propertyDescriptor.getReadMethod();
      System.out.println("属性名称:"+name+
      " 赋值方法:"+writeMethod.getName()+
      " 获取方法:"+readMethod.getName());
      } // 利用反射创建对象
      User user = User.class.newInstance();
      // 创建PropertyDescriptor对象
      PropertyDescriptor propertyDescriptor = new PropertyDescriptor("name",User.class); // 获取写入方法并执行
      Method writeMethod = propertyDescriptor.getWriteMethod();
      writeMethod.invoke(user,"Rangers"); // 获取读取方法并执行
      Method readMethod = propertyDescriptor.getReadMethod();
      Object readPropertyValue = readMethod.invoke(user);
      System.out.println("ReadMethod获取到到属性值:"+readPropertyValue);
      System.out.println("getter获取到到属性值:"+user.getName()); // 设置写入方法并执行
      Method writeMd = User.class.getDeclaredMethod("setOoo",String.class);
      propertyDescriptor.setWriteMethod(writeMd);
      Method writeMethodAgain = propertyDescriptor.getWriteMethod();
      writeMethodAgain.invoke(user, "ooo");
      System.out.println("设置写入方法重新赋值:"+user.getName());

内省详解(Introspector/BeanInfo/MethodDescriptor/PropertyDescriptor)的更多相关文章

  1. Java内省详解

    内省和反射有什么区别: 反射式在运行状态把Java类中的各种成分映射成相应的Java类,可以动态的获取所有的属性以及动态调用任意一个方法,强调的是运行状态.  内省机制是通过反射来实现的,BeanIn ...

  2. Java基础-反射(reflect)技术详解

    Java基础-反射(reflect)技术详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.类加载器 1>.JVM 类加载机制  如下图所示,JVM类加载机制分为五个部分 ...

  3. Spring之IOC原理及代码详解

    一.什么是IOC 引用 Spring 官方原文:This chapter covers the Spring Framework implementation of the Inversion of ...

  4. 常见 jar包详解

        常见 jar包详解 jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周期 ...

  5. MyBatis Generator 详解

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  6. MyBatis Generator 详解 【转来纯为备忘】

    版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com   目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...

  7. [转]application.properties详解 --springBoot配置文件

    本文转载:http://blog.csdn.net/lpfsuperman/article/details/78287265###; # spring boot application.propert ...

  8. application.properties详解 --springBoot配置文件【转载】

    # spring boot application.properties配置的各个属性详解 # 该示例文件作为标准提供.(官方文档 翻译过来的) # 还是花了些功夫翻译,各位如果转发,请留下本文地址, ...

  9. 类名.class 与 类名.this 详解

    类名.class 与 类名.this 详解 今天在看 PropertyPlaceholderConfigurer 源码时,突然看到一个 PropertyPlaceholderConfigurer.th ...

随机推荐

  1. 脚本化CSS(通过JS来间接操作CSS)

    (一)通过.style.形式,获取的是行间样式,可读可写 1.行间样式语法 1 <div style="width:100px;border:5px solid red;height: ...

  2. JavaScript基本包装类介绍

    为了便于操作基本类型值,ECMAScript 提供了 3 个特殊的引用类型:Boolean.Number和 String.这些类型与其他引用类型相似,但同时也具有与各自的基本类型相应的特殊行为.实际上 ...

  3. macOS & pbcopy

    macOS & pbcopy copy from command line pbcopy $ whoami | pbcopy # xgqfrms-mbp $ echo "hello, ...

  4. iPhone 12 导入通讯录排序 Bug

    iPhone 12 导入通讯录排序 Bug iOS iOS 通讯录排序问题 Huawei OK solution iOS 切换中英文,修复排序通讯录 bug Awesome iOS Contacts ...

  5. node.js module.exports & exports & module.export all in one

    node.js module.exports & exports & module.export all in one cjs const log = console.log; log ...

  6. rename github

    rename GitHub github repo rename xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  7. How to using PyPI publish a Python package

    How to using PyPI publish a Python package PyPI & Python package https://pypi.org/ main make a f ...

  8. GitHub for VSCode

    GitHub for VSCode A first-party GitHub OAuth application (GitHub for VSCode) with repo and workflow ...

  9. flex & flex-wrap

    flex & flex-wrap https://css-tricks.com/almanac/properties/f/flex-wrap/ https://developer.mozill ...

  10. GitHub & GitHub Package Registry

    GitHub & GitHub Package Registry npm https://github.blog/2019-05-10-introducing-github-package-r ...