日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA的内省机制(introspector)与反射机制(reflection)

發布時間:2024/4/17 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA的内省机制(introspector)与反射机制(reflection) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://yangzhibin-java.iteye.com/blog/452068

JAVA的內省機制(introspector)與反射機制(reflection)

  • 博客分類:
  • j2se
JavaBeanJ#

相對而言,反射比內省更容易理解一點。用一句比較白的話來概括,反射就是讓你可以通過名稱來得到對象 ( 類,屬性,方法 ) 的技術,這種技術比內省機制使用范圍更廣泛。例如我們可以通過類名來生成一個類的實例;知道了方法名,就可以調用這個方法;知道了屬性名就可以訪問這個屬性的值。 內省是 Java 語言對 Bean 類屬性、事件的一種缺省處理方法。例如類 A 中有屬性 name, 那我們可以通過 getName,setName 來得到其值或者設置新的值。通過 getName/setName 來訪問 name 屬性,這就是默認的規則。 Java 中提供了一套 API 用來訪問某個屬性的 getter/setter 方法,通過這些 API 可以使你不需要了解這個規則(但你最好還是要搞清楚),這些 API 存放于包 java.beans 中。
一般的做法是通過類 Introspector 來獲取某個對象的 BeanInfo 信息,然后通過 BeanInfo 來獲取屬性的描述器( PropertyDescriptor ),通過這個屬性描述器就可以獲取某個屬性對應的 getter/setter 方法,然后我們就可以通過反射機制來調用這些方法。下面我們來看一個例子,這個例子把某個對象的所有屬性名稱和值都打印出來:

