Java Introspection (Introspector)

Source: Internet
Author: User
Tags naming convention

Introspection (Introspector) is a default processing method of the Java language for JavaBean class properties and events.

JavaBean is a special class that is primarily used to pass data information, and methods in this kind are primarily used to access private fields, and the method name conforms to a certain naming convention. If you pass information between two modules, you can encapsulate the information into JavaBean, which is called a value object, or VO. method is less than. This information is stored in the private variables of the class, and the

is obtained through set (), get ().

     JAVA JDK provides a set of Getter/setter methods that the API uses to access a property, which is introspective

Package Com.liang.instopector;import Java.io.serializable;import Java.util.date;public class Student implements Serializable {private string Name= "Liang";p rivate int age=21;private Date birthday;public string GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;}}

package Com.liang.instopector;import Java.beans.beaninfo;import Java.beans.introspector;import Java.beans.propertydescriptor;import Java.lang.reflect.method;import Java.util.date;import Org.junit.Test;public Class Demo {@Testpublic void Test1 () throws exception{//gets the specified BeanInfo object beanInfo beanInfo = Introspector.getbeaninfo ( Student.class);//Get Property Description Array propertydescriptor[] PDS = Beaninfo.getpropertydescriptors ();//Traversal: Because the class inherits the object class, There is a getclass () method in the object class, so the number is one System.out.println (pds.length) more than you write; for (PropertyDescriptor Pd:pds) { System.out.print (Pd.getname () + "");} System.out.println ();//Get the specified propertydescriptorpropertydescriptor pd=new propertydescriptor ("name", Student.class) ;//Get Get Method M=pd.getreadmethod (); Student s=new Student (); String str= (String) M.invoke (s, null); System.out.println (str);//Get Set Method M1=pd.getwritemethod (); M1.invoke (S, "AAA"); System.out.println (S.getname ());}} 









Java Introspection (Introspector)

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.