Eclipse JUtils toString() Logo

Configuring Plugin Preferences
 


Not only is the JUtils ToString Generator plugin versatile and easy to use, it can also be tweaked according to your needs.

For instance, if you don't want a space before and after the '=' sign while generating toString() implementation, you can configure it so by opening the JUtils ToString preference page. This can be found at Window >> Preferences >> JUtils >> toString() in the Eclipse IDE.

Over there, under Method Body, you will find a template, similar to the sample shown below. Here, ${attribute} represents a field in the Java Class.

- hide

    public String toString()
    {
        final String TAB = "    ";
        
        String retValue = "";
        
        retValue = "${class_name} ( "
            + super.toString() + TAB
            + "${attribute} = " + this.${attribute} + TAB
            + " )";
        
        return retValue;
    }

Now, all you have to do is change the following line

    + "${attribute} = " + this.${attribute} + TAB
    // note the space padding around '=' sign

to this

    + "${attribute}=" + this.${attribute} + TAB

This way, you can perform numerous modifications. To enumerate some more, you can change the TAB character to 8 spaces instead of the default 4, separate the elements by a LF or CR character, comment out certain parts of the implementation template. The possibilities are infinite.

You can even change the JavaDoc according to your needs.

Lets say you want a StringBuilder implementation for your toString(). You can just select the mode in the ToString preference page and click OK. Next time you generate toString(), you will get a StringBuilder implementation. Please see the Screenshots page.

TOP This mechanism of configuring toString() or Copy Constructor code is very powerful and it has some cool consequences!

I recently happened to discover one such possibility, thanks to Mr. Hugo de Oude. Here, an XML had to be the output of execution of toString(). The following shows the XML output desired.

- hide

    <Person>
        <name>Bond, James</name>
        <age>46</age>
        <height>180</height>
        <weight>80</weight>
        <salary>1000000</salary>
        <address>Hollywood, CA</address>
    </Person>

Generating such an output is not possible with any other toString() plugin! However, with JUtils ToString Generator, it was a simple configuration. The following was the configuration used for generating such a code:

- hide

    public String toString()
    {
        final String TAB = "\n";
        
        String retValue = "";
        
        retValue = "<${class_name}>" + TAB
            + "    <${attribute}>" + this.${attribute} + "</${attribute}>" + TAB
            + "</${class_name}>";
    
        return retValue;
    }