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!