[ajug-members] object toString utility using reflection?
Shane Crosby
maccroz at gmail.com
Tue Aug 21 21:10:35 EDT 2007
commons-lang has a ToStringBuilder that uses Reflection and works on a
generic object. It would look something like this:
ToStringBuilder.reflectionToString(myObj);
http://commons.apache.org/lang/apidocs/org/apache/commons/lang/builder/ReflectionToStringBuilder.html
It may or may not work depending on your situation. See this note from the
javadoc:
This class uses reflection to determine the fields to append. Because these
> fields are usually private, the class uses AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[],
> boolean)<http://java.sun.com/j2se/1.3/docs/api/java/lang/reflect/AccessibleObject.html#setAccessible%28java.lang.reflect.AccessibleObject%5B%5D,%20boolean%29>to change the visibility of the fields. This will fail under a security
> manager, unless the appropriate permissions are set up correctly.
A similar topic was discussed recently on java.net. Perhaps you could find
some ideas there as well:
http://today.java.net/pub/a/today/2007/07/12/DontRepeatYourselfSQ17.html
On 8/21/07, Curt Smith <csmith at javadepot.com> wrote:
>
> I just reached wits end with a major short coming with Java, in that
> object.toString() doesn't dump state including referenced objects. This
> is a major pain with corba/SOAP when all of your DTO classes are
> generated. This feature should have been apart of the JDK since 1.0.
> If so how much debugging time would this have saved all you old timers
> who have suffered with logs from junior types that just say: "got
> here", or "error". LOL! Or maybe we should volunteer an extra page or
> two for Nutshell Java on debug ability of your java code. :)
>
> I'm aware of and recommend the Eclipse plugin: Jutils that right-click
> generate toString() method for any pojo. it's great when you own the
> source. This won't help with Corba and SOAP interfaces and you want to
> log the request and response and don't want to write a dump utility for
> every interface class. Or modify the generated code.
>
> Is there a reflection based utility class that can return a string of a
> given classes state?
>
> tnx,
> curt smith
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.ajug.org/pipermail/ajug-members/attachments/20070821/1d0a1267/attachment.html
More information about the ajug-members
mailing list