Java reflection calls private domain and method (setaccessible)

Source: Internet
Author: User
Tags object serialization

Java reflection calls private domain and method (setaccessible)

@author Ixenos

AccessibleObject class

The method, field, and constructor classes collectively inherit the AccessibleObject class, which has two setaccessible methods that suppress Java language access control checks at run time (Java language access Control checks), which enables arbitrary invocation of privatized protected methods, domains, and construction methods

accessibleobjectextends Objectimplements annotatedelement

The AccessibleObject class is the base class for Field, Method, and Constructor objects. It provides the ability to mark reflected objects as being used to cancel the default Java language access control check. For public members, default (packaged) Access members, protected members, and private members, access checks are performed when you use field, method, or Constructor objects to set or get fields, invoke methods, or create and initialize new instances of a class.

Setting the accessible flag in a reflection object allows complex applications with sufficient privileges (such as Java object serialization or other persistence mechanisms) to manipulate objects in a way that is generally forbidden.

Two setaccessible methods to set access rights

static void setAccessible(AccessibleObject[] array, boolean flag)
A convenient way to set the accessible flag for a set of objects using a single security check (for efficiency).
 void setAccessible(boolean flag)
Sets the accessible flag of this object to the indicated Boolean value.

Setaccessible
setaccessible (accessibleobject[] Array,                                 Boolean flag)                          throws SecurityException
A
convenient way to set the accessible flag for a set of objects using a single security check (for efficiency).

First, if there is a security manager, the ReflectPermission("suppressAccessChecks") permission is lowered by the checkPermission method.

If it is flag true , but cannot change the accessibility of any of the input array elements (for example, if the element object is an Class object of the class Constructor ), it is raised SecurityException . If SecurityException occurs, the accessibility of an object can be set to an array element that is less than (not included) an element that has an exception, and flag for those elements that exceed (including) the element that throws the exception, do not change its accessibility.

Parameters:
array -accessibleobjects array
flag -The new value of the accessible flag in each object
Thrown:
SecurityException-If the request is denied.
See also:
SecurityManager.checkPermission(java.security.Permission), RuntimePermission

Setaccessible
setaccessible (Boolean flag)                   Throws SecurityException
Sets the accessible flag of this object to the indicated Boolean value. A value of true indicates that the reflected object should cancel the Java language access check when it is used. A value of false indicates that the reflected object should implement a Java language access check.

First, if there is a security manager, the ReflectPermission("suppressAccessChecks") permission is lowered by the checkPermission method.

If it is flag true , and cannot change the accessibility of this object (for example, if this element object is an Class object of the class Constructor ), it is raised SecurityException .

If this object is an java.lang.Class object of the class Constructor and is flag true, it is raised SecurityException .

Parameters:
flag-New value of the accessible flag
Thrown:
SecurityException-If the request is denied.
See also:
SecurityManager.checkPermission(java.security.Permission), RuntimePermission

Java reflection calls private domain and method (setaccessible)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.