Use Java reflection mechanism for object operations

Source: Internet
Author: User
We often use the COMMONS-BEANUTILS package to perform bean operations, for example, from map to bean get from bean to map ing, then what is the implementation principle, the following is a simple example of the operation; first, I create a bean
Public class bean {
Private string test1;

Private string gettest1 (){
Return test1;
}

Private void settest1 (string test1 ){
This. test1 = test1;
}
}

The above example is extremely extreme. We cannot use these two private methods to set and obtain values using our common Java operations, but we can use the Java reflection mechanism to perform very convenient operations.
Next, I will use Java reflection to create an object and operate on it. Here we assume that we can only get the name of this bean, for example, we get the name from the configuration file, which is the configuration of the worker in the company's command framework. The name of the worker and the method to be executed are configured, we can access it whether it is private or shared.
Public class testclass {

/**
* @ Param ARGs
* @ Throws classnotfoundexception
* @ Throws nosuchmethodexception
* @ Throws securityexception
* @ Throws invocationtargetexception
* @ Throws illegalaccessexception
* @ Throws illegalargumentexception
* @ Throws instantiationexception
*/
Public static void main (string [] ARGs) throws classnotfoundexception, securityexception, nosuchmethodexception, illegalargumentexception, illegalaccessexception, invocationtargetexception, instantiationexception {
// 1.0 obtain the bean Class Object
Class beanclass = Class. forname ("org. Test. T. Bean ");
// 2.0 construct a bean object using Constructors
Constructor BC = beanclass. getconstructor (null );
Bean B = (bean) BC. newinstance (null );
/**
* You can also construct
* Bean B = Class. forname ("org. Test. T. Bean"). newinstance ();
*/

// 3.0 use the getdeclaredmethod method to obtain the method to be executed (public/protected/private/default). The function name of the method to be executed is specified by the first parameter, the second parameter specifies the parameters contained in the function.
// If you only need to obtain the Public Member method, call the getmethod method directly.
Method setmethod = beanclass. getdeclaredmethod ("settest1", new class [] {string. Class });
Method getmethod = beanclass. getdeclaredmethod ("gettest1", null );
// 4.0 if you want to access a private method, the JVM will not execute the access control check if the accessible method is set to true here. If there are common methods, you do not need to set the access control check.
Setmethod. setaccessible (true );
// 5.0 run the obtained method object in the object constructed in step 1
Setmethod. Invoke (B, new object [] {"hello "});
System. Out. println (getmethod. isaccessible ());
Getmethod. setaccessible (true );
System. Out. println (getmethod. isaccessible ());
System. Out. println (getmethod. Invoke (B, null ));

}

}
The red part above is a function that obtains all the public/private/protected/default methods. If you only need to obtain the public method, you can call getmethod.

The pink method above is to set whether to perform the access check for the method object to be executed, that is, whether we can access public/private/protected/by default. If the value is true, it indicates that the reflected object should cancel the Java language access check during use. If the value is false, it indicates that the reflected object should perform Java access checks.
If there are common methods, you do not need to set them.

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.