commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <ggreg...@seagullsw.com>
Subject RE: [lang] ToStringBuilder.reflectionToString
Date Wed, 16 Jul 2003 18:46:34 GMT
FYI, the issue is being tracked here:
http://issues.apache.org/bugzilla/show_bug.cgi?id=21663

You can add yourself to the email list if you'd like.

Gary

-----Original Message-----
From: Gary Gregory [mailto:ggregory@seagullsw.com] 
Sent: Wednesday, July 16, 2003 11:39
To: 'Jakarta Commons Users List'
Subject: RE: [lang] ToStringBuilder.reflectionToString 

Brian,

Ah, you need a current build then. Please download the latest from
http://cvs.apache.org/builds/jakarta-commons/nightly/commons-lang/

I am posting a follow up message on commons-dev with a proposal on an
extension to ToStringBuilder if you are interested.

Gary

-----Original Message-----
From: Brian Buckley [mailto:briankbuckley@comcast.net] 
Sent: Wednesday, July 16, 2003 11:23
To: Jakarta Commons Users List
Subject: Re: [lang] ToStringBuilder.reflectionToString 

Gary,   Thank you, your solution appears to do exactly what I need.

However my commons-lang-1.0.1.jar doesn't have the class
ReflectionToStringBuilder.  Is that class new and not yet released?

Also your example requires one to know the field name ["myDate"].  To avoid
that I'm thinking I'll do something like

if (java.util.Date.class.equals(field.getType())) {...}
instead of
if ("myDate".equals(field.getName())) {...}

-Brian

> Here is one option below. Supporting formatting in ToStringStyle would be
> better of course. We can think about it for post 2.0 (or now).
>
> import java.lang.reflect.Field;
> import java.text.SimpleDateFormat;
> import java.util.Date;
>
> import org.apache.commons.lang.builder.ReflectionToStringBuilder;
>
> /**
>  * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
>  * @version $Id: $
>  */
> public class TestRTSB {
>
>     private Date myDate = new Date();
>     private int foo= 22;
>
>     public static void main(String[] arguments) {
>         System.out.println(new TestRTSB());
>     }
>
>     public String toString() {
>         return (new ReflectionToStringBuilder(this) {
>             protected Object getValue(Field field) throws
> IllegalArgumentException, IllegalAccessException {
>                 Object value = super.getValue(field);
>                 if ("myDate".equals(field.getName())) {
>                     return new
SimpleDateFormat("yyyy-MM-dd").format(value);
>                 } else {
>                     return value;
>                 }
>             }
>         }).toString();
>     }
> }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org

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