3

When auto-generating the toString method in IntelliJ it is putting all text onto a single line. Some of these lines are massive - how can I get Intellij to split the toString onto multi lines, after each new variable?

This is what I have:

@Override
    public String toString() {
        return "Address{" + " line1='" + line1 + '\'' + ", line2='" + line2 + '\'' + ", town='" + town + '\'' + ", county='" + county + '\'' + ", postcode='" + postcode + '\'' + ", country='" + country + '\'' + '}';
    }

And this is what I want:

@Override
    public String toString() {
        return "Address{" 
            + " line1='" + line1 + '\'' 
            + ", line2='" + line2 + '\'' 
            + ", town='" + town + '\'' 
            + ", county='" + county + '\'' 
            + ", postcode='" + postcode + '\'' 
            + ", country='" + country + '\'' + '}';
        }

I have tried creating a custom template but it just seems to ignore this.

1

2 Answers 2

1

IntelliJ's String concat (+) template produces output ~identical to the desired output shown in your question.

To engage this template ...

  • Code > Generate > toString
  • Choose Template: String concat (+)

If you need to tweak this template then click on Settings and choose the Templates tab and copy and create a copy of whichever existing template is closest to your needs, edit this copy and then it will appear in the Template dropdown for selection.

Here's a screenshot:

enter image description here

If - after using this template - the output is in a single long line then it's possible that your chosen Java code formatter is reformatting the output produced by the toString template.

3
  • Something must be overriding the template then as I have not changed the default String concat (+) template.
    – maloney
    Nov 3, 2017 at 12:06
  • Is it possible that your chosen Java code formatter is reformatting the output produced by the toString template?
    – glytching
    Nov 3, 2017 at 12:09
  • 1
    Restoring the IntelliJ Code Style preferences to default resolved my issue. I think it was the Line Breaks option under Preferences > Editor > Code style > Java which needs to be ticked!
    – maloney
    Nov 3, 2017 at 12:16
0

Here is a template to generate toString() method's body with plus sign in the next line

  1. Open templates setings toString() -> Settings -> Templates
  2. Clone String concat(+)
  3. Use that content
public java.lang.String toString() {
#if ( $members.size() > 0 )
    #set ( $i = 0 )
return "$classname{"
    #foreach( $member in $members )
        #if ( $i == 0 )
            + "$member.name="##
        #else
            + ", $member.name="##
        #end
        #if ( $member.objectArray )
            + java.util.Arrays.toString($member.accessor)
        #elseif ( $member.primitiveArray && $java_version >= 5)
            + java.util.Arrays.toString($member.accessor)
        #elseif ( $member.string )
            + $member.accessor + '\''
        #else
            + $member.accessor
        #end
        #set ( $i = $i + 1 )
    #end
+ '}';
#else
return "$classname{}";
#end
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.