1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules; |
5 |
| |
6 |
| import java.util.List; |
7 |
| import java.util.ArrayList; |
8 |
| |
9 |
| import net.sourceforge.pmd.AbstractRule; |
10 |
| import net.sourceforge.pmd.ast.ASTArgumentList; |
11 |
| import net.sourceforge.pmd.ast.ASTArguments; |
12 |
| import net.sourceforge.pmd.ast.ASTBlock; |
13 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
14 |
| import net.sourceforge.pmd.ast.ASTFormalParameter; |
15 |
| import net.sourceforge.pmd.ast.ASTFormalParameters; |
16 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
17 |
| import net.sourceforge.pmd.ast.ASTMethodDeclarator; |
18 |
| import net.sourceforge.pmd.ast.ASTName; |
19 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
20 |
| import net.sourceforge.pmd.ast.ASTPrimaryPrefix; |
21 |
| import net.sourceforge.pmd.ast.ASTPrimarySuffix; |
22 |
| import net.sourceforge.pmd.ast.ASTStatement; |
23 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
24 |
| import net.sourceforge.pmd.ast.Node; |
25 |
| import net.sourceforge.pmd.ast.SimpleNode; |
26 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceBodyDeclaration; |
27 |
| |
28 |
| public class UselessOverridingMethod extends AbstractRule { |
29 |
| |
30 |
12
| public Object visit(ASTClassOrInterfaceDeclaration clz, Object data) {
|
31 |
12
| if (clz.isInterface()) {
|
32 |
1
| return data;
|
33 |
| } |
34 |
11
| return super.visit(clz, data);
|
35 |
| } |
36 |
| |
37 |
11
| public Object visit(ASTMethodDeclaration node, Object data) {
|
38 |
| |
39 |
| |
40 |
| |
41 |
11
| if (node.isAbstract() || node.isFinal() || node.isNative()) {
|
42 |
1
| return super.visit(node, data);
|
43 |
| } |
44 |
| |
45 |
10
| ASTBlock block = node.getBlock();
|
46 |
| |
47 |
10
| if (block.jjtGetNumChildren()!=1 || block.findChildrenOfType(ASTStatement.class).size()!=1)
|
48 |
0
| return super.visit(node, data);
|
49 |
| |
50 |
10
| ASTStatement statement = (ASTStatement)block.jjtGetChild(0).jjtGetChild(0);
|
51 |
10
| if (statement.jjtGetChild(0).jjtGetNumChildren() == 0) {
|
52 |
1
| return data;
|
53 |
| } |
54 |
9
| SimpleNode statementGrandChild = (SimpleNode)statement.jjtGetChild(0).jjtGetChild(0);
|
55 |
9
| ASTPrimaryExpression primaryExpression;
|
56 |
| |
57 |
9
| if (statementGrandChild instanceof ASTPrimaryExpression)
|
58 |
6
| primaryExpression = (ASTPrimaryExpression) statementGrandChild;
|
59 |
| else { |
60 |
3
| List primaryExpressions = findFirstDegreeChildrenOfType(statementGrandChild, ASTPrimaryExpression.class);
|
61 |
3
| if (primaryExpressions.size()!=1)
|
62 |
1
| return super.visit(node, data);
|
63 |
2
| primaryExpression = (ASTPrimaryExpression)primaryExpressions.get(0);
|
64 |
| } |
65 |
| |
66 |
8
| ASTPrimaryPrefix primaryPrefix = (ASTPrimaryPrefix)findFirstDegreeChildrenOfType(primaryExpression, ASTPrimaryPrefix.class).get(0);
|
67 |
8
| if (!primaryPrefix.usesSuperModifier())
|
68 |
0
| return super.visit(node, data);
|
69 |
| |
70 |
8
| ASTMethodDeclarator methodDeclarator = (ASTMethodDeclarator)findFirstDegreeChildrenOfType(node, ASTMethodDeclarator.class).get(0);
|
71 |
8
| if (!primaryPrefix.getImage().equals(methodDeclarator.getImage()))
|
72 |
0
| return super.visit(node, data);
|
73 |
| |
74 |
| |
75 |
8
| ASTPrimarySuffix primarySuffix = (ASTPrimarySuffix)findFirstDegreeChildrenOfType(primaryExpression, ASTPrimarySuffix.class).get(0);
|
76 |
8
| ASTArguments arguments = (ASTArguments)primarySuffix.jjtGetChild(0);
|
77 |
8
| ASTFormalParameters formalParameters = (ASTFormalParameters)methodDeclarator.jjtGetChild(0);
|
78 |
8
| if (formalParameters.jjtGetNumChildren() != arguments.jjtGetNumChildren())
|
79 |
1
| return super.visit(node, data);
|
80 |
| |
81 |
7
| if (arguments.jjtGetNumChildren()==0)
|
82 |
2
| addViolation(data, node, getMessage());
|
83 |
| else { |
84 |
5
| ASTArgumentList argumentList = (ASTArgumentList)arguments.jjtGetChild(0);
|
85 |
5
| for (int i = 0; i < argumentList.jjtGetNumChildren(); i++) {
|
86 |
5
| Node ExpressionChild = argumentList.jjtGetChild(i).jjtGetChild(0);
|
87 |
5
| if (!(ExpressionChild instanceof ASTPrimaryExpression) || ExpressionChild.jjtGetNumChildren()!=1)
|
88 |
2
| return super.visit(node, data);
|
89 |
| |
90 |
3
| ASTPrimaryExpression argumentPrimaryExpression = (ASTPrimaryExpression)ExpressionChild;
|
91 |
3
| ASTPrimaryPrefix argumentPrimaryPrefix = (ASTPrimaryPrefix)argumentPrimaryExpression.jjtGetChild(0);
|
92 |
3
| Node argumentPrimaryPrefixChild = argumentPrimaryPrefix.jjtGetChild(0);
|
93 |
3
| if (!(argumentPrimaryPrefixChild instanceof ASTName))
|
94 |
0
| return super.visit(node, data);
|
95 |
| |
96 |
3
| ASTName argumentName = (ASTName)argumentPrimaryPrefixChild;
|
97 |
3
| ASTFormalParameter formalParameter = (ASTFormalParameter)formalParameters.jjtGetChild(i);
|
98 |
3
| ASTVariableDeclaratorId variableId = (ASTVariableDeclaratorId)findFirstDegreeChildrenOfType(formalParameter, ASTVariableDeclaratorId.class).get(0);
|
99 |
3
| if (!argumentName.getImage().equals(variableId.getImage()))
|
100 |
1
| return super.visit(node, data);
|
101 |
| |
102 |
| } |
103 |
2
| addViolation(data, node, getMessage());
|
104 |
| } |
105 |
4
| return super.visit(node, data);
|
106 |
| } |
107 |
| |
108 |
30
| public List findFirstDegreeChildrenOfType(SimpleNode n, Class targetType) {
|
109 |
30
| List l = new ArrayList();
|
110 |
30
| lclFindChildrenOfType(n, targetType, l, 0);
|
111 |
30
| return l;
|
112 |
| } |
113 |
| |
114 |
30
| private void lclFindChildrenOfType(Node node, Class targetType, List results, int depth) {
|
115 |
30
| if (node.getClass().equals(targetType)) {
|
116 |
0
| results.add(node);
|
117 |
| } |
118 |
| |
119 |
30
| if (node instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) node).isNested()) {
|
120 |
0
| return;
|
121 |
| } |
122 |
| |
123 |
30
| if (node instanceof ASTClassOrInterfaceBodyDeclaration && ((ASTClassOrInterfaceBodyDeclaration) node).isAnonymousInnerClass()) {
|
124 |
0
| return;
|
125 |
| } |
126 |
| |
127 |
30
| for (int i = 0; i < node.jjtGetNumChildren(); i++) {
|
128 |
65
| Node child = node.jjtGetChild(i);
|
129 |
65
| if (child.getClass().equals(targetType)) {
|
130 |
29
| results.add(child);
|
131 |
| } |
132 |
| } |
133 |
| } |
134 |
| } |