hi,
Is there a easy way to dump a object structure to the screen or String
?
.toString() doesnt do what I need.
thanx in advance
Jeffrey Schwab - 22 May 2006 05:09 GMT
> hi,
>
> Is there a easy way to dump a object structure to the screen or String
> ?
> .toString() doesnt do what I need.
Could you please be more specific? Do you need to reconstruct the
objects from the string later?b
raptor - 22 May 2006 06:41 GMT
no, I just want to visualize on the screen the structure...
doesn't need to reconstruct it later...
JamesPraveen - 22 May 2006 06:47 GMT
do you want a generalised toString()?
raptor - 22 May 2006 07:16 GMT
I want something like this :
System.out.println(obj.dump);
obj = class ZZZ {
varX => 33,
varZ => class XXX {
varA => "hello",
}
....
}
and so on..
Philipp Leitner - 22 May 2006 10:24 GMT
I don't think that there's any standard API method for that, but it
seems easy enough to do using reflection in a general way, and even
easier if you already know the structure of the objects to visualize at
compile- time.
/philipp
raptor schrieb:
> I want something like this :
>
[quoted text clipped - 8 lines]
>
> and so on..
Dražen Gemić - 22 May 2006 13:36 GMT
> I want something like this :
>
[quoted text clipped - 9 lines]
>
> and so on..
I see. Something like PHP print_r ?
No, there is not.
DG
steve - 22 May 2006 22:44 GMT
> I want something like this :
>
[quoted text clipped - 9 lines]
>
> and so on..
pull the book "hardcore java" O'Reilly
check out chapter 9
there is a complete setup to do what you want, including a "toString()"
class you can pass an object to, it's about 50 lines
raptor - 27 May 2006 08:01 GMT
thanx for all the replies..
Real Gagnon - 22 May 2006 12:45 GMT
> Is there a easy way to dump a object structure to the screen or String
> ?
> .toString() doesnt do what I need.
Try a generic toString() :
http://www.rgagnon.com/javadetails/java-0432.html
Bye.

Signature
Real Gagnon from Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to
* http://www.rgagnon.com/howto.html
Joe - 22 May 2006 13:50 GMT
I think you can use the reflection api for this, in a relatively
straightforward manner.
http://java.sun.com/docs/books/tutorial/reflect/TOC.html
--Joe
dimitar - 22 May 2006 14:08 GMT
If you don't mind reading XML, you can use XStream.
Danno - 22 May 2006 17:05 GMT
Just want to make sure that you know that you can override toString()
to do exactly what you need. If that is not what you need, you can
take a look at a MBean implementation and use jconsole
(http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html) or
some other tool to get the info that you need.
Stefan Ram - 22 May 2006 20:05 GMT
>Is there a easy way to dump a object structure to the screen or String ?
>.toString() doesnt do what I need.
I just have finished a quick hack. The usage can be seen from
the following example.
class Ship { Position position; } class Position { int x; int y; }
public class Main
{
public static void main( final java.lang.String[] args )
{
Position position = new Position(); position.x = 1; position.y = 2;
Ship ship = new Ship(); ship.position = position;
java.lang.System.out.println
( new de.dclj.ram.notation.junobjects.Junobjects().dump( ship )); }}
The output is:
< &objectmap
object=
< &Ship
position=
< &Position zz0 >>
zz0=
< &Position
x=
< &int 1 >
y=
< &int 2 >>>
It needs the following jar file:
http://www.purl.org/stefan_ram/jar/ram.jar
This jar is released under the GPL. This is software in pre-alpha
state. Distribution with source and documentation is not yet
finished, but it is allowed to decompile the jar.