[ANN] toString plugin v0.8

GenerateToString is a action plugin for IDEA that is used to create or update java classes toString() method. The reason is valuebeans usually needs to dump their fieldvalues for debug purpose, and it's tedious to write the dump code for this. So this action plugin generates the code to dump all the fields in a simple manner.
In the code below we need to override the public toString method so the fields can be dumped:


After invoking the GenerateToString action the bean is now added with the following method

And if you change the fields you can run the action again and have it update the toString() method. In this situation where the toString() method already exists a dialog is displayed with choises to do:
- Update existing method
- Create a new toString() method, so you'll have duplicate methods
- Abort

The plugin uses Velocity Template Language to generate the code, so it's very flexible if you want your toString to be a little different. The template can be changed from the settings at Options -> IDEA Settings -> GenerateToString.


You can find the plugin at the IntelliJ community site at:

http://www.intellij.org/twiki/bin/view/Main/ToStringPlugin

6 comments
Comment actions Permalink

Claus,

I think that adding ' around field value will help to debug through string
values.

--
Best regards,
Mike Aizatsky.
-


JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"


0
Comment actions Permalink

I think that adding ' around field value will help to debug through string values.

Good point, it's added to the todo-list.

Just to make sure you only want the ' around the value if it's a String value right?

/Claus

0
Comment actions Permalink

Dunno.
I'm personally using it with any field type (just have live template
$VAR2$='" + $VAR1$ + "' $END$).

--
Best regards,
Mike Aizatsky.
-


JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"


0
Comment actions Permalink

It would be nice if the functionality of this plugin will be available in
live and file templates. AFAIK file template is in fact velocity script,
but this is not so for live templates which would be nice to have.

And more important of course is to expose some simplified psi via velocity
variables, just like it's done in tostring plugin, i.e. fields array etc.

Having that one can create complex templates with logic and with access to psi
without writing plugin in java.

Note to Claus Ibsen (just an idea): you can extend your plugin pretty easy to
support that, if you provide some templates storage and expose more psi to velocity.

--
Dmitry Skavish
-


Boston, MA, USA
tel. +1 781 370-6909
http://www.jzox.com
http://www.flashgap.com

0
Comment actions Permalink

Version 0.9 is just released.

Changelog
- Added new varaibles to velocity context ($field.string, $field.objectArray, $field.primitiveArray).
- Fixed bug in generated code for primitive array types.
- Single quotes around dumped values for String fields.
- Automatic import of java.util.Arrays if Arrays is used in the javacode.

0
Comment actions Permalink

It would be nice if the functionality of this plugin
will be available in
live and file templates. AFAIK file template is in
fact velocity script,
but this is not so for live templates which would be
nice to have.

I don't know if people will have their filetemplates mixed with this toString generator. And at this time I don't have a clue how I read these template files, but I can look into this if people would like this.

And more important of course is to expose some
simplified psi via velocity
variables, just like it's done in tostring plugin,
i.e. fields array etc.

Yes this will give you a greater flexibility that I think is important so you can fiddle with it until it matches your coding style. Thats why IDEA is soo cool, all the small details.

Having that one can create complex templates with
logic and with access to psi
without writing plugin in java.

Yes but I think JetBrains can do something in this area, to expose more variables $$ in the live/file templates, then you can make something simular like my plugin in the live templates itself, and that would be much more cool.

Note to Claus Ibsen (just an idea): you can extend
your plugin pretty easy to
support that, if you provide some templates storage
and expose more psi to velocity.

I can do this but I think it would be as another plugin. So if have the spartime and the lust I might give it a go.


/Claus

0

Please sign in to leave a comment.