Better detection of corner cases

the firts changes checks for a exact level to search AdditiveExpression.

So code like
throw new Exception("something bad:" + (e)); gets undetected.

This change search if part of a AdditiveExpression to the base node.
This commit is contained in:
Alberto Fernandez
2018-01-17 15:25:38 +01:00
parent a4d5ff683d
commit 883bed3cf8
2 changed files with 26 additions and 6 deletions

View File

@ -140,9 +140,8 @@ public class PreserveStackTraceRule extends AbstractJavaRule {
if (target != null && baseNode != null) {
List<ASTName> nameNodes = baseNode.findDescendantsOfType(ASTName.class);
for (ASTName nameNode : nameNodes) {
if (target.equals(nameNode.getImage())) {
boolean isPartOfStringConcatenation =
(nameNode.jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTAdditiveExpression);
if (target.equals(nameNode.getImage())) {
boolean isPartOfStringConcatenation = isStringConcat(nameNode, baseNode);
if (!isPartOfStringConcatenation) {
match = true;
break;
@ -152,6 +151,23 @@ public class PreserveStackTraceRule extends AbstractJavaRule {
}
return match;
}
/**
* Checks whether the given childNode is part of an additive expression (String concatenation) limiting search to base Node.
* @param childNode
* @param baseNode
* @return
*/
private boolean isStringConcat(Node childNode, Node baseNode) {
Node currentNode = childNode;
// limit to 10 levels
for (int i = 0; i < 10 && currentNode != null && currentNode != baseNode; i++) {
currentNode = currentNode.jjtGetParent();
if (currentNode instanceof ASTAdditiveExpression) {
return true;
}
}
return false;
}
private void ck(Object data, String target, ASTThrowStatement throwStatement, Node baseNode) {
if (!checkForTargetUsage(target, baseNode)) {

View File

@ -542,14 +542,18 @@ public class Bug {
<test-code>
<description>#543 False negative with String concatenation</description>
<expected-problems>1</expected-problems>
<expected-problems>3</expected-problems>
<code><![CDATA[
public class Foo {
public void foo(String a) throws Exception {
try {
int i = Integer.parseInt(a);
} catch(Exception e){
throw new Exception("something bad:" + e);
} catch(java.io.FileNotFoundException e) {
throw new Exception("file not found:" + e.toString());
} catch(java.io.IOException e) {
throw new Exception("I/O error:" + e);
} catch(Exception e) {
throw new Exception("something bad:" + (e));
}
}
}