You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "SPRINGER,IAN (HP-NewJersey,ex1)" <ia...@hp.com> on 2003/03/31 17:49:15 UTC
RE: [lang] ToStringBuilder.reflectionToString() and inherited fie
lds
| 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: commons-user-unsubscribe@jakarta.apache.org
| For additional commands, e-mail: commons-user-help@jakarta.apache.org
|
Re: [lang] ToStringBuilder.reflectionToString() and inherited fields
Posted by Stephen Colebourne <sc...@btopenworld.com>.
The latest CVS versions of ToStringBuilder and other builders now allow for
inherited fields. Work is underway to move to a 2.0 release.
Stephen
----- Original Message -----
From: "SPRINGER,IAN (HP-NewJersey,ex1)" <ia...@hp.com>
To: "'Jakarta Commons Users List'" <co...@jakarta.apache.org>
Sent: Monday, March 31, 2003 4:49 PM
Subject: RE: [lang] ToStringBuilder.reflectionToString() and inherited
fields
> | 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: commons-user-unsubscribe@jakarta.apache.org
> | For additional commands, e-mail: commons-user-help@jakarta.apache.org
> |
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>