Return-Path: Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 55535 invoked from network); 27 May 2003 16:36:58 -0000 Received: from vpn1.seagullsw.com (HELO atlanta.seagullsw.com) (12.6.96.4) by daedalus.apache.org with SMTP; 27 May 2003 16:36:58 -0000 Received: by atlanta.seagullsw.com with Internet Mail Service (5.5.2656.59) id ; Tue, 27 May 2003 12:37:00 -0400 Message-ID: <245A7290F0E0D311BF6E009027E7908B07204540@atlanta.seagullsw.com> From: Gary Gregory To: 'Jakarta Commons Users List' Subject: RE: ToStringBuilder.reflectionToString() problem Date: Tue, 27 May 2003 12:36:58 -0400 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2656.59) Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C3246E.314788C0" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N ------_=_NextPart_001_01C3246E.314788C0 Content-Type: text/plain Hello, 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). toString(); } 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. Gary -----Original Message----- From: Tolley Shorn [mailto:Shorn.Tolley@nrm.qld.gov.au] Sent: Tuesday, May 27, 2003 02:51 To: commons-user@jakarta.apache.org Subject: ToStringBuilder.reflectionToString() problem Hi folks, I've got a bit of a problem with the ToStringBuilder's reflectionToString method. 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. CgLib is a pretty nifty little library that dynamically constructs JVM bytecode. 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 reflection. 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()); } } 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 { builder.append(f); } } } 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? 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 ------_=_NextPart_001_01C3246E.314788C0--