[jdom-interest] Re: Factories for Element creation

Louis Tribble louis.tribble at metamata.com
Fri Nov 3 13:19:22 PST 2000

Elliotte Rusty Harold wrote:
> At 10:23 AM +0100 11/2/00, Thomas Koch wrote:
> >We are using a modified SAXBuilder to accomplish approximately what
> >has been discussed earlier and that approach seems to work just fine.
> >
> >The private SAXBuilder is not optimal from the maintenance perspective,
> >but it is not too complex in its current form. An element factory hook
> >could improve this, but you might need other modifications to SAXBuilder
> >in addition to that, such as processing attributes etc.
> >
> >However, we have encountered one problem when subclassing Element:
> >the final toString() method (which we would like to override).
> >Is there a chance to change that?
> >
> We originally made it final to prevent people from using it instead
> of getSerializedForm(). toString() is for debugging only.

This could be addressed by prefixing the string with, for example,
the class name. But see my comment below about Swing.

> Here's my question to you: why do you want to subclass it? What's
> wrong with the existing behavior? What use-case do you have for which
> the current implementation of toString() fails you? What do you want
> to change about this method?

I'm not speaking for the original requester, of course, but here is
one use case:

If I've subclassed Element to give it extra state, I want that state
to be reflected in the toString(). We use an assertion and logging
package fairly heavily. To minimize overhead, we log entire objects
rather than extracting selected properties. When logging is disabled, 
nothing much happens and overhead is very low; when it is enabled, the 
toString takes care of getting and formatting _all_ the interesting 
state of the object into the log. Base classes with final toString() 
methods mean that I either lose important state when logging a 
subclass, or have to clutter up every logging point with extra
code to get and log the extra state _and_ the extra code uses
resources even when logging is disabled otherwise would have been
left alone.

Another use case that may come up is displaying Elements in a Swing 
component. Swing, unfortunately I think, encourages reliance on the 
toString() method for labeling objects gotten from its data models
(avoiding this usually requires subclassing or installing renderers).


Louis Tribble                                         louis at metamata.com
Metamata, Inc.                                   http://www.metamata.com
Tools for serious Java developers.                       +1 510 796 0915

More information about the jdom-interest mailing list