Set the
accessible
flag for this reflected object to
true
if possible. This method sets the
accessible
flag, as if by invoking
setAccessible(true)
, and returns the possibly-updated value for the
accessible
flag. If access cannot be enabled, i.e. the checks or Java language access control cannot be suppressed, this method returns
false
(as opposed to
setAccessible(true)
throwing
InaccessibleObjectException
when it fails).
This method is a no-op if the accessible
flag for this reflected object is true
.
For example, a caller can invoke trySetAccessible
on a Method
object for a private instance method p.T::privateMethod
to suppress the checks for Java language access control when the Method
is invoked. If p.T
class is in a different module to the caller and package p
is open to at least the caller's module, the code below successfully sets the accessible
flag to true
.
p.T obj = ....; // instance of p.T
:
Method m = p.T.class.getDeclaredMethod("privateMethod");
if (m.trySetAccessible()) {
m.invoke(obj);
} else {
// package p is not opened to the caller to access private member of T
...
}
If this method is invoked by JNI code with no caller class on the stack, the accessible
flag can only be set if the member and the declaring class are public, and the class is in a package that is exported unconditionally.
If there is a security manager, its checkPermission
method is first called with a ReflectPermission("suppressAccessChecks")
permission.
false
on a reflected object that is accessible to the caller. To test if this reflected object is accessible, it should usecanAccess(Object)
.