• Introspector内省和反射的区别.


    Introspector 是一个专门处理bean的工具类.用来获取Bean体系里的 propertiesDescriptor,methodDescriptor.

    要理解这个,就要理解下面几个议题.

     
    *bean是啥?
         普通的class 可能有 computerAges(){ }等方法.
        Bean是 一个field ,有 get 或者set. 除了这些别无其他.
         bean是class的一种
        例如 public class People {
                String name;
                public String getName(){
                }
                public void setName(String name){
                }
            }
    *Bean在jdk里对应的的概念
         BeanInfo , 他包含了Bean所有的descriptor(描述符) .
              BeanDescriptor PropertiesDescriptor MethodDescriptor
     
    *  一个类的属性field 和 propertiesDescriptor(描述)有什么区别.
       propertiesDescriptor,它来至于 对Method的解析.
        如果是严格的Bean.例如上面的People. field一个叫做name, propertiesDescriptor 只有一个,刚好也是name, 来自set和get的解析, 解析出来都是       name.,所有两个merge为一个. 
      
     
    详细逻辑见Introspector中代码.见附件
     
    *  反射的method和bean概念体系里的methodDescriptor的区别
          2:1的对应关系. People里有set和get两个方法,反射得到两个Method,但这两个method会组合成一个MethodDescriptor. 
     
    *  Introspector内省 和 反射的区别和关系?
        Introspector 是一个专门处理bean的工具类.用来获取Bean体系里的 propertiesDescriptor,methodDescriptor.
        利用反射获取Method信息,是反射的上层. 
        性能优化: 只进行一次反射解析. 通过WeakReference静态类级别缓存Method, 在jvm不够时会被回收.  
            // Static Caches to speed up introspection.
        private static Map declaredMethodCache = Collections.synchronizedMap(new WeakHashMap());
     
     
    附件1:
    解析method得到properties,并且合并同名的properties.
    把 method根据 解析出的properties放入的map中,将 setMethod和 getMethod合并成一个 methodDescriptor
     
    见 Introspector.java的
         /**
         * Populates the property descriptor table by merging the
         * lists of Property descriptors.
         */
        private void processPropertyDescriptors() { 
                            ...
                // Complete simple properties set 
                 pd = mergePropertyDescriptor(gpd, spd); //merge get方法解析出的gpd和set方法解析出的spd . 一个PropertyDescriptor里面有两个属性,一个是setMethodName,一个是getMethodName.
                     
                        ....
                 properties.put(pd.getName(), pd);  
     
            } 
     
    /**
         * Adds the property descriptor to the indexedproperty descriptor only if the
         * types are the same.
         *
         * The most specific property descriptor will take precedence.
         */
        private PropertyDescriptor mergePropertyDescriptor(IndexedPropertyDescriptor ipd,
                                                           PropertyDescriptor pd) {  
    }
    PropertyDescriptor里的 private Reference<Class> propertyTypeRef; 里的值决定了type, 距离, int string等类型.
  • 相关阅读:
    转: CvMat,Mat和IplImage之间的转化和拷贝
    Scrapy 轻松定制网络爬虫
    榆林暑期见习
    文学
    浅析Redis实现lock互斥访问资源
    使用Maven构建Java项目
    正则表达式
    CSS 命名规范及标题供参考与学习
    从零开始在linux下搭建wordpress博客
    python2和python3的区别
  • 原文地址:https://www.cnblogs.com/zjj1996/p/9139700.html
Copyright © 2020-2023  润新知