{"id":858,"date":"2010-01-08T19:40:00","date_gmt":"2010-01-08T19:40:00","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=858"},"modified":"2011-11-17T13:53:07","modified_gmt":"2011-11-17T13:53:07","slug":"debug-logging-of-bean-properties","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=858","title":{"rendered":"Debug Logging of Bean properties"},"content":{"rendered":"<p>A useful tool for doing this is the <a title=\"http:\/\/commons.apache.org\/lang\/api\/org\/apache\/commons\/lang\/builder\/ToStringBuilder.html\" href=\"http:\/\/commons.apache.org\/lang\/api\/org\/apache\/commons\/lang\/builder\/ToStringBuilder.html\"><strong>apache.commons.lang.builder.ToStringBuilder<\/strong> <\/a> using one of the\u00a0<strong>reflectionToString<\/strong> method flavours\u00a0which\u00a0are designed for this job. You can easily create dumps of entity beans and bean collections with this, and you can choose the output format.<\/p>\n<p>Whilst it is tempting to override the <strong>toString<\/strong> methods on e.g. all your entity beans, as this will automatically dump all related beans &amp; collections just by calling <strong>toString<\/strong> on a parent bean, this is <strong>not<\/strong> recommended! It can easily cause a storm of toString calls to propagate across all the related beans, resulting in an almost instant stack overflow! <a title=\"http:\/\/www.benmccann.com\/dev-blog\/easy-java-bean-tostring-using-beanutils\/\" href=\"http:\/\/www.benmccann.com\/dev-blog\/easy-java-bean-tostring-using-beanutils\/\"><strong>This post<\/strong> <\/a>details how to do this using the <strong>apache BeanUtils describe<\/strong> method, and the comments take credit for pointing me in the direction of the <strong>apache.commons.lang.ToStringBuilder<\/strong>. However, both methods cause stack overflows when used with JSF and JPA, even when you are not doing any logging.<\/p>\n<p>A better way is to provide a utility method into which you pass any bean or collection, and get the dump string returned which you can then log. The following is an example of a utility\u00a0class to do this :-<\/p>\n<pre>package uk.co.salientsoft.util.beans;<\/pre>\n<pre>import java.util.Collection;\r\nimport org.apache.commons.lang.builder.ToStringBuilder;\r\nimport org.apache.commons.lang.builder.ToStringStyle;\r\n\r\npublic class BeanUtil {\r\n\r\n  private static final ToStringStyle defaultStyle = ToStringStyle.SHORT_PREFIX_STYLE;\r\n\r\n  public static &lt;E&gt; String dumpProperties(Collection&lt;E&gt; coll) {\r\n    return dumpProperties(\"\", coll, defaultStyle);\r\n  }\r\n\u00a0 public static &lt;E&gt; String dumpProperties(String prompt, Collection&lt;E&gt; coll) {\r\n\u00a0\u00a0  return dumpProperties(prompt, coll, defaultStyle);\r\n\u00a0 }\r\n\u00a0 public static &lt;E&gt; String dumpProperties(Collection&lt;E&gt; coll, ToStringStyle style) {\r\n\u00a0\u00a0  return dumpProperties(\"\", coll, style);\r\n\u00a0 }\r\n\u00a0 public static &lt;E&gt; String dumpProperties(String prompt, Collection&lt;E&gt; coll,\r\n                                          ToStringStyle style) {\r\n\u00a0\u00a0  String properties = prompt;\r\n\u00a0\u00a0  for (E e : coll) {\r\n\u00a0\u00a0\u00a0  \/* Use the apache.commons.lang.builder toStringBuilder\r\n      * to list all this bean's properties\r\n      *\/\r\n\u00a0\u00a0\u00a0  properties += \"\\n\" + ToStringBuilder.reflectionToString(e, style);\r\n\u00a0\u00a0  }\r\n\u00a0\u00a0  return properties;\r\n\u00a0 }\u00a0\r\n\r\n\u00a0 public static &lt;E&gt; String dumpProperties(E e) {\r\n\u00a0\u00a0  return dumpProperties(\"\", e, defaultStyle);\r\n\u00a0 }\r\n\u00a0  public static &lt;E&gt; String dumpProperties(String prompt, E e) {\r\n\u00a0\u00a0   return dumpProperties(prompt, e, defaultStyle);\r\n\u00a0  }\r\n\u00a0  public static &lt;E&gt; String dumpProperties(E e, ToStringStyle style) {\r\n\u00a0\u00a0   return dumpProperties(\"\", e, style);\r\n\u00a0  }\r\n\u00a0  public static &lt;E&gt; String dumpProperties(String prompt, E e, ToStringStyle style) {\r\n\u00a0\u00a0   return prompt + ToStringBuilder.reflectionToString(e, style);\r\n\u00a0  }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A useful tool for doing this is the apache.commons.lang.builder.ToStringBuilder using one of the\u00a0reflectionToString method flavours\u00a0which\u00a0are designed for this job. You can easily create dumps of entity beans and bean collections with this, and you can choose the output format. Whilst it is tempting to override the toString methods on e.g. all your entity beans, as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[102,181,101,103],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/858"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=858"}],"version-history":[{"count":9,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/858\/revisions"}],"predecessor-version":[{"id":862,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/858\/revisions\/862"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}