IntelliJ IDEA 2016.3 Help

Generating toString()

On this page:

Introduction

The action Generate toString() enables creating or updating toString() method.

The beans usually needs to dump their field values for debug purposes, and it's rather tedious to write the dump code for it. The Generate toString() action generates the toString() method, dumping all the fields in a simple manner.

Generating toString() method

To generate toString() method

  1. Open the desired class for editing and do one of the following:
    • On the main menu, choose Code | Generate.
    • Right-click the editor and choose Generate on the context menu
    • Press Alt+Insert.
  2. From the pop-up list that shows up, select toString() option. Generate toString() wizard displays the list of fields in the class.
  3. In the wizard, specify the following:
    • Select the fields to be used to generate a toString() method.

      By default, all the available fields are selected. Clicking the button Select None results in adding a toString() method consisting of method declaration and return statement only.

    • Select the desired way of generating a toString() method from the Templates drop-down list.
    • Select the check box Insert @Override if necessary.

      Refer to the section Generate ToString() Dialog for details.

    • If you are not happy with the settings, click the Settings button. This results in showing the toString() Generation Settings dialog box, where one can tune the function's behavior. Refer to the section Generate ToString() Settings Dialog for details.

    When ready, click OK.

Inspections

There are two related code inspections under the node toString() issues:

  • Class does not override 'toString()' method
  • Field not used in 'toString()' method

The inspection Class does not override toString() method can be used to identify any classes where you might have forgotten to add a toString() method.
This inspection uses the exclude settings to ignore classes with the fields not supposed to be dumped. An additional settings is to exclude certain classes by using a regular expression matching their classname. As default, this is used to exclude any Exception classes.

The inspection Field not used in 'toString()' method: This inspection can be used to identify out-of-synchronization situations, where you have an existing toString() method that dumps the fields. However, some fields have been added to the class later, and these new fields are not dumped in the toString() method.
Change the severity of this inspection by enabling it to show errors as warnings. This will highlight any unused fields on-the-fly in the editor; the right gutter will indicate the location of the errors using a yellow marker.

Logging

Log4j is used for logging. To enable logging, open for editing the file log.xml used by IntelliJ IDEA. This file resides in the bin folder of IntelliJ IDEA installation. Add the following lines to this file:

<category name="org.jetbrains.generate.tostring"> <priority value="DEBUG"/> <appender-ref ref="FILE"/> </category>

Examples

Basic code

Consider the following code:

public class MyServerConfigBean { private String name; private String url; private int port; private String[] validIPs; ... }

Place the caret somewhere within the class, press Alt+Insert, and choose toString() from the pop-up list. The following method is now added to the bean:

public String toString() { return "MyServerConfigBean{" + "name='" + name + '\'' + ", url='" + url + '\'' + ", port=" + port + ", validIps=" + Arrays.toString(validIps) + '}'; }

Getter is enabled

Consider the following code:

public class MyServerConfigBean { private String name; private String url; private int port; private String[] validIPs; ... public String getServerAddress() { return url + ":" + port; } ... }

Place the caret somewhere within the class, press Alt+Insert, and choose toString() from the pop-up list. After invoking the action Generate toString(), the result is:

public String toString() { return "MyServerConfigBean{" + "name='" + name + '\'' + ", url='" + url + '\'' + ", port=" + port + ", serverAddress='" + getServerAddress() + "'" + ", validIps=" + Arrays.toString(validIps) + '}' }

Excluding fields and methods

Refer to the Exclude section of the Settings dialog.

Usually you don't want to add constant fields as the debug information in your toString() method. So you select the check box Exclude constant fields, and prevent constant fields in the output.
Besides that, it is possible to filter by the field's name, to exclude, say, an internal debug field. So, type ^debug in the text field Exclude fields by name (reg exp), to prevent debug fields.

The example below shows the results with excluded fields. The original code is:

public class MyServerConfigBean { private final static String USERNAME = "scott"; private final static String PASSWORD = "tiger"; private String name; private String url; private int port; private String[] validIPs; ... public String getServerAddress() { return url + ":" + port; private boolean debug = true; } ... }

After generating a toString() method, the code looks like the following:

public String toString() { return "MyServerConfigBean{" + "name='" + name + '\'' + ", url='" + url + '\'' + ", port=" + port + ", validIps=" + Arrays.toString(validIps) + '}' }

As you see, the constant fields (USERNAME, PASSWORD) are not used in the generated code. The regular expression excludes the debug field. The excluded fields do not appear in the Generate ToString() Dialog.

To exclude a method, select the check box Exclude methods by name (reg exp) field. For example, if you type ^getCausedBy.* in the text field Exclude methods by name (reg exp), you will thus prevent outputting methods whose names start with getCausedBy.

JavaDoc

It is possible to add JavaDoc comments to the generated toString() method. This is done by inserting the JavaDoc comments in a Velocity template. See the following template example:

/** * Insert your JavaDoc comments here * * @return a string representation of the object. */ return "$classname{}";

See Also

Last modified: 29 November 2016