程序代碼:內省Person類。
private String password;
private int age;
private String province;// 這個不是Person的屬性,因爲他並沒有提供相應 的get或者set方法。
return name;
}
this.name = name;
}
return password;
}
this.password = password;
}
return age;
}
this.age = age;
}
System.out.println();
return "";
}
// 方法有返回值,set方法接收傳入的參數),那麼對應的這個就是他的屬性,比如說上面的那個getAddress方法也是他的屬性
// 因爲Person類繼承自object,object有一個getClass方法,所以Person類共有5個屬性
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
// 內省APi操作Bean屬性
@Test
public void test1() throws IntrospectionException {
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();// 得到屬性描述器
for (PropertyDescriptor pd : pds) {
System.out.println(pd);// 獲知Bean到底有多少屬性
// 程序輸出一下內容,共四個屬性
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=address; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getAddress()]
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=class; propertyType=class
// java.lang.Class; readMethod=public final native java.lang.Class
// java.lang.Object.getClass()]
// java.beans.PropertyDescriptor[name=name; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getName(); writeMethod=public void
// neixing.Person.setName(java.lang.String)]
// java.beans.PropertyDescriptor[name=password; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getPassword(); writeMethod=public void
// neixing.Person.setPassword(java.lang.String)]
// 這個說明了屬性是由get或者set方法決定的
public void test2() throws Exception {
// ------上面中class屬性是父類的屬性,只是子類繼承了object的getClass()方法。如果只想獲取Bean自己的屬性,那該怎麼做??
// Open Declaration BeanInfo
// java.beans.Introspector.getBeanInfo(Class<?> beanClass, Class<?>
// stopClass) throws IntrospectionException
// 其中Class<?> stopClass表示不再內省這個類的繼承樹種包括這個類以及這個類的父類的所有屬性。
BeanInfo beanInfo = Introspector
.getBeanInfo(Person.class, Object.class);
PropertyDescriptor[] pds2 = beanInfo.getPropertyDescriptors();// 得到屬性描述器
for (PropertyDescriptor pd : pds2) {
System.out.println(pd);// 獲知Bean到底有多少屬性
// java.beans.PropertyDescriptor[name=address; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getAddress()]
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=class; propertyType=class
// java.lang.Class; readMethod=public final native java.lang.Class
// java.lang.Object.getClass()]
// java.beans.PropertyDescriptor[name=name; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getName(); writeMethod=public void
// neixing.Person.setName(java.lang.String)]
// java.beans.PropertyDescriptor[name=password; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getPassword(); writeMethod=public void
// neixing.Person.setPassword(java.lang.String)]
public void test3() throws Exception {
Person p = new Person();
// 操作age屬性
PropertyDescriptor pdDescriptor = new PropertyDescriptor("age",
Person.class);// 第一個是屬性名
// 這樣pdDescriptor就專門用來操作age屬性.操作無非就是寫數據和讀數據
Method method = pdDescriptor.getWriteMethod();// ---->public void
// setAge(int age);
method.invoke(p, 45);
System.out.println(p.getAge());// 成功打印
// 獲取屬性的值
Method methodRead = pdDescriptor.getReadMethod();// ---->public void
// getAge()
int ageRead = (Integer) methodRead.invoke(p, null);// get方法不需要接收參數
System.out.println(ageRead);
}
* 獲取當前屬性的類型
*
* @throws IntrospectionException
*
*/
@Test
public void test4() throws IntrospectionException {
// 操作age屬性
PropertyDescriptor pdDescriptor = new PropertyDescriptor("age",
Person.class);// 第一個是屬性名
// 查看age屬性的類型
Class classz = pdDescriptor.getPropertyType();
System.out.println(classz);// 打印:int
}