對(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ù).
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ù).