Java reflection method

Source: Internet
Author: User
[1] method class Introduction

One of the basic applications of Java reflection technology is to call Methods dynamically during runtime. To call a method dynamically, you must first obtain the method itself. The procedure is as follows:
1. Get the Class Object
2. Call the getmethod (string, class []) method of the Class Object to obtain the specified method.
 
The first parameter of the getmethod method is used to specify the method name, and the second parameter is a class array used to store class objects representing each parameter type. This method is worth noting:
 
If the parameter type is atomic (INT, long, short, etc.), you must use a parameter such as Int. class, long. class to obtain the corresponding class object, instead of using the corresponding encapsulation class object.

After obtaining the method object, you can call the method dynamically at runtime. The main methods in the method class are as follows:
1. getdeclaringclass ()
2. getexceptiontypes ()
3. Obtain the type of all parameters in the method Signature: getparametertypes ()
4. Get the type of the return value in the method Signature: getreturntype ()
5. Call method: Object invoke (Object OBJ, object... ARGs)
 
The core of the method class is the invoke method, which is used to call the method object.
The method corresponding to the object. Pay special attention to the second parameter: Usually this is an object array, which means that if the parameter is atomic data, he must first be converted to the corresponding encapsulation class pair
For example, the invoke method will automatically decompress it to the atomic type in the background.
 
Since jdk1.5 and later, the "auto-boxing" and "auto-Unpacking" features are added. Therefore, you can see that at row 61, parameters are transmitted using atomic data rather than encapsulation classes. If JDK or earlier is used, an error is returned. We recommend that you do not use this method to avoid full data types.

[2] how to copy objects using reflection

1. Get the list of all member variables of the source object

2. retrieve a variable from the Variable list each time and obtain its getxxx () and setxxx (type) method names.

3. obtain the corresponding method object based on the getxxx () and setxxx (type) method names.

4. The source object uses the invoke (object, class []) method to call the getxxx () method to obtain the value of the member variable.

5. the target object uses the setxxx (type) method of the invoke (object, class []) method to assign values to the member variables of the target object.



The key code segment format is as follows:


1. Create a method object: class. getmethod (method name, method prameters class array)


2. Call method: method. Invoke (object, method parameters class array)

[3] sample code

// Obtain all attributes of an object
Field fields [] = classtype. getdeclaredfields ();

For (INT I = 0; I <fields. length; I ++ ){
Field field = Fields [I];

String fieldname = field. getname ();
String firstletter = fieldname. substring (0, 1). touppercase ();

// Obtain the name of the getxxx () method corresponding to the property
String getmethodname = "get" + firstletter + fieldname. substring (1 );
// Obtain the name of the setxxx () method corresponding to the property
String setmethodname = "set" + firstletter + fieldname. substring (1 );

// Obtain the getxxx () method corresponding to the property
Method getmethod = classtype. getmethod (getmethodname,
New Class [] {});
// Obtain the setxxx () method corresponding to the property and use the filed object type
Method setmethod = classtype. getmethod (setmethodname,
New Class [] {field. GetType ()});

// Call the getxxx () method of the original object: Specify the list of parameter values of the called object and Method
Object value = getmethod. Invoke (object, new object [] {});
System. Out. println (fieldname + ":" + value );
// Call the setxxx () method of the copy object: Specify the list of called objects and parameter values (note that the object type must be used)
Setmethod. Invoke (objectcopy, new object [] {value });
}

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.