Operate JavaBean in The introspection Mode

Source: Internet
Author: User
Import Java. beans. beaninfo; import Java. beans. introspector; import Java. beans. propertydescriptor; import Java. lang. reflect. field; import Java. lang. reflect. method;/*** use the introspection method to operate JavaBean */public class introspectortest {public static void main (string [] ARGs) throws exception {reflectpoint = new reflectpoint (3, 7 ); // call the traditional method class classz = reflectpoint in JavaBean. getclass (); field [] fields = classz. getdeclaredfields (); For (field: fields) {string name = "set" + field. getname (). touppercase (); Method method = classz. getmethod (name, Int. class); method. invoke (reflectpoint, 3);} system. out. println (reflectpoint); // call the an method in the internal mode string propertyname = "X"; // obtain the value of attribute x object retval = getproperty2 (reflectpoint, propertyname ); system. out. println (retval); // setproperty (reflectpoint, propertyname, 10) of attribute X; system. out. println (reflectpoint. getx ());} /*** set the attribute value * @ object to which the param OBJ attribute belongs * @ Param propertyname attribute name * @ Param propertyvalue attribute value */Private Static void setproperty (Object OBJ, string propertyname, object propertyvalue) throws exception {propertydescriptor Pd2 = new propertydescriptor (propertyname, obj. getclass (); Method setmethod = pd2.getwritemethod (); setmethod. invoke (OBJ, propertyvalue );} /*** get object property value * @ Param OBJ object * @ Param propertyname attribute name * @ return attribute value */private static object getproperty (Object OBJ, string propertyname) throws exception {propertydescriptor Pd = new propertydescriptor (propertyname, obj. getclass (); Method getmethod = PD. getreadmethod (); // get the get method object retval = getmethod. invoke (OBJ); Return retval ;} /*** Use Inner province to operate the JavaBean * @ Param OBJ attribute object * @ Param propertyname attribute name * @ return attribute value */private static object getproperty2 (Object OBJ, string propertyname) throws exception {object retval = NULL; beaninfo = introspector. getbeaninfo (obj. getclass (); propertydescriptor [] PDS = beaninfo. getpropertydescriptors (); For (propertydescriptor PD: PPS) {If (PD. getname (). equals (propertyname) {method getmethod = PD. getreadmethod (); retval = getmethod. invoke (OBJ); break ;}} return retval ;}}

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.