1 |
| package net.sourceforge.pmd.rules.design; |
2 |
| |
3 |
| import net.sourceforge.pmd.AbstractRule; |
4 |
| import net.sourceforge.pmd.RuleContext; |
5 |
| import net.sourceforge.pmd.ast.ASTArgumentList; |
6 |
| import net.sourceforge.pmd.ast.ASTCastExpression; |
7 |
| import net.sourceforge.pmd.ast.ASTCatchStatement; |
8 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
9 |
| import net.sourceforge.pmd.ast.ASTPrimaryPrefix; |
10 |
| import net.sourceforge.pmd.ast.ASTThrowStatement; |
11 |
| import net.sourceforge.pmd.ast.SimpleNode; |
12 |
| |
13 |
| import java.util.Iterator; |
14 |
| import java.util.List; |
15 |
| |
16 |
| import org.jaxen.JaxenException; |
17 |
| |
18 |
| public class PreserveStackTrace extends AbstractRule { |
19 |
| |
20 |
9
| public Object visit(ASTCatchStatement node, Object data) {
|
21 |
9
| String target = (((SimpleNode) node.jjtGetChild(0).jjtGetChild(1)).getImage());
|
22 |
9
| List lstThrowStatements = node.findChildrenOfType(ASTThrowStatement.class);
|
23 |
9
| for (Iterator iter = lstThrowStatements.iterator(); iter.hasNext();) {
|
24 |
7
| ASTThrowStatement throwStatement = (ASTThrowStatement) iter.next();
|
25 |
7
| SimpleNode sn = (SimpleNode)throwStatement.jjtGetChild(0).jjtGetChild(0);
|
26 |
7
| if(sn.getClass().equals(ASTCastExpression.class)){
|
27 |
2
| ASTPrimaryExpression expr = (ASTPrimaryExpression)sn.jjtGetChild(1);
|
28 |
2
| if(expr.jjtGetNumChildren()> 1 && expr.jjtGetChild(1).getClass().equals(ASTPrimaryPrefix.class)) {
|
29 |
0
| RuleContext ctx = (RuleContext) data;
|
30 |
0
| addViolation(ctx, throwStatement);
|
31 |
| } |
32 |
2
| continue;
|
33 |
| } |
34 |
5
| ASTArgumentList args = (ASTArgumentList) throwStatement.getFirstChildOfType(ASTArgumentList.class);
|
35 |
| |
36 |
5
| if (args != null) {
|
37 |
4
| try {
|
38 |
4
| List lst = args.findChildNodesWithXPath("//Name[@Image='" + target + "']");
|
39 |
4
| if (lst.size() == 0) {
|
40 |
1
| RuleContext ctx = (RuleContext) data;
|
41 |
1
| addViolation(ctx, throwStatement);
|
42 |
| } |
43 |
| } catch (JaxenException e) { |
44 |
0
| e.printStackTrace();
|
45 |
| } |
46 |
1
| } else if (args == null) {
|
47 |
1
| SimpleNode child = (SimpleNode) throwStatement.jjtGetChild(0);
|
48 |
1
| while (child != null && child.jjtGetNumChildren() > 0 && !child.getClass().getName().equals("net.sourceforge.pmd.ast.ASTName")) {
|
49 |
3
| child = (SimpleNode) child.jjtGetChild(0);
|
50 |
| } |
51 |
1
| if (child != null && (!target.equals(child.getImage()) && !child.getImage().equals(target + ".fillInStackTrace"))) {
|
52 |
0
| RuleContext ctx = (RuleContext) data;
|
53 |
0
| addViolation(ctx, throwStatement);
|
54 |
| } |
55 |
| } |
56 |
| } |
57 |
9
| return super.visit(node, data);
|
58 |
| } |
59 |
| } |