class A {
}
}
注意:
setAccessible(true) 并不是将方法的访问权限改成了public,而是取消java的权限控制检查。所以即使是public方法,其accessible 属相默认也是false
JDK 文档:
setAccessible
public void setAccessible(booleanflag) 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
, andflag
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
-