Generate toString() plugin v1.0
.
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:
public class MyServerConfigBean {
private String name;
private String url;
private int port;
private String[] validIPs;
...
}
After invoking the GenerateToString action the bean is now added with the following method
public String toString() {
return "MyServerConfigBean{" +
"name='" + name + "'" +
", url='" + url + "'" +
", port=" + port +
", validIPs=" + (validIPs == null ? null : "length:" + validIPs.length + Arrays.asList(validIPs)) +
"}";
}
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 options to:
Replace existing method
Create a new toString() method, so you'll have duplicate methods
Cancel
The plugin uses Velocity Template Language to generate the code, so it's very flexible if you want your toString method to be a little different. The template can be changed from the settings at Options -> IDEA Settings -> GenerateToString.
Usage
Press CTRL + T to active the action, or use it from the Code menu. It's also in the editor popup menu (right-click).
Settings
You can change the settings of the plugin in Options -> IDE Settings.
Use fully qualified classname means that it will dump the classname including packagename.
Use field chooser dialog means that it will display a dialog for selecting fields.
Always replace existing toString() method means that existing toString methods will be replaced with the new code without prompting the confirm dialog.
Default selection for method already exists dialog means that you can change the default button for this dialog.
Method body (Velocity Template Language). The javacode for the method body is generated using Velocity. In this text field you can change the code generated.
Velocity context
The following variables are possible in the Velocity context:
| field | description |
| $classname | the name of the class |
| $fields | list of FieldElement objects |
| $field | the FieldElement object |
| $field.name | the name of the field |
| $field.array | is the field an array type? |
| $field.primitiveArray | is the field a primitve array type? (int[], short[], float[] etc.) |
| $field.objectArray | is the field an Object array type? (Object[], String[] etc.) |
| $field.collection | is the field a Collection type? (is assignable from java.util.Map) |
| $field.map | is the field a Map type? (is assignable from java.util.Map) |
| $field.primitive | is the field a primitive type? (int, byte, long, etc.) |
| $field.constant | is the field a constant type? (it's static and it's name is in UPPERCASE.) |
| $field.transient | does the field have a transient modifier? |
| $field.string | is the field a java.lang.String type? |
| $FQClassname | the fully qualified name of the class |
Default velocity template
Here is the default velocity template that is used, you can change the template from Options -> IDE Settings.
#if ( $fields.size() > 0 )
#set ( $i = 0 )
return "$classname{" +
#foreach( $field in $fields )
#if ( $i == 0 )
"##
#else
", ##
#end
#if ( $field.primitiveArray )
$field.name=" + ($field.name == null ? null : "(length:" + $field.name .length + ")") +
#elseif ( $field.objectArray )
$field.name=" + ($field.name == null ? null : "(length:" + $field.name .length + ")" + Arrays.asList($field.name)) +
#elseif ( $field.collection || $field.map )
$field.name=" + ($field.name == null ? null : "(size:" + $field.name .size() + ")" + $field.name) +
#elseif ( $field.string )
$field.name='" + $field.name + "'" +
#else
$field.name=" + $field.name +
#end
#set ( $i = $i + 1 )
#end
"}";
#else
return "$classname{}";
#end
Installation (not installed before)
Copy the tostring-plugin.jar file to the plugins folder of IDEA.
Installation (to upgrade default velocity template for method code)
Sadly IDEA needs to be run without the plugin one time, to delete the old configuration.
1: Delete the tostring-plugin.jar from the plugins folder
2: Start/restart IDEA to startup without the plugin
3: Copy the new version of tostring-plugin.jar to plugins folder
4: Restart IDEA
or you can simple copy the velocity code from the template above and insert it in the settings dialog.
Changelog
v1.0 - Added action to generate... menu (alt + insert). Thanks to Glen Schrader for the info.
v0.9 - 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.
v0.8 - Reusing IDEA's dialog for choosing fields. Added new variables to velocity context ($field.primitive, $field.constant, $field.transient, $FQClassname). Changed "method exists dialog", and added a new default settings for it. Action added to Code menu, and removed from the Other mainmenu.
v0.7 - Using RomanPorotnikov's velocity variation as default. Added field option map. Now handles evaluation of types for Collection and Map using isAssignableFrom method calls so all kind of objects are regonized as a Collection/Map properly. New dialog to select fields, enable/disable it in the settings. Thanks to Martin Schmid (author of the greate SimpleUML plugin) for the dialog code.
v0.6 - All code is now generated using a velocity template. See the template in Options -> IDE Settings. Now handles arrays and collections to dump their contents.
v0.5 - Fixed problem using same Velocity engine as IDEA. Cosmetic fix for body code when no fields and for first field. Updated javadoc.
v0.4 - Added Velocity template for method body code generation. Added build.xml in source distribution.
v0.3 - Can generate methods for inner classes. Added UML diagram to javadoc. Doesn't generate field dump for constant fields.
v0.2 - Removed super call option.
v0.1 - Initial version.
Todo/Ideas
- A settings icon.
- Filter settings for unwanted fields (by type, by modifier, by name).
- More PsiField information duplicated in FieldElement objects.
- File storing of templates, and select option in settings.
- Generation/Update of toString for all classes with certain names (etc. to generate it for all classes that ends with Bean in their name.)
- Fix JComboBox in settings to be placed right after it's label. (Anyone a Swing guru who can help me? - I'm a serverside programmer)
- More ready-to-use templates on the homepage.
Disclaimer
This plugin uses functions outside the openAPI (PSI, Internal IDEA dialogs), so it might not work in the future if IDEA changes these core API's.
UML Diagram