Java內省(基礎一)

程序代碼:內省Person類。

package neixing;

public class Person {

 private String name;
 private String password;
 private int age;
 private String province;// 這個不是Person的屬性,因爲他並沒有提供相應 的get或者set方法。

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getAddress() {
  System.out.println();
  return "";
 }

 // 只要Person類提供了相應的get或set方法(get
 // 方法有返回值,set方法接收傳入的參數),那麼對應的這個就是他的屬性,比如說上面的那個getAddress方法也是他的屬性
 // 因爲Person類繼承自object,object有一個getClass方法,所以Person類共有5個屬性

}
----------------------------------
測試代碼:


package neixing;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.junit.Test;

public class Demo1 {
 // 內省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方法決定的

  }

 }

 @Test
 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到底有多少屬性

   // 打印結果表明不再內省父類Object 的Class屬性
   // 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)]

  }

 }

 @Test
 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
 }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章