Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Java introspection

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of Java introspection, which is very detailed and has certain reference value. Friends who are interested must finish it!

Let's give a brief introduction to introspection:

1. Introspection is a method of dealing with bean class attributes and events in java language.

two。 Why learn introspection? When developing a framework, it is often necessary to use the properties of java objects to encapsulate the data of the program, and it is too troublesome to use reflection technology to complete such operations every time, so sun has developed a set of API specifically for manipulating the properties of java objects.

3. There are two ways for introspection to access the JavaBean property:

Manipulate the properties of Bean through the PropertyDescriptor class

Get the BeanInfo of the Bean object through the Introspector class, and then get the PropertyDescriptor of the property through the BeanInfo. Through this property descriptor, you can get the getter/setter methods corresponding to a property, and then call these methods through the reflection mechanism.

Let's write the test code.

Java code

/ * * get the BeanInfo of the Bean object through the Introspector class, and then get the property descriptor through BeanInfo (* PropertyDescriptor). Through this property descriptor, you can get the getter/setter method corresponding to a property, and then call these methods through the reflection mechanism. * / @ Test public void test () throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {Student st = new Student (); / / 1. Get the BeanInfo of the Bean object through the Introspector class, BeanInfo entity = Introspector.getBeanInfo (Student.class) / / 2. Then use BeanInfo to get the PropertyDescriptor PropertyDescriptor pdrs [] = entity.getPropertyDescriptors (); / / 3. Through this property descriptor, you can get the getter/setter method corresponding to a property, for (PropertyDescriptor pd: pdrs) {/ / System.out.println (pd.getName ()) / * * System.out.println (pd.getShortDescription ()); * System.out.println (pd.getDisplayName ()); * / if (pd.getName (). Equals ("age")) {/ / what type is age? Method md = pd.getWriteMethod (); md.invoke (st, 12);} / / System.out.println (st.getAge ());}

Bean class

Java code

Package cn.csdn.Introspector; public class Student {private String name; private int age; public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getEmail () {return email } public void setEmail (String email) {this.email = email;} private String email; public String getXxx () {return "Longmanfei";}} above is all the content of the article "sample Analysis of Java introspection". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report