Java reflection beanutils

Source: Internet
Author: User

Package reflect; import Java. lang. reflect. field; import Java. lang. reflect. invocationtargetexception; import Java. util. date; import Java. util. hashmap; import Java. util. map; import Org. apache. commons. beanutils. beanutils; import Org. apache. commons. beanutils. propertyutils; public class beanutilstest {public static void printobj (Object O) throws illegalargumentexception, illegalaccessexception {for (field F:. class. getdeclaredfields () {// getfields () cannot obtain the private variable F. setaccessible (true); // brute force access to private system. out. println (F. get (O) ;}} public static void properties (Object O) throws illegalaccessexception, invocationtargetexception, nosuchmethodexception {beanutils. setproperty (O, "birthday. year "," 113 "); // class attribute cascade settings // age is int type. Here, the string value is automatically converted to int, which is suitable for web projects. // If propertyutils is used. setproperty will not automatically convert beanutils. setproperty (O, "Age", "27"); system. out. println (beanutils. getproperty (O, "Birthday"); system. out. println (beanutils. getproperty (O, "Age");} public static void convertobjtomap (Object O) throws illegalaccessexception, invocationtargetexception, nosuchmethodexception {map = beanutils. describe (o); For (Object key: map. keyset () {system. out. println (Key + ":" + map. get (key) ;}} public static void putmapintoobj (Object O) throws illegalaccessexception, invocationtargetexception {Map <string, Object> properties = new hashmap <string, Object> (); properties. put ("name", "Rain"); properties. put ("Age", "24"); properties. put ("Birthday", new date (); beanutils. populate (O, properties); // import map data to objprintobj (o); // print} public static void main (string [] ARGs) throws illegalaccessexception, invocationtargetexception, nosuchmethodexception {A = new A (); beanutilstest. printobj (a); // beanutilstest. properties (a); // beanutilstest. convertobjtomap (a); // beanutilstest. putmapintoobj ();}}

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.