Java reading Note 11 (introspection mechanism in Java)

Source: Internet
Author: User


1. Preface

Recently in the study of Java, suddenly discovered this concept, in view of the oppression of curiosity, so intends to write a blog to summarize.


2. What is the introspection mechanism

In order for programmers to better get the properties of left Java objects, Sun has developed a set of APIs that we call "introspection", which facilitates our operations on the properties of class objects, reducing the number of code.


3. What is the difference between introspection and reflection?

Reflective mode in the running state of the Java class to map the various components into the corresponding Java class, you can dynamically get all the properties and dynamic call any method, emphasizing the running state.

The introspection mechanism is implemented by reflection, BeanInfo is used to expose the properties, methods, and events of a bean, and we can manipulate the properties of the JavaBean later.


4. A picture summary



In Java introspection, basically the above classes are used.

By beaninfo This class you can get to the methods and properties in the class, and you can refer to the JDK documentation


5.Demo Display

5.1. Modify the attribute mode by PropertyDescriptor

Package Com.query;import Java.beans.propertydescriptor;import Java.lang.reflect.method;public class BeanInfoUtil { public static void SetProperty (UserInfo userinfo,string userName) throws exception{        PropertyDescriptor propdesc= New PropertyDescriptor (Username,userinfo.class);        Method Methodsetusername=propdesc.getwritemethod ();        Methodsetusername.invoke (UserInfo, "Wong");        System.out.println ("Set UserName:" +userinfo.getusername ());    }      public static void GetProperty (UserInfo userinfo,string userName) throws exception{        PropertyDescriptor Prodescriptor =new PropertyDescriptor (username,userinfo.class);        Method Methodgetusername=prodescriptor.getreadmethod ();        Object Objusername=methodgetusername.invoke (userInfo);        System.out.println ("Get UserName:" +objusername.tostring ());}    }

5.2. Modifying properties through the Introspector class

Package Com.query;import Java.beans.beaninfo;import Java.beans.introspector;import java.beans.PropertyDescriptor; Import Java.lang.reflect.method;public class BeanInfoUtil2 {public static void Setpropertybyintrospector (UserInfo Userinfo,string userName) throws Exception {BeanInfo BeanInfo = Introspector.getbeaninfo (Userinfo.class); propertydescriptor[] prodescrtptors = Beaninfo.getpropertydescriptors (); if (prodescrtptors! = null && Prodescrtptors.length > 0) {for (PropertyDescriptor propdesc:prodescrtptors) {if (Propdesc.getname (). Equals ( UserName) {Method methodsetusername = Propdesc.getwritemethod (); Methodsetusername.invoke (UserInfo, "Alan"); System.out.println ("Set UserName:" + userinfo.getusername ()); public static void Getpropertybyintrospector (UserInfo userinfo,string userName) throws Exception {BeanInfo BeanInfo = Int Rospector.getbeaninfo (Userinfo.class); propertydescriptor[] prodescrtptors = Beaninfo.getpropertydescriptors (); if (prodescrtptors! = null &&amP Prodescrtptors.length > 0) {for (PropertyDescriptor propdesc:prodescrtptors) {if (Propdesc.getname (). Equals ( UserName)) {Method methodgetusername = Propdesc.getreadmethod (); Object objusername = Methodgetusername.invoke ( UserInfo); System.out.println ("Get userName:" + objusername.tostring ()); break;}}}}


Note, if the data type is incorrect, the error will be given when the JavaBean property is modified. For example Beaninfoutil.setproperty (UserInfo, "age"); the error is that the Age property is an int data type, and the value assigned to the Age property by default in the SetProperty method is a string type. Therefore, the argument type mismatch parameter type mismatch error message will be burst.

To address these issues, Apache has developed a simple, easy-to-use API to manipulate the Bean's Properties--beanutils toolkit.
Beanutils Toolkit: Download: http://commons.apache.org/beanutils/Note: A logging package is also required when applying http://commons.apache.org/logging/









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java reading Note 11 (introspection mechanism in Java)

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.