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

The usage of java.beans package in Java reflection

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "the usage of java.beans package in Java reflection". In daily operation, I believe that many people have doubts about the usage of java.beans package in Java reflection. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the usage of java.beans package in Java reflection". Next, please follow the editor to study!

Look at an example:

NodeDO.java: a standard javabean object

Public class NodeDO {private String name; private String email; private Date date; public String getName () {return name;} public void setName (String name) {this.name = name;} public String getEmail () {return email } public void setEmail (String email) {this.email = email;} public Date getDate () {return date;} public void setDate (Date date) {this.date = date @ Override public String toString () {return "NodeDO [name=" + name + ", email=" + email + ", date=" + DodeDOEditor.sdf.format (date) + "]";}}

Like TestDO.java, it refers to NodeDO:

Public class TestDO {private String nodeName; private NodeDO nodeDO; public String getNodeName () {return nodeName;} public void setNodeName (String nodeName) {this.nodeName = nodeName;} public NodeDO getNodeDO () {return nodeDO;} public void setNodeDO (NodeDO nodeDO) {this.nodeDO = nodeDO;}}

We want to implement something like this:

Public static void main (String [] args) throws Exception {Map parameters = new HashMap () {{put ("nodeName", "Little Fat Test"); put ("nodeDO", "xiaopang | xiaopang@163.com | 2015-10-20 12:00:00");}}; TestDO testDo = convert (parameters); System.out.println (testDo.getNodeName ()); System.out.println (testDo.getNodeDO ();}

How to convert the map of parameters into an object of TestDO?

(1) the first step is to define a PropertyEditor to convert Property:

Public class DodeDOEditor extends PropertyEditorSupport {public static final SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); @ Override public void setAsText (String text) throws IllegalArgumentException {String [] tokens = text.split ("\\ |"); NodeDO nodeDo = new NodeDO (); nodeDo.setName (tokens [0]); nodeDo.setEmail (tokens [1]); try {nodeDo.setDate (sdf.parse (tokens [2])) } catch (ParseException e) {throw new IllegalArgumentException (e);} setValue (nodeDo);}}

(2) to make transformation:

Public class PropertyEditorSample {static {PropertyEditorManager.registerEditor (NodeDO.class, DodeDOEditor.class)} public static void main (String [] args) throws Exception {Map parameters = new HashMap () {{put ("nodeName", "Little Fat Test"); put ("nodeDO", "xiaopang | xiaopang@163.com | 2015-10-20 12:00:00");}; TestDO testDo = convert (parameters) System.out.println (testDo.getNodeName ()); System.out.println (testDo.getNodeDO ());} private static TestDO convert (Map parameters) throws Exception {TestDO testDO = new TestDO (); BeanInfo bi = Introspector.getBeanInfo (TestDO.class); PropertyDescriptor [] pds = bi.getPropertyDescriptors (); for (PropertyDescriptor pd: pds) {Class propertyType = pd.getPropertyType (); Method writeMethod = pd.getWriteMethod () If (propertyType = = Class.class) {/ / ignore} else if (propertyType = = String.class) {writeMethod.invoke (testDO, parameters.get (pd.getName ());} else {PropertyEditor editor = PropertyEditorManager.findEditor (propertyType); if (editor! = null) {editor.setAsText (parameters.get (pd.getName (); writeMethod.invoke (testDO, editor.getValue ()) } else {System.out.println ("no editor for:" + pd.getName ());} return testDO;}}

In fact, there are only two words on the key point.

Editor.setAsText (parameters.get (pd.getName (); / / 1 editor.getValue (); / / 2 / / therefore, it is common to call setValue () in setAsText to save the transformed value, so that it can be obtained through getValue (), and the study on "the usage of java.beans package in Java reflection" is over. I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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