Worked for three years, the second time using reflection!
Description of the problem encountered:
After multiple page queries, the object property in the returned list is ". 00" and the page is displayed unfriendly.
The reason for the query is because the SQL for querying data is:to_char (A.applyamount, ' 999g999g999d99 ') as Applyamount,
The value of Applyamount in the database is 0.
If you test with the following SQL statement , the query results will also query the ". xx" situation.
At the same time, the Web side has been in the form of a tag library to solve the problem, the mobile side when using the label, cannot parse the label.
SELECT to_char (' 0 ', ' 999g999g999d99 ') from DUAL
This time the coding idea:
Using the reflection mechanism, use all the setter () and Getter () methods in the list to reassign the properties of the Object!
The code is as follows:
/*** Using the Java Reflection mechanism * When the object attribute in List-amount appears ". 00", replace "0" *@authorCZX * @date 2014-10-29 *@paramList to be processed may be List of <?> type *@returnList of the processed list, that is, replace ". 00" for "0"*/@SuppressWarnings ({"Unchecked", "Rawtypes" }) Public Staticlist Amonuntoflist2stringutil (List list) {list NewList=NewArrayList (); //non-null judgment if(List! =NULL&& list.size () > 0) { //Traversal Loop List for(Object o:list) {//gets the class that the object stored in the list belongs toClass C =O.getclass (); //gets the array of all methods of the classMethod[] Ma =C.getmethods (); if(Ma.length > 0) { //iterate over the method array for(Method m:ma) {//handling only Set methods if(M.getname (). IndexOf ("set") >= 0) { Try { //Get the Get () methodMethod NEWM =C.getmethod (M.getname (). Replace ("Set", "get"),Newclass[] {}); //Use the Get () method to obtain the corresponding property of Object Newo = Newm.invoke (o, new object[] {}); //The property satisfies the de-empty. Trim () and the ". 00" equivalent when replaced with "0" if(Newo! =NULL&& ". 00". Equals (Newo.tostring (). Trim ())) { //using the Set () method, set the property M.invoke (O, new object[] {"0" }); } } Catch(IllegalArgumentException e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(InvocationTargetException e) {e.printstacktrace (); } Catch(SecurityException e) {e.printstacktrace (); } Catch(nosuchmethodexception e) {e.printstacktrace (); }}}} Newlist.add (O); } returnNewList; } returnlist; }
Java Reflection-Methods (method)