String concatenation in for loop inspection gets false positives

Hi, all.

The following code:

for (Iterator iterator = reactions.iterator();
iterator.hasNext();) {
ReactionCluster reactionCluster = (ReactionCluster)
iterator.next();
reactionDependenciesByName.put(reactionCluster.getName(),
getNextReactions(reactionCluster));
if (reactionDependenciesByName.size() % 25 == 0)
logger.warning("processed " + reactionDependenciesByName.size() + "
reactions");
}

throws a warning about string concatenation in a for loop.

I am a bit confused about just what it wants me to do here to fix it -
yes, a string is being concatenated, but that string is also created and
consumed in the same for loop, so it does not produce an endless string,
and a StringBuffer is not really a better choice.

Sure, I can turn the warning off, but this seems a bit aggressive.

Scott

--
Scott Ellsworth
scott@alodar.nospam.com
Java and database consulting for the life sciences

4 comments
Comment actions Permalink

There's a checkbox on the inspection that will turn off reporting in those cases where the concatenation isn't assigned to. For all but the most performance critical code, it's probably reasonable to limit the inspection in that way.

--Dave Griffith

0
Comment actions Permalink

There is an option "Only warn if concatenated string is assigned".

Scott Ellsworth schrieb:

Hi, all.

The following code:

for (Iterator iterator = reactions.iterator();
iterator.hasNext();) {
ReactionCluster reactionCluster = (ReactionCluster)
iterator.next();
reactionDependenciesByName.put(reactionCluster.getName(),
getNextReactions(reactionCluster));
if (reactionDependenciesByName.size() % 25 == 0)
logger.warning("processed " + reactionDependenciesByName.size() + "
reactions");
}

throws a warning about string concatenation in a for loop.

I am a bit confused about just what it wants me to do here to fix it -
yes, a string is being concatenated, but that string is also created and
consumed in the same for loop, so it does not produce an endless string,
and a StringBuffer is not really a better choice.

Sure, I can turn the warning off, but this seems a bit aggressive.

Scott

0
Comment actions Permalink

I turned off all those warnings about string concatenation because the javac automatically converts string concatenation to a series of StringBuilder appends.

With the Java 5.0 javac, it will convert code like:

String data = START_STRING1
+ deviceInfo
+ "')\""
+ START_STRING2
+ '>'
+ deviceName
+ "</a>";

to this: (decompiled)

String data = (new StringBuilder()).append("Start").
append(deviceInfo).
append("')\"").
append("Start2").
append('>').
append(deviceName).
append("</a>").toString();

I find the code much easier to read with the plain '+' string concatenation operator, so now I just leave it alone for sipmle cases where a sequence of strings are concatenated into a local variable.

Now, I find myself wanting an inspection w/ quickfix that would change simple StringBuffer/StringBuilder chained appends back to simple string concatentation for readability, since the compiler will convert it to the optimized form for me.

0
Comment actions Permalink

In article <dd0oek$rem$1@is.intellij.net>,
Sven Steiniger <sven.steiniger@gmx.de> wrote:

There is an option "Only warn if concatenated string is assigned".


'Kay. That helps.

Scott


Scott Ellsworth schrieb:

Hi, all.

The following code:

for (Iterator iterator = reactions.iterator();
iterator.hasNext();) {
ReactionCluster reactionCluster = (ReactionCluster)
iterator.next();
reactionDependenciesByName.put(reactionCluster.getName(),
getNextReactions(reactionCluster));
if (reactionDependenciesByName.size() % 25 == 0)
logger.warning("processed " + reactionDependenciesByName.size() + "
reactions");
}

throws a warning about string concatenation in a for loop.

I am a bit confused about just what it wants me to do here to fix it -
yes, a string is being concatenated, but that string is also created and
consumed in the same for loop, so it does not produce an endless string,
and a StringBuffer is not really a better choice.

Sure, I can turn the warning off, but this seems a bit aggressive.

Scott

>


--
Scott Ellsworth
scott@alodar.nospam.com
Java and database consulting for the life sciences

0

Please sign in to leave a comment.