forked from phoedos/pmd
1573795 - PreserveStackTrace throws ClassCastException
Code didn't take into account throws with 0 args. Fixed. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4605 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -7,6 +7,7 @@ Fixed bug 1570915 - AvoidRethrowingException no longer reports a false positive
|
||||
Fixed bug 1571324 - UselessStringValueOf no longer reports a false positive for additive expressions.
|
||||
Fixed CSVRenderer - had flipped line and priority columns
|
||||
Applied patch 1551189 - SingularField false + for initialization blocks
|
||||
Fixed bug 1573795 - PreserveStackTrace doesn't throw CastClassException on exception with 0 args
|
||||
|
||||
October 4, 2006 - 3.8:
|
||||
New rules:
|
||||
|
@ -31,6 +31,8 @@ public class PreserveStackTraceTest extends SimpleAggregatorTst {
|
||||
new TestDescriptor(TEST9_OK, "9, Excetion is cast, OK", 0, rule),
|
||||
new TestDescriptor(TEST10_OK, "10, Throwing new Exception, OK", 0, rule),
|
||||
new TestDescriptor(TEST11_OK, "11, Throwing new Exception, OK", 0, rule),
|
||||
new TestDescriptor(TEST12_OK, "12, Catch and throw RuntimeException", 0, rule),
|
||||
new TestDescriptor(TEST13_FAIL, "13, Catch and throw RuntimeException, fail", 1, rule),
|
||||
});
|
||||
}
|
||||
|
||||
@ -166,4 +168,27 @@ public class PreserveStackTraceTest extends SimpleAggregatorTst {
|
||||
" }" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
public static final String TEST12_OK =
|
||||
"public class B {" + PMD.EOL +
|
||||
"public void bla(){" + PMD.EOL +
|
||||
"try {" + PMD.EOL +
|
||||
";" + PMD.EOL +
|
||||
"} catch (IllegalStateException e) {" + PMD.EOL +
|
||||
"throw new RuntimeException(e);" + PMD.EOL +
|
||||
"}" + PMD.EOL +
|
||||
"}" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
public static final String TEST13_FAIL =
|
||||
"public class B {" + PMD.EOL +
|
||||
"public void bla(){" + PMD.EOL +
|
||||
"try {" + PMD.EOL +
|
||||
";" + PMD.EOL +
|
||||
"} catch (IllegalStateException e) {" + PMD.EOL +
|
||||
"throw new RuntimeException();" + PMD.EOL +
|
||||
"}" + PMD.EOL +
|
||||
"}" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.ast.ASTArgumentList;
|
||||
import net.sourceforge.pmd.ast.ASTCastExpression;
|
||||
import net.sourceforge.pmd.ast.ASTCatchStatement;
|
||||
import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
|
||||
import net.sourceforge.pmd.ast.ASTName;
|
||||
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
|
||||
import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
|
||||
@ -19,7 +20,7 @@ import java.util.Map;
|
||||
|
||||
public class PreserveStackTrace extends AbstractRule {
|
||||
|
||||
public Object visit(ASTCatchStatement node, Object data) {
|
||||
public Object visit(ASTCatchStatement node, Object data) {
|
||||
String target = (((SimpleNode) node.jjtGetChild(0).jjtGetChild(1)).getImage());
|
||||
List lstThrowStatements = node.findChildrenOfType(ASTThrowStatement.class);
|
||||
for (Iterator iter = lstThrowStatements.iterator(); iter.hasNext();) {
|
||||
@ -40,19 +41,22 @@ public class PreserveStackTrace extends AbstractRule {
|
||||
} else if (args == null) {
|
||||
SimpleNode child = (SimpleNode) throwStatement.jjtGetChild(0);
|
||||
while (child != null && child.jjtGetNumChildren() > 0
|
||||
&& !child.getClass().getName().equals("net.sourceforge.pmd.ast.ASTName")) {
|
||||
&& !child.getClass().equals(ASTName.class)) {
|
||||
child = (SimpleNode) child.jjtGetChild(0);
|
||||
}
|
||||
if (child != null
|
||||
&& (!target.equals(child.getImage()) && !child.getImage().equals(target + ".fillInStackTrace"))) {
|
||||
Map vars = ((ASTName) child).getScope().getVariableDeclarations();
|
||||
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
|
||||
args = (ASTArgumentList) ((SimpleNode) decl.getNode().jjtGetParent())
|
||||
.getFirstChildOfType(ASTArgumentList.class);
|
||||
if (args != null) {
|
||||
ck(data, target, throwStatement, args);
|
||||
}
|
||||
if (child != null){
|
||||
if( child.getClass().equals(ASTName.class) && (!target.equals(child.getImage()) && !child.getImage().equals(target + ".fillInStackTrace"))) {
|
||||
Map vars = ((ASTName) child).getScope().getVariableDeclarations();
|
||||
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
|
||||
args = (ASTArgumentList) ((SimpleNode) decl.getNode().jjtGetParent())
|
||||
.getFirstChildOfType(ASTArgumentList.class);
|
||||
if (args != null) {
|
||||
ck(data, target, throwStatement, args);
|
||||
}
|
||||
}
|
||||
} else if(child.getClass().equals(ASTClassOrInterfaceType.class)){
|
||||
addViolation((RuleContext) data, throwStatement);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -60,15 +64,17 @@ public class PreserveStackTrace extends AbstractRule {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
private void ck(Object data, String target, ASTThrowStatement throwStatement, ASTArgumentList args) {
|
||||
try {
|
||||
List lst = args.findChildNodesWithXPath("//Name[@Image='" + target + "']");
|
||||
if (lst.size() == 0) {
|
||||
RuleContext ctx = (RuleContext) data;
|
||||
addViolation(ctx, throwStatement);
|
||||
}
|
||||
} catch (JaxenException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
private void ck(Object data, String target,
|
||||
ASTThrowStatement throwStatement, ASTArgumentList args) {
|
||||
try {
|
||||
List lst = args.findChildNodesWithXPath("//Name[@Image='" + target
|
||||
+ "']");
|
||||
if (lst.size() == 0) {
|
||||
RuleContext ctx = (RuleContext) data;
|
||||
addViolation(ctx, throwStatement);
|
||||
}
|
||||
} catch (JaxenException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user