Java reflection mechanism

Source: Internet
Author: User

Package com. yemaozi. Reflect;
Import java. Lang. Reflect. constructor;
Import java. Lang. Reflect. field;
Import java. Lang. Reflect. method;
Public class reflecttest {
/**
* Get all the attributes and methods of the class through the reflection mechanism of Java
*/
Public void test1 (){
Try {
Class C = Class. forname ("com. yemaozi. Reflect. Customer ");
System. Out. println ("Property :");
Field F [] = C. getdeclaredfields ();
For (INT I = 0; I <F. length; I ++ ){
System. Out. println (F [I]. getname ());
}
System. Out. println ("method :");
Method M [] = C. getdeclaredmethods ();
For (INT I = 0; I <M. length; I ++ ){
System. Out. println (M [I]. tostring ());
}

} Catch (throwable e ){
System. Err. println (E );
}
}

/**
* Dynamic Modification of object attributes through the reflection mechanism of Java
* @ Param o
*/
Public void Test2 (customer O ){

Try {
Class C = O. getclass ();
// The first parameter of the getmethod method specifies the name of the method to be called, and the second parameter is the list of parameter types of the method to be called. If no parameter can be specified, null is returned.
Method sage = C. getmethod ("setage", new class [] {Int. Class });
Method Gage = C. getmethod ("getage", null );
Method sname = C. getmethod ("setname", new class [] {string. Class });
// Dynamically modify the age of the customer object
Object [] args1 = {New INTEGER (25 )};
Sage. Invoke (O, args1 );
// Dynamically Retrieve the age of the customer object
Integer age = (integer) Gage. Invoke (O, null );
System. Out. println ("the customer age is:" + age. intvalue ());
// Dynamically modify the name of the customer object
Object [] args2 = {New String ("Li Si ")};
Sname. Invoke (O, args2 );

} Catch (throwable e ){
System. Err. println (E );
}
}
/**
* Clone a simple object using the Java reflection mechanism
* @ Param o
* @ Return
*/
Public object test3 (customer O ){
Object O2 = NULL;
Try {
Class C = O. getclass ();
// Create a new object using the default constructor
O2 = C. getconstructor (new class [] {}). newinstance (
New object [] {});
Field fields [] = C. 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 = C. getmethod (getmethodname, new class [] {});
// Obtain the setxxx () method corresponding to the property
Method setmethod = C. getmethod (setmethodname, new class [] {field. GetType ()});
// Call the getxxx () method of the original object
Object value = getmethod. Invoke (O, new object [] {});
// Call the setxxx () method of the copy object
Setmethod. Invoke (O2, new object [] {value });
}
} Catch (throwable e ){
System. Err. println (E );
}
Return O2;
}

Public static void main (string [] ARGs) throws exception {
Reflecttest T = new reflecttest ();
T. test1 ();
Customer customer = new customer ();
Customer. setage (20 );
Customer. setname ("James ");
System. Out. println ("name before call:" + customer. getname ());
System. Out. println ("age before calling:" + customer. getage ());
T. Test2 (customer );
System. Out. println ("name:" + customer. getname ());
System. Out. println ("age after calling:" + customer. getage ());
Customer customer2 = (customer) T. test3 (customer );
System. Out. println ("Name of the cloned object:" + customer2.getname ());
System. Out. println ("clone object age:" + customer2.getage ());
}
}

Class Customer {
Private long ID;
Private string name;
Private int age;

Public customer (){
}
Public customer (string name, int age ){
This. Name = Name;
This. Age = age;
}
Public long GETID (){
Return ID;
}
Public void setid (long ID ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
}

Running result:
Attribute:
ID
Name
Age
Method:
Public java. Lang. String demo1.client. Customer. getname ()
Public long demo1.client. Customer. GETID ()
Public void demo1.client. Customer. setname (Java. Lang. String)
Public void demo1.client. Customer. setage (INT)
Public int demo1.client. Customer. getage ()
Public void demo1.client. Customer. setid (long)
Name before call: James
Age: 20
The customer age is: 25
Name: Li Si after the call
Age after call: 25
Name of the cloned object: Li Si
Clone object age: 25

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.