I guess regardless of the output format you end up choosing, you could
write a static reflective toString generator and call that in your
toString methods. perhaps with some varargs to list exclusions which
you don't want to expose (or even private fields to include).
public String toString() {
return ToString.tostringificator(this, "field7", "field12");
}
The problem I end up having with my "simple" getSimpleName() + ' ' +
field1 + ' ' + field2, is that if a field is a large collection, you
often don't want to print that out. That's where if say it's a pool,
i'll print current size and capasity.
return this.getSimpleName() + ' ' + size + '/' + capasity;
I try to avoid using extra strings (like " size=" + hoping that the
toStrings of the other objects will provide this information or it
will just look obvious. Sometimes its a little obscure, but I see the
toStrings as a kind of debug representation most of the time.
On Sep 21, 11:58 pm, Jack <[EMAIL PROTECTED]> wrote:
> Christian,
> I do something similar in my current toString() implementations, but
> without the class information right now. I generally try to come up
> with something brief, human readable and can be used as a reasonable
> default if someone were to just use it to build a prototype UI for an
> application. In that regard, YAML may not be a good choice, especially
> considering it's a multi-line format.
>
> Yet I can see scenarios where YAML would be a good format. I'll have
> to think about this a little bit more.
>
> There are some YAML examples herehttp://www.yaml.org/start.html.
> Here's a quick example, let's say you have the following classes:
> class Car {
> String make;
> String model;
> Engine engine;
> /* Getters/setters omitted */
>
> }
>
> class Engine {
> String code;
> float displacementInCC;
>
> }
>
> The Honda S2000 instance would be represented as YAML as follows:
> --- !arbitrary type descriptor, e.g., com.foo.Car
> make : Honda
> model : S2000
> engine:
> code :f20c
> displacementInCC: 1997
>
> On Sep 21, 2:25 am, Christian Catchpole <[EMAIL PROTECTED]>
> wrote:
>
> > I don't know nothing about YAML but my default toString goes a little
> > something like this..
>
> > return this.getClass().getSimpleName() + ' ' + imporantField1 + ' ' +
> > importantField2;
>
> > getSimpleName survives refactoring and char space ' ' is 'neater' in
> > my mind than a white space string.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---