Java代碼 ?
  • package?MyTest; ??
  • public?class?bean?{ ??
  • ????????private?String?id?=?null?; ??
  • ????????private?String?name?=?null?; ??
  • ??
  • ????????public?String?getId()?{ ??
  • ????????????????return?id; ??
  • ????????} ??
  • ????????public?void?setId(String?id)?{ ??
  • ????????????????this.id?=?id; ??
  • ????????} ??
  • ??
  • ????????public?String?getName()?{ ??
  • ????????????????return?name; ??
  • ????????} ??
  • ????????public?void?setName(String?name)?{ ??
  • ????????????????this.name?=?name; ??
  • ????????} ??
  • } ??
  • ??
  • package?MyTest; ??
  • import?java.beans.BeanInfo; ??
  • import?java.beans.EventSetDescriptor; ??
  • import?java.beans.Introspector; ??
  • import?java.beans.MethodDescriptor; ??
  • import?java.beans.PropertyDescriptor; ??
  • import?java.lang.reflect.Method; ??
  • public?class?myBeanIntrospector?{ ??
  • public?myBeanIntrospector()??? ??
  • ??{??? ??
  • ???try?? ??
  • ???{??? ??
  • ????//實例化一個Bean??? ??
  • ????bean?beanObj?=?new?bean();??? ??
  • ????//依據Bean產生一個相關的BeanInfo類??? ??
  • ????BeanInfo?bInfoObject?=???? ??
  • ????Introspector.getBeanInfo(beanObj.getClass(),beanObj.getClass().getSuperclass());??? ??
  • ????//定義一個用于顯示的字符串??? ??
  • ????String?output?=?"";??? ??
  • ??? ??
  • ????//開始自省??? ??
  • ????? ??
  • ????/*?? ?
  • ????*?BeanInfo.getMethodDescriptors()?? ?
  • ????*?用于獲取該Bean中的所有允許公開的成員方法,以MethodDescriptor數組的形式返回?? ?
  • ????*?? ?
  • ????*?MethodDescriptor類?? ?
  • ????*?用于記載一個成員方法的所有信息?? ?
  • ????*?MethodDescriptor.getName()?? ?
  • ????*?獲得該方法的方法名字?? ?
  • ????*?MethodDescriptor.getMethod()?? ?
  • ????*?獲得該方法的方法對象(Method類)?? ?
  • ????*?? ?
  • ????*?Method類?? ?
  • ????*?記載一個具體的的方法的所有信息?? ?
  • ????*?Method.getParameterTypes()?? ?
  • ????*?獲得該方法所用到的所有參數,以Class數組的形式返回?? ?
  • ????*?? ?
  • ????*?Class..getName()?? ?
  • ????*?獲得該類型的名字?? ?
  • ????*/?? ??
  • ????output?=?"內省成員方法:\n";??? ??
  • ????MethodDescriptor[]?mDescArray?=?bInfoObject.getMethodDescriptors();??? ??
  • ????for?(int?i=0;i<mDescArray.length?;i++?)??? ??
  • ????{??? ??
  • ?????//獲得一個成員方法描述器所代表的方法的名字??? ??
  • ?????String?methodName?=?mDescArray[i].getName();??? ??
  • ???????? ??
  • ?????String?methodParams?=?new?String();??? ??
  • ?????//獲得該方法對象??? ??
  • ?????Method?methodObj?=?mDescArray[i].getMethod();??? ??
  • ?????//通過方法對象獲得該方法的所有參數,以Class數組的形式返回??? ??
  • ?????Class[]?parameters?=?methodObj.getParameterTypes();??? ??
  • ?????if?(parameters.length>0)??? ??
  • ?????{??? ??
  • ??????//獲得參數的類型的名字??? ??
  • ??????methodParams?=?parameters[0].getName();??? ??
  • ??????for?(int?j=1;j<parameters.length?;j++?)??? ??
  • ??????{??? ??
  • ???????methodParams?=?methodParams?+?","?+?parameters[j].getName();??? ??
  • ??????}??? ??
  • ?????}??? ??
  • ?????output?+=?methodName?+?"("?+?methodParams?+?")\n";??? ??
  • ????}??? ??
  • ????System.out.println(output);??? ??
  • ????? ??
  • ????/*?? ?
  • ????*?BeanInfo.getPropertyDescriptors()?? ?
  • ????*?用于獲取該Bean中的所有允許公開的成員屬性,以PropertyDescriptor數組的形式返回?? ?
  • ????*?? ?
  • ????*?PropertyDescriptor類?? ?
  • ????*?用于描述一個成員屬性?? ?
  • ????*?? ?
  • ????*?PropertyDescriptor.getName()?? ?
  • ????*?獲得該屬性的名字?? ?
  • ????*?? ?
  • ????*?PropertyDescriptor.getPropertyType()?? ?
  • ????*?獲得該屬性的數據類型,以Class的形式給出?? ?
  • ????*?? ?
  • ????*/?? ??
  • ????output?=?"內省成員屬性:\n";??? ??
  • ????PropertyDescriptor[]?mPropertyArray?=?bInfoObject.getPropertyDescriptors();??? ??
  • ????for?(int?i=0;i<mPropertyArray.length?;i++?)??? ??
  • ????{??? ??
  • ?????String?propertyName?=?mPropertyArray[i].getName();??? ??
  • ?????Class?propertyType?=?mPropertyArray[i].getPropertyType();??? ??
  • ?????output?+=?propertyName?+?"?(?"?+?propertyType.getName()?+?"?)\n";??? ??
  • ????}??? ??
  • ????System.out.println(output);??? ??
  • ????? ??
  • ??? ??
  • ????/*?? ?
  • ????*?BeanInfo.getEventSetDescriptors()?? ?
  • ????*?用于獲取該Bean中的所有允許公開的成員事件,以EventSetDescriptor數組的形式返回?? ?
  • ????*?? ?
  • ????*?EventSetDescriptor類?? ?
  • ????*?用于描述一個成員事件?? ?
  • ????*?? ?
  • ????*?EventSetDescriptor.getName()?? ?
  • ????*?獲得該事件的名字?? ?
  • ????*?? ?
  • ????*?EventSetDescriptor.getListenerType()?? ?
  • ????*?獲得該事件所依賴的事件監聽器,以Class的形式給出?? ?
  • ????*?? ?
  • ????*/?? ??
  • ????output?=?"內省綁定事件:\n";??? ??
  • ????EventSetDescriptor[]?mEventArray?=?bInfoObject.getEventSetDescriptors();??? ??
  • ????for?(int?i=0;i<mEventArray.length?;i++?)??? ??
  • ????{??? ??
  • ?????String?EventName?=?mEventArray[i].getName();??? ??
  • ?????Class?listenerType?=?mEventArray[i].getListenerType();??? ??
  • ?????output?+=?EventName?+?"("?+?listenerType.getName()?+?")\n";??? ??
  • ????}??? ??
  • ????System.out.println(output);??? ??
  • ????System.out.println("write?by?esonghui?:");??? ??
  • ????? ??
  • ???}??? ??
  • ???catch?(Exception?e)??? ??
  • ???{??? ??
  • ????System.out.println("異常:"?+?e);??? ??
  • ???}??? ??
  • ??}??? ??
  • ??public?static?void?main(String[]?args)???? ??
  • ??{??? ??
  • ???new?myBeanIntrospector();??? ??
  • ??}??? ??
  • }??
  • 總結

    以上是生活随笔為你收集整理的JAVA的内省机制(introspector)与反射机制(reflection)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。