commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@btopenworld.com
Subject Re: [Lang] Omitting a field in ToStringBuilder
Date Fri, 14 Feb 2003 10:29:44 GMT
>  from:    Gary Gregory <ggregory@seagullsw.com>
> I need to exclude a given field with
> ToStringBuilder.reflectionToString(Object). 
> The Object I pass in is a javax.jms.Message which is a JMS abstract
> interface which can be implemented with all sorts of fields. I know I just
> do not want "messageText". 
> 
> My current implementation is nasty and does not feel right since I have to
> use a ToStringStyle:
> 
> 	private Object getMessageDescription(Message message) {
> 		class TempToStringStyle extends ToStringStyle {
> 			public void append(StringBuffer buffer, String
> fieldName, Object value, Boolean fullDetail) {
> 				if ("messageText".equals(fieldName) ==
> false) {
>                     super.append(buffer, fieldName, value, fullDetail);
> 				}
>  			}
> 		}
> 		return ToStringBuilder.reflectionToString(message, new
> TempToStringStyle(), false);
> 	}
> 
> 
> I could imagine doing something like:
> 
> ToStringBuilder builder = new ToStringBuilder(message);
> builder.remove(messageText);
> return builder.toString();

An interesting requirement. This last solution is incomplete as it doesn't use reflectionToString.
A ToStringStyle can be passed in to reflectionToString though, so your basic solution seems
sound.

Perhaps what is needed is a ToStringStyle decorator (like the collections unmodifiable and
synchronized decorators) that blocks certain field names? Essentially this would add your
temporary tostringstyle to [lang].

Stephen




Mime
View raw message