1 |
| package net.sourceforge.pmd.rules.strictexception; |
2 |
| |
3 |
| import net.sourceforge.pmd.AbstractRule; |
4 |
| import net.sourceforge.pmd.ast.ASTCompilationUnit; |
5 |
| import net.sourceforge.pmd.ast.ASTConstructorDeclaration; |
6 |
| import net.sourceforge.pmd.ast.ASTImportDeclaration; |
7 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
8 |
| import net.sourceforge.pmd.ast.ASTName; |
9 |
| import net.sourceforge.pmd.ast.Node; |
10 |
| |
11 |
| import java.util.Iterator; |
12 |
| import java.util.List; |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class ExceptionSignatureDeclaration extends AbstractRule { |
22 |
| |
23 |
| private boolean junitImported; |
24 |
| |
25 |
6
| public Object visit(ASTCompilationUnit node, Object o) {
|
26 |
6
| junitImported = false;
|
27 |
6
| return super.visit(node, o);
|
28 |
| } |
29 |
| |
30 |
2
| public Object visit(ASTImportDeclaration node, Object o) {
|
31 |
2
| if (node.getImportedName().indexOf("junit") != -1) {
|
32 |
2
| junitImported = true;
|
33 |
| } |
34 |
2
| return super.visit(node, o);
|
35 |
| } |
36 |
| |
37 |
5
| public Object visit(ASTMethodDeclaration methodDeclaration, Object o) {
|
38 |
5
| if ((methodDeclaration.getMethodName().equals("setUp") || methodDeclaration.getMethodName().equals("tearDown")) && junitImported) {
|
39 |
2
| return super.visit(methodDeclaration, o);
|
40 |
| } |
41 |
| |
42 |
3
| List exceptionList = methodDeclaration.findChildrenOfType(ASTName.class);
|
43 |
3
| if (!hasContent(exceptionList)) {
|
44 |
2
| return super.visit(methodDeclaration, o);
|
45 |
| } |
46 |
| |
47 |
1
| evaluateExceptions(exceptionList, o);
|
48 |
1
| return super.visit(methodDeclaration, o);
|
49 |
| } |
50 |
| |
51 |
| |
52 |
1
| public Object visit(ASTConstructorDeclaration constructorDeclaration, Object o) {
|
53 |
1
| List exceptionList = constructorDeclaration.findChildrenOfType(ASTName.class);
|
54 |
1
| if (!hasContent(exceptionList)) {
|
55 |
0
| return super.visit(constructorDeclaration, o);
|
56 |
| } |
57 |
| |
58 |
1
| evaluateExceptions(exceptionList, o);
|
59 |
1
| return super.visit(constructorDeclaration, o);
|
60 |
| } |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
2
| private void evaluateExceptions(List exceptionList, Object context) {
|
69 |
2
| ASTName exception;
|
70 |
2
| for (Iterator iter = exceptionList.iterator(); iter.hasNext();) {
|
71 |
2
| exception = (ASTName) iter.next();
|
72 |
2
| if (hasDeclaredExceptionInSignature(exception)) {
|
73 |
2
| addViolation(context, exception);
|
74 |
| } |
75 |
| } |
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
2
| private boolean hasDeclaredExceptionInSignature(ASTName exception) {
|
86 |
2
| return exception.getImage().equals("Exception") && isParentSignatureDeclaration(exception);
|
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
2
| private boolean isParentSignatureDeclaration(ASTName exception) {
|
94 |
2
| Node parent = exception.jjtGetParent().jjtGetParent();
|
95 |
2
| return parent instanceof ASTMethodDeclaration || parent instanceof ASTConstructorDeclaration;
|
96 |
| } |
97 |
| |
98 |
4
| private boolean hasContent(List nameList) {
|
99 |
4
| return nameList != null && nameList.size() > 0;
|
100 |
| } |
101 |
| } |