| Please correct me if I'm posting to the wrong place or something.
| 
| I'd like to use commons-lang's reflective toString() builder
| functionality in the toString() methods of my domain objects.  But we
| use a fair bit of field inheritance, and reflectionToString() calls
| Class.getDeclaredFields() which the Java API states won't return
| inherited fields.
| 
| Am I barking up the wrong tree using the ToStringBuilder for this?
| Are there any plans to make the ToStringBuilder use a different
| mechanism for this (like maybe using the JavaBean standard?)

Hi Shorn,

Here's a reflective toString() that handles inherited fields:

   /**
    * For the specified object, prints names and values of all public
fields,
    * both declared and inherited.
    *
    * @param obj the object
    *
    * @return a nicely-formatted string containing the names and values of
all public fields,
    *         both declared and inherited.
    */
   public static String toString( Object obj )
   {

      Class clazz = obj.getClass();
      StringBuffer buf = new StringBuffer( 1024 );
      buf.append( "[Field Name]\t[Value]\n" );
      while ( clazz != null )
      {
         Field[] field = clazz.getDeclaredFields();
         for ( int i = 0; i < field.length; i++ )
         {
            buf.append( field[i].getName() + ":\t" );
            Object field_object = null;
            try
            {
               field_object = field[i].get( obj );
            }
            catch ( IllegalAccessException iae )
            {
               buf.append( "<" );
               buf.append( iae.getMessage() );
               buf.append( ">" );
            }
            if ( field_object != null )
            {
               buf.append( "\"" );
               buf.append( field_object );
               buf.append( "\"" );
            }
            else
            {
               buf.append( "<null>" );
            }
            buf.append( "\n" );
         }
         clazz = clazz.getSuperclass(); //Recurse super classes
      }
      return ( buf.toString() );

   }



Feel free to incorporate this into commons-lang.

Regards,
Ian
 
| Cheers,
| Shorn.
| 
| 
| **************************************************************
| **********
| The information in this e-mail together with any attachments is
| intended only for the person or entity to which it is addressed
| and may contain confidential and/or privileged material.
| 
| Any form of review, disclosure, modification, distribution
| and/or publication of this e-mail message is prohibited.  
| 
| If you have received this message in error, you are asked to
| inform the sender as quickly as possible and delete this message
| and any copies of this message from your computer and/or your
| computer system network.  
| **************************************************************
| **********
| 
| 
| ---------------------------------------------------------------------
| To unsubscribe, e-mail: [EMAIL PROTECTED]
| For additional commands, e-mail: [EMAIL PROTECTED]
| 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to