ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
          對(duì)反射API的訪問(wèn)由安全管理器所控制.Field,Method和Constructor類都是由一個(gè)名為AccessibleObject的基類擴(kuò)展的.AccessibleObject類有一個(gè)主要的方法,名為setAccessible(),由此可以在訪問(wèn)特定的類成員時(shí)解除平常所設(shè)定的安全性.Javadoc說(shuō)明如下:
          setAccessible

          public void setAccessible(boolean flag)
                             throws SecurityException

              Set the accessible flag for this object to the indicated boolean value. A value of true indicates that the reflected object should suppress Java language access checking when it is used. A value of false indicates that the reflected object should enforce Java language access checks.

              First, if there is a security manager, its checkPermission method is called with a ReflectPermission("suppressAccessChecks") permission.

              A SecurityException is raised if flag is true but accessibility of this object may not be changed (for example, if this element object is a Constructor object for the class Class).

              A SecurityException is raised if this object is a Constructor object for the class java.lang.Class, and flag is true.

              Parameters:
                  flag - the new value for the accessible flag
              Throws:
                  SecurityException - if the request is denied.
              See Also:
                  SecurityManager.checkPermission(java.security.Permission), RuntimePermission

          Class類提供了兩組方法來(lái)得到每一種特性.其中一組允許訪問(wèn)類的公共特性(其中包括由其超類所繼承得到的成員),而另一組則允許訪問(wèn)在類中直接聲明的任何公共或非公共成員(而不包括繼承得來(lái)的成員),這要取決于有何安全性考慮.以下是一些例子:
          .getFields()將返回一個(gè)Field對(duì)象數(shù)組,它表示一個(gè)類的所有公共變量,其中包括繼承得到的公共變量.
          .getDeclareFields()將返回一個(gè)數(shù)組,以表示類中聲明的所有變量,而不論其訪問(wèn)修飾符如何(不包括安全管理器不允許看到的變量),但是不包括繼承得到的變量.
          .對(duì)于構(gòu)造函數(shù),"所有構(gòu)造函數(shù)"和"所聲明構(gòu)造函數(shù)"之間無(wú)所謂差別(類不會(huì)繼承構(gòu)造函數(shù)),因此getConstructors()和getDeclaredConstructors()的唯一區(qū)別在于,前者返回的是公共構(gòu)造函數(shù),而后者則返回類的所有構(gòu)造函數(shù).
          posted on 2005-08-04 14:45 ivaneeo 閱讀(351) 評(píng)論(0)  編輯  收藏 所屬分類: java魔力
          主站蜘蛛池模板: 夏津县| 峡江县| 马尔康县| 江陵县| 西昌市| 宁化县| 龙山县| 手游| 新化县| 富源县| 北京市| 津南区| 白城市| 丁青县| 孟连| 蓝田县| 马山县| 江阴市| 祁门县| 苗栗市| 澎湖县| 竹北市| 新闻| 加查县| 五大连池市| 余干县| 永福县| 衡山县| 正定县| 上蔡县| 大城县| 山阳县| 阳曲县| 张家界市| 嘉义县| 阳原县| 承德县| 桐庐县| 泰来县| 化德县| 成安县|