Package dk.lec.idea.plugin.generate

Generate toString() plugin v1.0

See:
          Description

Interface Summary
ConflictResolutionPolicy Interface that defines a policy for dealing with conflicts (i.e., the user is trying to generate a Object.toString() method but one already exists in this class.
 

Class Summary
CancelPolicy  
Config Configuration.
ConfigUI Configuration User Interface.
DuplicatePolicy  
FieldElement This is a field element containing information about the field.
FieldElementFactory Factory for creating FieldElement objects.
GenerateToStringAction The IDEA action for this plugin.
GenerateToStringPlugin The IDEA component for this plugin.
MethodExistsDialog This is a dialog when the toString() method already exists.
PsiUtil Utility methods accessing the IDEA PSI API (Program Structure Information).
ReplacePolicy  
 

Exception Summary
GenerateCodeException Error generating the javacode for the toString method.
 

Package dk.lec.idea.plugin.generate Description

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: 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:
fielddescription
$classnamethe name of the class
$fieldslist of FieldElement objects
$fieldthe FieldElement object
$field.namethe name of the field
$field.arrayis the field an array type?
$field.primitiveArrayis the field a primitve array type? (int[], short[], float[] etc.)
$field.objectArrayis the field an Object array type? (Object[], String[] etc.)
$field.collectionis the field a Collection type? (is assignable from java.util.Map)
$field.mapis the field a Map type? (is assignable from java.util.Map)
$field.primitiveis the field a primitive type? (int, byte, long, etc.)
$field.constantis the field a constant type? (it's static and it's name is in UPPERCASE.)
$field.transientdoes the field have a transient modifier?
$field.stringis the field a java.lang.String type?
$FQClassnamethe 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

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