A simple Java reflection example (obtain fields in JavaBean)

Source: Internet
Author: User

// Obtain the methods in the object by passing in the object

Public void ddddd (Object O ){
// Convert the object type to the character type
String pojo = string. valueof (O );
// Truncate the correct pojo class instance path
Pojo = pojo. substring (0, pojo. indexof ("@"));

Class pojoin = NULL;
System. Out. println ("pojo:" + pojo );
Try {

// Create an object
Pojoin = Class. forname (pojo );
} Catch (classnotfoundexception e ){
E. printstacktrace ();
}

// Obtain the object's method set
Method [] Methods = pojoin. getdeclaredmethods ();

// Declare an array based on half of the size of the method set
String [] temp = new string [methods. Length/2];
List array = new arraylist ();

// Read the name of the method in the method set cyclically and extract
For (INT I = 0; I <methods. length; I ++ ){
If (methods [I]. tostring (). substring (7, 11). Equals ("Void ")){
String STR = methods [I]. tostring ();
STR = Str. substring (Str. indexof (pojo), str. indexof ("("));
STR = Str. substring (Str. lastindexof (".") + 4, str. Length ());
STR = Str. substring (0, 1). tolowercase (). tostring ()
+ Str. substring (1, str. Length ());

Array. Add (STR );
}
}

// Obtain the name of the field in JavaBean
For (INT I = 0; I <temp. length; I ++ ){
Temp [I] = array. Get (I). tostring ();
}

}

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.