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(); 
3
  • What programming language are you working in? Is that C# or Java? You picked so many other tags you forgot to specify a language. Commented Feb 2, 2011 at 6:20
  • @Code Gray - Can you develop C# in Eclipse? Also camel casing is a giveaway... ;-) Commented Feb 2, 2011 at 6:43
  • 4
    Is there a reason you want to do this? I believe your two blocks of code are more or less identical, once the Java compiler is done with it. Commented Feb 2, 2011 at 7:07

5 Answers 5

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(); 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

0

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

Comments

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).

2 Comments

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 )
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 : stackoverflow.com/questions/1296571/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.