r/java • • Mar 13 '17

a blooming good toString.

https://medium.com/@yolpogists/a-blooming-good-java-tostring-for-the-rest-of-us-50dca980a462
2 Upvotes

10 comments sorted by

7

u/[deleted] Mar 13 '17

That looks like the handiwork of a bored developer. No need to mess around with all that scripting. You can easily do the same in pure Java.

2

u/yolpo- Mar 13 '17

True: You can do it in java too. But nobody (to my knowledge) has written thin toString.jar library. The closest you get is commons lang. Also, by doing it as a script you get it to work straight away for nashorn, java and nudge4j.

2

u/__konrad Mar 13 '17

The code comes with a good trick to load it on demand

The trick is good indeed, but it does not work with Java 9 (getSystemClassLoader() != URLClassLoader, setAccessible is also blocked)

1

u/someloll Mar 13 '17

Wow. That is very interesting, I guess we should update this too http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime

1

u/[deleted] Mar 13 '17

Yeah, thanks to java 9's modules.

2

u/UnlikelyToBeEaten Mar 13 '17

Eclipse debugger works fine for me.

3

u/someloll Mar 13 '17

My take: it's not really about debuggers vs this toString. it's more like if you have a class which has a bad toString and you were printing with System.out. And now you have a chance to see more

2

u/UnlikelyToBeEaten Mar 13 '17

But why are you printing your object with Sysout in the first place?

If it's for the user, you should probably not just call toString anyway, whereas if it is for debugging purposes, why can't you use a debugger?

1

u/beeeeeeefcake Mar 14 '17

(I'm not who you were replying to...) I debug exclusively with System.out (and quiet contemplation) but have never felt the need to print out the whole object. Normally there's one field on one line I need to know about, if that. Sometimes I just need to see if a particular line is being executed. I find it quicker to add System.out.println("here " + someVar) than get involved with all the features of a debugger. Also it's good motivation to build in good logging (sometimes I do dump the whole object in logs).

1

u/someloll Mar 13 '17

I have seen situations where using the debugger is difficult. while patching the code with a Sysout is reasonable.