{"id":16,"date":"2005-10-13T22:06:20","date_gmt":"2005-10-14T03:06:20","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/?p=16"},"modified":"2009-07-21T22:36:24","modified_gmt":"2009-07-22T05:36:24","slug":"jakarta-beanutils-propertyutils","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2005\/10\/13\/jakarta-beanutils-propertyutils\/","title":{"rendered":"Jakarta BeanUtils PropertyUtils"},"content":{"rendered":"<p>I recently had to work with the Jakarta BeanUtils project and found it really useful.  I think of it as BeanUseful.  The BeanUtils project allows you to easily manipulate a JavaBean object using the static methods from the PropertyUtils class.  Here is a simple example of how to update a property value of a JavaBean.  Imagine I have a simple Greeting JavaBean class with a single property message.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nGreeting object = new Greeting();\r\nobject.setMessage(&quot;Hello&quot;);\r\n\/\/ ...\r\nString greeting = (String)PropertyUtils\r\n   .getSimpleProperty(object, &quot;message&quot;);\r\nPropertyUtils.setSimpleProperty(\r\n   object, &quot;message&quot;, greeting+&quot;, World!&quot;);\r\n<\/pre>\n<p>Of course, object is an instance of a JavaBean with a property named message.<\/p>\n<p>PropertyUtils can also access an indexed method.  Here is how you define an indexed property for a JavaBean:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic Item getItem(int index);\r\npublic void setItem(int index, Item item);\r\n<\/pre>\n<p>And here is how you would access an index property using PropertyUtils:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nint index = ...;\r\nString name = &quot;item&#x5B;&quot; + index + &quot;]&quot;;\r\nItem item = (Item)PropertyUtils\r\n   .getIndexedProperty(object, name);\r\n<\/pre>\n<p>In similar fashion, you can access a property that is mapped.  Here is an example of how you define a mapped property in a JavaBean:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic Object getValue(String key);\r\npublic void setValue(String key, Object item);\r\n<\/pre>\n<p>And here is how you would access a mapped property using PropertyUtils:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nPropertyUtils.setMappedProperty(\r\n   object, &quot;value(&quot;+key+&quot;)&quot;, value);\r\n<\/pre>\n<p>The BeanUtils project and Java Reflection in general, is best used in a dynamic setting when you do not know the type of a bean at compile time; that is the strength of Java Reflection, which BeanUtils builds on.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently had to work with the Jakarta BeanUtils project and found it really useful. I think of it as BeanUseful. The BeanUtils project allows you to easily manipulate a JavaBean object using the static methods from the PropertyUtils class. Here is a simple example of how to update a property value of a JavaBean. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-g","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":1,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":799,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions\/799"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}