Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 282755 - [quick assist] "Use 'StringBuilder' for string concatenation" could fix existing misuses
Summary: [quick assist] "Use 'StringBuilder' for string concatenation" could fix exist...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.5   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 3.6 M3   Edit
Assignee: Markus Keller CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-07 18:05 EDT by Chris West (Faux) CLA
Modified: 2009-10-12 11:49 EDT (History)
1 user (show)

See Also:
markus.kell.r: review+


Attachments
Proposed implementaiton (10.51 KB, patch)
2009-07-07 18:05 EDT, Chris West (Faux) CLA
no flags Details | Diff
Actual proposed implementaiton (10.81 KB, patch)
2009-07-08 08:23 EDT, Chris West (Faux) CLA
markus.kell.r: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris West (Faux) CLA 2009-07-07 18:05:15 EDT
I've seen a lot of code that looks like:

StringBuilder sb = new StringBuilder();
sb.append("high" + 5);
sb.append("more" + STUFF + "here");

etc.

Currently, Eclipse will refactor this to e.g.:

StringBuilder sb = new StringBuilder();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("high")
stringBuilder.append(5);
sb.append(stringBuilder.toString());
sb.append("more" + STUFF + "here");

...which isn't exactly great (although correct).

The attached patch will make that refactor result in:

StringBuilder sb = new StringBuilder();
sb.append("high")
sb.append(5);
sb.append("more" + STUFF + "here");

..which is a lot closer to what I'd expect.

It only looks for variableName.append(blah blah blah);, i.e. not in expressions, or with an expression on the left.  It works inside single-line ifs, as the original did.

I've not written code against the Eclipse API before, and sacrificed my normal code style in the name of keeping the patch small (ignoring whitespace), buyer beware.
Comment 1 Chris West (Faux) CLA 2009-07-07 18:05:49 EDT
Created attachment 141002 [details]
Proposed implementaiton
Comment 2 Chris West (Faux) CLA 2009-07-08 08:23:17 EDT
Created attachment 141073 [details]
Actual proposed implementaiton
Comment 3 Markus Keller CLA 2009-10-12 11:49:10 EDT
Thanks for the patch. Released to HEAD with some code cleanup (removed 'final' from local variables, made it a bit more type safe, fixed label, ...), and added regression tests.