11

I want to create IntelliJ Idea template for toString method using String.format instead of concatenation, StringBuffer, etc.

For example I have following object:

public class Foo {
    private int id;
    private String name;
    private List<String> values;
}

If I generate toString for all fields by default Idea will generate:

@Override
public String toString() {
    return "Foo{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", values=" + values +
            '}';
}

But I want to generate following:

@Override
public String toString() {
    return String.format("Foo(id=%d, name=%s, values=%s)", id, name, values);
}
Justinw
  • 631
  • 10
  • 18
Yuriy Seredyuk
  • 1,643
  • 2
  • 15
  • 18
  • You should be careful with this. Even though String.format is more readable, it has very poor performance compared to old fashioned concatenation with +. – Justin Rowe Dec 18 '17 at 13:33
  • A newer approach with Java 8 and later is use of [`StringJoiner`](https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html), as shown in [my Answer](https://stackoverflow.com/a/54838010/642706) to a similar Question. – Basil Bourque Feb 23 '19 at 21:38

1 Answers1

17

For anybody still looking for this:

public java.lang.String toString() {
return String.format(
"$classname (##
#set ($i = 0)
#foreach ($member in $members)
#if ($i != 0)##
, ##
#end
$member.name=%s##
#set ($i = $i + 1)
#end
)",##
#set ($i = 0)
#foreach ($member in $members)
    #if ($i != 0)
    ,##
    #end
    #if ($member.primitiveArray || $member.objectArray)
    java.util.Arrays.toString(this.$member.name)##
    #else
    this.$member.name ##
    #end
    #set ($i = $i + 1)
#end
);
}

Adapted from this template.

EDIT

For those who wonder what is this stuff (I wonder how you ended up here tbh, since the question is very specific xD): this is an IntelliJ template for generation of toString method.

Code generation helps you generate code constructs and recurring elements according to preset templates, instead of writing everything by hand.

Please read more here and here for toString specifically.

Linuslabo
  • 1,568
  • 2
  • 24
  • 34
  • Awesome. Thank you @Linuslabo! – Yuriy Seredyuk Oct 10 '16 at 14:04
  • 1
    @linuslabo what do we do with these templates exactly? – fIwJlxSzApHEZIl Mar 14 '17 at 19:32
  • @anon58192932 You can generate a `toString` method in your class, which uses the `String.format` (just like requested by the question). Give a look at [Intellij docs](https://www.jetbrains.com/help/idea/2016.3/generating-tostring.html). – Linuslabo Mar 15 '17 at 09:28
  • Where in IntelliJ does this template belong? – Basil Bourque Feb 22 '19 at 04:41
  • @Basil have a look at the link to the docs in the comment above. – Linuslabo Feb 23 '19 at 21:23
  • Essential information, like how to use this should be explained in answers, not just a link and certainly not just a link in a comment. – Martin of Hessle Mar 27 '19 at 14:42
  • For those wonder where to put this template (just like me and @KrishnaAcharya), and don't want bother reading linked docs word by word(yes, there isn't a snapshot...), IT IS IN THE DIALOG WHERE YOU SELECT THE FIELDS OF toString(). You can choose from the "Template" pull down list or add your own by clicking the "Settings..." button. – lidlesseye Jun 25 '21 at 12:44