内省(Introspector
)是Java
语言对JavaBean
类属性、事件的一种缺省处理方法。JavaBean
是一种特殊的类,遵守JavaBean
的规范,即提供默认构造方法,同时所有的属性都是私有的,且每个属性都有公开的读取和设定的方法(getter
和setter
),这些方法都遵守命名的规范。例如类Person
中有属性name
,可以通过getName/setName
来得到其值或者设置新的值。
Person
类:
package com.sigalhu;
public class Person {
private String name;
private String sex;
private int age;
public Person() {
this.name = "SigalHu";
this.sex = "男";
this.age = 26;
}
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name=" + name +
", sex=" + sex +
", age=" + age +
'}';
}
}
首先通过Introspector
类来获取某个对象的BeanInfo
信息,然后通过BeanInfo
来获取属性的描述器PropertyDescriptor
,通过PropertyDescriptor
就可以获取某个属性对应的getter/setter
方法,因此就可以通过反射机制来调用这些方法。此外,还可以通过构造方法直接创建PropertyDescriptor
对象来对某个对象的属性进行相应操作。
PropertyDescriptor
类的主要方法有:
* getPropertyType()
:获得属性的Class
对象;
* getReadMethod()
:获得用于读取属性值的方法;
* getWriteMethod()
:获得用于写入属性值的方法;
* hashCode()
:获取对象的哈希值;
* setReadMethod(Method readMethod)
:设置用于读取属性值的方法;
* setWriteMethod(Method writeMethod)
:设置用于写入属性值的方法。
Main
类:
package com.sigalhu;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class Main {
static public void main(String[] args) throws Exception{
Person person = new Person("a","男",25);
//如果不想把父类的属性也列出来的话,getBeanInfo的第二个参数填写父类的信息
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class);
//读取所有属性值
PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor prop:props){
System.out.println(prop.getName() + ": " + prop.getReadMethod().invoke(person));
}
System.out.println();
//写入指定属性值
PropertyDescriptor propName = new PropertyDescriptor("name",Person.class);
propName.getWriteMethod().invoke(person,"b");
System.out.println(person);
System.out.println();
//设置用于写入属性值的方法
PropertyDescriptor propSex = new PropertyDescriptor("sex",Person.class);
propName.setWriteMethod(propSex.getWriteMethod());
propName.getWriteMethod().invoke(person,"c");
System.out.println(person);
}
}
运行结果:
age: 25
name: a
sex: 男
Person{name=b, sex=男, age=25}
Person{name=b, sex=c, age=25}
参考链接
Java 内省机制
java的内省机制
Java内省机制小总结
《JavaBean》-Java的内省机制
Java bean 是个什么概念?
Java内省机制