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());
内省(IntroSpector)是Java语言对JavaBean 类属性、事件的一种处理方法。
例如类A中有属性name, 那我们可以通过getName,setName 来得到其值或者设置新的值。
通过getName/setName 来访问name属性,这就是默认的规则。
Java中提供了一套API 用来访问某个属性的getter/setter方法,通过这些API 可以使你不需要了解这个规则,这些API存放于包java.beans 中。
一般的做法是通过类Introspector的getBeanInfo方法获取某个对象的BeanInfo 信息,然后通过BeanInfo来获取属性的描述器(PropertyDescriptor),通过这个属性描述器就可以获取某个属性对应的getter/setter方法,然后我们就可以通过反射机制来调用这些方法。
我们又通常把javabean的实例对象称之为值对象(Value Object),因为这些bean中通常只有一些信息字段和存储方法,没有功能性方法。
一个JavaBean类可以不当JavaBean用,而当成普通类用。JavaBean实际就是一种规范,当一个类满足这个规范,这个类就能被其它特定的类调用。一个类被当作javaBean使用时,JavaBean的属性是根据方法名推断出来的,它根本看不到java类内部的成员变量(即便没有属性也没关系,IntroSpector是假设性的操作)。去掉set前缀,然后取剩余部分,如果剩余部分的第二个字母是小写的,则把剩余部分的首字母改成小的。
除了反射用到的类需要引入外,内省需要引入的类如下所示,它们都属于java.beans包中的类,自己写程序的时候也不能忘了引入相应的包或者类。下面代码片断是设置某个JavaBean类某个属性的关键代码:
import java.beans.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by wangpl on 1/16/15.
*/
public class Test {
public static void main(String[] args) throws IntrospectionException, InvocationTargetException,
IllegalAccessException {
User user =new User("zhangsan", 21);
String propertyName = "name";
PropertyDescriptor pd=new PropertyDescriptor(propertyName, user.getClass());// 直接指定要访问的属性
Method readMethod = pd.getReadMethod();//获取到读方法
Object invoke = readMethod.invoke(user, null);// 反射机制调用
System.out.println("名字:" + invoke);
pd.getWriteMethod().invoke(user, "lisi");
invoke = readMethod.invoke(user, null);
System.out.println("名字:" + invoke);
// 获取整个Bean的信息
// BeanInfo beanInfo= Introspector.getBeanInfo(user.getClass());
BeanInfo beanInfo= Introspector.getBeanInfo(user.getClass(), Object.class);// 在Object类时候停止检索,可以选择在任意一个父类停止
System.out.println("所有属性描述:");
PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();// 获取所有的属性描述
for (PropertyDescriptor propertyDescriptor: pds) {
System.out.println(propertyDescriptor.getName());
}
System.out.println("所有方法描述:");
for (MethodDescriptor methodDescriptor: beanInfo.getMethodDescriptors()) {
System.out.println(methodDescriptor.getName());
// Method method = methodDescriptor.getMethod();
}
}
}
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
附件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等类型.