Java Reference
In-Depth Information
16.5. Access Checking and
AccessibleObject
The classes
Field
,
Constructor
, and
Method
are also all subclasses of the
class
AccessibleObject
, which lets you enable or disable the checking
of the language-level access modifiers, such as
public
and
private
.
Normally, attempts to use reflection to access a member are subject to
the same access checks that would be required for regular, explicit code-
for example, if you cannot access a field directly in code, you cannot ac-
cess it indirectly via reflection. You can disable this check by invoking
setAccessible
on the object with a value of
TRue
the object is now access-
ible, regardless of language-level access control. This would be required,
for example, if you were writing a debugger. The methods are:
public void
setAccessible(boolean flag)
Sets the accessible flag for this object to the indicated boolean
value. A value of
true
means that the object should suppress
language-level access control (and so will always be access-
ible);
false
means the object should enforce language-level ac-
cess control. If you are not allowed to change the accessibility
of an object a
SecurityException
is thrown.
public static void
setAccessible(AccessibleObject[] array, boolean
flag)
A convenience method that sets the accessible flag for an array
of objects. If setting the flag of an object throws a
SecurityEx-
ception
only objects earlier in the array will have their flags set
to the given value, and all other objects are unchanged.
public boolean
isAccessible()
Returns the current value of the accessible flag for this object.