commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <>
Subject RE: ToStringBuilder.reflectionToString() problem
Date Tue, 27 May 2003 16:36:58 GMT

The quick answer to your question (without adding features to
ToStringBuilder) is to use the non-reflection APIs. For example:

   public String toString() {
     return new ToStringBuilder(this).
       append("name", name).
       append("age", age).
       append("smoker", smoker).

If this is not acceptable, we can discuss how to make your feature request
fit in the ToStringBuilder framework. I am not sure the proposed solution
fits: (1) It breaks the model-view type of separation b/w the builder and
the style class and (2) it requires the access to be set on a per field
basis, which, the current impl is not factored to do.

So, in a nutshell, is the above example workable for you (or not)?
Obviously, using the reflection method is less code.


-----Original Message-----
From: Tolley Shorn [] 
Sent: Tuesday, May 27, 2003 02:51
Subject: ToStringBuilder.reflectionToString() problem

Hi folks,

I've got a bit of a problem with the ToStringBuilder's reflectionToString
I'd like to suggest a very small improvement (I think) to the
ToStringBuilder interface.

We're using Hibernate to provide the persistence of our domain model.
I would like to use ToStringBuilder to provide accurate toString() methods
for the classes in our domain model.
Unfortunately, in some circumstances Hibernate uses an OS library call
CgLib is a pretty nifty little library that dynamically constructs JVM
Hibernate uses CgLib to provide transparent proxies.

Unfortunately, becuase of the structure of our app (not really Hibernate or
CgLib's fault), the reflectionToString() method will cause an exception
because it will try to access the CgLib class outside of an appropriate
scope (becuase we call toString() outside of the appropriate scope).

Basically, I would like to be able to customize the ToStringBuilder to
decide on a per-attribute basis whether or not to access a given field using

What I'd like the reflectionToString method to do is to implement the
template design pattern by delegating the actual access and formatting of
the individual attribute to a method on the ToStringStyle.

The basic ToStringStyle could just delegate this decision straight back to
the ordinary ToStringBuiler, so for ordinary use the interface wouldn't
change at all.
With this functionality you could have all kinds of fun with doing special
things for certain attribute names or types.

What this change would allow me to do is something like this:

class BaseEntity{
	public toString(){
		return ToStringBuilder.reflectionToString(this, new

CgLibAwareToStringStyle extends StandardToStringStyle {
	public String appendField(ToStringBuilder builder, Field f){
		if( f.getType().isAssignableFrom(CgLibMarkerInterface.class)
			// do a special CgLib appropriate thing that won't
make my app barf :)
		else {

Maybe I'm missing a better way of doing this with the current
ToStringBuilder interface?
At the moment it appears I'll have to duplicate the ToStringBuilder in our
app and extend it so I can keep going (or forfeit ToStringBuiler's labour
saving functionality).

What do you folks think?


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:
For additional commands, e-mail:

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message