Java Reflection-Methods (method)

Source: Internet
Author: User

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)

Related Article

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.