Introduction to invoking properties and private members and proxy modes using reflection mechanisms

Source: Internet
Author: User
Tags reflection

To invoke a property using the reflection mechanism:

The properties of the class can be obtained through the reflection mechanism, the same properties can be assigned, the obtained value operation, call the GetField method and pass the property name can get "Learn Java, to the Gothic academy kaige123.com" The specified properties, Call the GetFields method to get all the properties, but you cannot get a private property in this way: code example:

Student Class Example:

Operation Result:

From the running results you can see that only the public properties are taken out and the private properties are not available.

To invoke a private member using the reflection mechanism:

1. Calling Private properties

Invoking a private property in the reflection mechanism requires either the Getdeclaredfields method or the Getdeclaredfield method, which is the acquisition of all private properties in the class, which is obtained with the specified private property, similar to GetFields and GetField. However, using the Getdeclaredfields or Getdeclaredfield method to obtain a private property requires a call to the Setaccessible method for a call to authorize the operation to operate on the obtained private property: code example:

Student code Example:

Operation Result:

2. Calling private method calls the private method is also the same, you need to get the private method through the Getdeclaredmethods method or the Getdeclaredmethod method, and then you have to call the Setaccessible method to invoke Authorization: code example:

Student code Example:

Operation Result:

3. Calling the private constructor

Student code Example:

Operation Result:

Proxy mode: The proxy mode is also called cutting-plane programming, as the name implies is the code can be cut into a gap plug into some code, proxy mode can be in the method before and after monitoring the exception, you can also put the log print statements in the agent, the method does not need to write the journal print statements, Resource opening and closing this number of repetitions of the code can also be written in the agent, the same in the method does not need to write, so the proxy mode is similar to the intermediary, in the middle of the code to resolve some repetitive and regular code. To write the code of the proxy mode, the first step is to write an interface to declare a method, the second step is to write a class A to implement this interface, write some code in the method, the third step is to write a Class B, the class is to implement the interface, and on the properties of the class declaration interface variables, the constructor declares the interface type parameters Used to store the object in the property, and then in the method of overriding the interface called a implementation of the method in the class, the fourth step write run class, in the main method first new Class A object out, then the new Class B object and put the class A object into Class B constructor, and finally the Class B object called method. code example:

Operation Result:

Code plots:

You can also write proxy patterns in an inherited way, but inheritance has a disadvantage, in case the class is not allowed to be inherited (the final decorated class), or you need to inherit other classes (Java can only inherit only one), then you cannot write the proxy mode, because the interface can be implemented multiple, so the use of interfaces is the best. code example:

Operation Result:

Code plots:

Introduction to invoking properties and private members and proxy modes using reflection mechanisms

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.