Java method reflection mechanism (2)

Source: Internet
Author: User

In Java Web development, we often encounter getting foreground values from the background, assigning the obtained values to the object through the set method, and then saving the object to the database, sometimes, when there are many object attributes, the amount of code is very large (in fact, they are all set methods). To address this, we can use the method reflection mechanism, write a general underlying method (effective for any entity), so that we do not need to set the object one by one.

Public static object objectmethod (Object OBJ, httpservletrequest request ){
Field [] fields = obj. getclass (). getdeclaredfields ();
For (field: fields ){
String MD = "set" + stringutils. capitalize (field. getname ());
Try {
Method method = obj. getclass (). getdeclaredmethod (MD, field. GetType ());
If ("Java. Lang. String". Equals (field. GetType (). getname ())){
String value = request. getparameter (field. getname ());
Method. Invoke (OBJ, value );
} Else if ("Java. Lang. Integer". Equals (field. GetType (). getname ())){
If (! "". Equals (request. getparameter (field. getname () & request. getparameter (field. getname ())! = NULL ){
Integer value = integer. valueof (request. getparameter (field. getname ()));
Method. Invoke (OBJ, value );
}
} Else {// here, when an attribute in the object is determined to be of the object type, it is then de-reflected.
Class C = Class. forname (field. GetType (). getname (); // obtain the object class corresponding to the attribute
Object o = C. newinstance (); // a new object class
Field [] FS = O. getclass (). getdeclaredfields ();
For (field F: FS ){
If (F. getname (). endswith ("ID") {// naming conventions are required
String set = "set" + stringutils. capitalize (F. getname ());
Method M = O. getclass (). getdeclaredmethod (set, F. GetType ());
If ("Java. Lang. Integer". Equals (F. GetType (). getname ())){
Integer value = integer. valueof (request. getparameter (field. getname ()));
M. Invoke (O, value );
Method. Invoke (OBJ, O );
} Else if ("Java. Lang. String". Equals (field. GetType (). getname ())){
String value = request. getparameter (field. getname ());
M. Invoke (O, value );
Method. Invoke (OBJ, O );
} // Add basic types in the future
}
}
}
} Catch (securityexception e ){
E. printstacktrace ();
} Catch (nosuchmethodexception e ){
E. printstacktrace ();
} Catch (invocationtargetexception e ){
E. printstacktrace ();
} Catch (illegalargumentexception e ){
E. printstacktrace ();
} Catch (illegalaccessexception e ){
E. printstacktrace ();
} Catch (classnotfoundexception e ){
E. printstacktrace ();
} Catch (instantiationexception e ){
E. printstacktrace ();
}
}
Return OBJ;
}

 

The above two articles are an understanding of the Java method reflection mechanism. In fact, the frameworks we use in Java Web development, such as Hibernate and spring, they are all written through the Java method reflection mechanism. Due to time issues, I am not allowed to use the reflection mechanism of Java method to simulate Hibernate and spring frameworks here, if you are interested, you can study it on your own. If you have any questions, please leave a message!

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.