Frank,

For me it wouldn't be any use unless it also handled DynaBeans. Even then
I'd end up overriding it because I have some formatting utils which do
dates, arrays and collections. Seems to me if we put it in then we would end
up with a monster trying to satisy everyones needs and forever dealing with
bugzilla requests for enhacements (someone would want an i18n version!) -
all just for debugging.

The easiest thing is to just put all that code into a utility method - that
way its only a one liner in the toString() - even better if you have your
own "base" ActionForm that all you others derive from, then its only in one
place.

Also, there are a set of "toString" builders in commons lang which you might
like to use - including a reflection one like yours:

http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/builder/package-summary.html
http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/builder/ReflectionToStringBuilder.html

Niall

----- Original Message ----- 
From: "Frank W. Zammetti" <[EMAIL PROTECTED]>
To: "Struts Developer" <[EMAIL PROTECTED]>
Sent: Thursday, October 07, 2004 4:29 AM
Subject: Re: Proposed Action base class change


> Obviously I made a typo in the subject... this applies to the ActionForm
> base class.
>
> Did anyone have any comment on this?  I've noticed a lack of activity on
> the list lately...
>
> > Hello all...
> >
> > I find myself all the time overloading toString() of my ActionForms for
debugging
> > purposes, so I can easily dump the current state of the object.  I had
been doing
> > this for each ActionForm class, specifically for it, but it ocurrs to me
that a
> > general-purpose reflection-based approach would be better.
> >
> > I'd like to propose adding this functionality to the ActionForm base
class.  Here's
> > the code I propose adding:
> >
> > import java.lang.reflect.Field;
> > public static final AVERAGE_FIELD_SIZE = 25;
> > public String toString() {
> >   String str = "";
> >   StringBuffer sb = null;
> >   try {
> >     Field[] fields = this.getClass().getDeclaredFields();
> >     sb = new StringBuffer(fields.length * AVERAGE_FIELD_SIZE);
> >     for (int i = 0; i < fields.length; i++) {
> >       if (sb.length() > 0) { sb.append(", "); }
> >       sb.append(fields[i].getName() + "=" + fields[i].get(this));
> >     }
> >     str = sb.toString().trim();
> >   } catch (Exception e) {
> >     str = "Exception in ActionForm.toString() : " + e;
> >   }
> >   return str;
> > }
> >
> > The value of AVERAGE_FIELD_SIZE is a matter of debate, and it's of
course impossible
> > to come up with a real value, so something reasonable is the answer.  25
struck me
> > as a decent starting point.
> >
> > What does everyone think?  I find this functionality to be very useful
in my work,
> > and I suspect others may as well.  The code doesn't add any dependencies
outside
> > J2SE, and it's certainly simple enough as to not be particularly risky.
> >
> > Thanks all!
> >
> > Frank W. Zammetti
> > Founder and Chief Software Architect
> > Omnytex Technologies
> > http://www.omnytex.com
> >
>
>
> ---------------------------------------------------------------------
> 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