3

How can recursively replace all catenations in toString method to StringBuilder for Java? Is there such a plugin in eclipse? For example: Replace it:

    return "AccountAddresses ["
            + ", corporateAddresses=" + CommonHelper.isNotNull(corporateAddresses)
            + ", corporateDeliveryMinimum=" + corporateDeliveryMinimum
            + ", depot=" +  CommonHelper.isNotNull(depot)
            + ", depotDeliveryMinimum=" + depotDeliveryMinimum
            + ", preSelectedId=" + preSelectedId
            + ", residentialAddresses=" +  CommonHelper.isNotNull(residentialAddresses)
            + ", residentialDeliveryMinimum=" + residentialDeliveryMinimum
            + "]";

at this:

    return new StringBuilder("AccountAddresses [")
            .append(", corporateAddresses=").append(CommonHelper.isNotNull(corporateAddresses))
            .append(", corporateDeliveryMinimum=").append(corporateDeliveryMinimum)
            .append(", depot=").append(CommonHelper.isNotNull(depot))
            .append(", depotDeliveryMinimum=").append(depotDeliveryMinimum)
            .append(", preSelectedId=").append(preSelectedId)
            .append(", residentialAddresses=").append(CommonHelper.isNotNull(residentialAddresses))
            .append(", residentialDeliveryMinimum=").append(residentialDeliveryMinimum)
            .append("]").toString();
Nikolay Moskvin
  • 1,038
  • 1
  • 16
  • 27

5 Answers5

14

It's a builtin command of Eclipse.

  • Click on one of the quotation marks (") in your String concatenation.
  • Bring the Quick Fix menu (Hit Ctrl + 1 on the keyboard).
  • Select Use 'StringBuilder' for String concatenation.

Magic! Your

    return "foo" + "bar";

changed to

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("foo");
    stringBuilder.append("bar");
    return stringBuilder.toString();
Splo
  • 10,544
  • 4
  • 19
  • 16
2

I haven't heard of an Eclipse plugin for this - so feel free to ignore this off topic answer - but IntelliJ has an "intention" that will do it for you (at least in 10.0 it does). There is a community edition available if you want to give it a shot.

Spyder
  • 1,882
  • 13
  • 23
1

Do a regex search and replace :

", ([a-zA-z0-9]+)=" \+ CommonHelper\.isNotNull\(([a-zA-z0-9]+)\)  // Find this

append(", $1=").append(CommonHelper.isNotNull($2))    // and replace with this

It is not complete, but you get the idea.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
0

Why dont you override the toString method in your class , and implement the stringbuilder append.

Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
0

I don't think any plugin would do that for you... this would be useless anyways : the Java compiler will do it better than anyone can (and if "StringBuilder" is ever replaced by something better, the Java compiler will be able to use this "something better" if you do not explicitely use a StringBuilder).

Kellindil
  • 4,523
  • 21
  • 20
  • By now the compiler replaces every single concatenation with a StringBuilder. This is expensive (from the micro optimization theatre point of view). `a + b + c` becomes `new sb( new sb(a + b) + c )` – DerMike Feb 03 '11 at 09:19
  • Are you sure about this? If I am not mistaken, this kind of scenario (one StringBuilder per "+") only happens when the compiler cannot do with "appends" (for instance, in a for loop). Something like this : http://stackoverflow.com/questions/1296571/what-happens-when-java-compiler-sees-many-string-concatenations-in-one-line – Kellindil Feb 03 '11 at 15:19