commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Commented: (BEANUTILS-172) [beanutils] BeanUtils and PropertyUtils toString function (code included)
Date Wed, 15 Nov 2006 01:47:38 GMT
    [ http://issues.apache.org/jira/browse/BEANUTILS-172?page=comments#action_12449888 ] 
            
Henri Yandell commented on BEANUTILS-172:
-----------------------------------------

Biggest problem is that it would add a Lang dependency to BeanUtils.

Maybe the solution is to put this in a 'examples' or 'Commons Util' or 'Commons Glue' that
contains bits of code that can depend on a lot of the libraries. 

Things like the BeanUtils optional library could go in that too.

Hen

> [beanutils] BeanUtils and PropertyUtils toString function (code included)
> -------------------------------------------------------------------------
>
>                 Key: BEANUTILS-172
>                 URL: http://issues.apache.org/jira/browse/BEANUTILS-172
>             Project: Commons BeanUtils
>          Issue Type: Improvement
>          Components: Bean / Property Utils
>         Environment: Operating System: All
> Platform: All
>            Reporter: Ryan Barker
>            Priority: Minor
>             Fix For: 1.8.0
>
>         Attachments: BeanToStringBuilder.java
>
>
> I thought it would be nice if there was an easy way to have a toString function
> that would take a bean and output a string, as writing toString functions for
> the BeanUtils class gets old fast. So I went and wrote one based off the
> toString function for org.apache.struts.action.DynaActionForm.
> The following function will output the contents of a bean in a very verbose
> manner. I would suggest that this version is suitable for PropertyUtils, while a
> BeanUtils version would not have the code blocks for  the verbose List, Array
> and Map strings. This code uses recursion to handle nested beans.
> Feel free to email me regarding this code
> ////////////////////////////////////////////////
> // Code Start
> ////////////////////////////////////////////////
>    public static String toString(Object value) {
>       if (value == null) {
>          return "<NULL>";
>       }
>       StringBuffer sb = new StringBuffer();
>       if (value.getClass().isArray()) {
>          // verbose array output
>          int n = Array.getLength(value);
>          sb.append("{");
>          for (int j = 0; j < n; j++) {
>             if (j > 0) {
>                sb.append(',');
>             }
>             sb.append(toString(Array.get(value, j)));
>          }
>          sb.append("}");
>       } else if (value instanceof List) {
>          // verbose list output
>          int n = ((List) value).size();
>          sb.append("{");
>          for (Iterator j=((List) value).iterator(); j.hasNext(); ) {
>             sb.append(toString(j.next()));
>             if (j.hasNext()) {
>                sb.append(',');
>             }
>          }
>          sb.append("}");
>       } else if (value instanceof Map) {
>          // verbose map output
>          Iterator j = ((Map) value).entrySet().iterator();
>          sb.append("{");
>          while (j.hasNext()) {
>             Map.Entry e = (Map.Entry) j.next();
>             sb.append(e.getKey());
>             sb.append('=');
>             sb.append(toString(e.getValue()));
>             if (j.hasNext()) {
>                sb.append(',');
>             }
>          }
>          sb.append("}");
>       } else if (ConvertUtils.lookup(value.getClass()) != null) {
>          // use the convertutils for this parameter
>          sb.append(ConvertUtils.convert(value));
>       } else {
>          // check if this class has a toString method declared anywhere for it
>          Method toStringMethod =
> MethodUtils.getAccessibleMethod(value.getClass(),"toString",new Class [0]);
>          if (toStringMethod == null ||
> toStringMethod.getDeclaringClass().getName().equals("java.lang.Object")) {
>             // its a bean!
>             sb.append("[className=");
>             sb.append(value.getClass().getName());
>             // find its properties
>             Map props = null;
>             try {
>                props = PropertyUtils.describe(value);
>             } catch (Exception e) {
>                sb.append("<EXCEPTION>");
>             }
>             // and print them
>             if (props != null) {
>                for (Iterator i=props.entrySet().iterator(); i.hasNext(); ) {
>                   Map.Entry entry = (Map.Entry) i.next();
>                   Object entryValue = entry.getValue();
>                   // dont print the class name twice
>                   if (!(entryValue instanceof Class)) {
>                      sb.append(',');
>                      sb.append(entry.getKey());
>                      sb.append('=');
>                      sb.append(toString(entry.getValue()));
>                   }
>                }
>             }
>             sb.append("]");
>          } else {
>             // use the normal toString method
>             sb.append(value.toString());
>          }
>       }
>       return (sb.toString());
>    }

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message