|
1 |
| package net.sourceforge.pmd.rules; |
|
2 |
| |
|
3 |
| import net.sourceforge.pmd.AbstractRule; |
|
4 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
|
5 |
| import net.sourceforge.pmd.ast.ASTFieldDeclaration; |
|
6 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
|
7 |
| import net.sourceforge.pmd.ast.Node; |
|
8 |
| import net.sourceforge.pmd.ast.SimpleNode; |
|
9 |
| |
|
10 |
| public class UnusedModifier extends AbstractRule { |
|
11 |
| |
|
12 |
34
| public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
|
13 |
34
| if (!node.isInterface() && node.isNested() && (node.isPublic() || node.isStatic())) {
|
|
14 |
2
| ASTClassOrInterfaceDeclaration parent = (ASTClassOrInterfaceDeclaration) node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
|
15 |
2
| if (parent != null && parent.isInterface()) {
|
|
16 |
2
| addViolation(data, node, getMessage());
|
|
17 |
| } |
|
18 |
32
| } else if (node.isInterface() && node.isNested() && (node.isPublic() || node.isStatic())) {
|
|
19 |
5
| ASTClassOrInterfaceDeclaration parent = (ASTClassOrInterfaceDeclaration) node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
|
20 |
5
| if (parent.isInterface() || (!parent.isInterface() && node.isStatic())) {
|
|
21 |
4
| addViolation(data, node, getMessage());
|
|
22 |
| } |
|
23 |
| } |
|
24 |
34
| return super.visit(node, data);
|
|
25 |
| } |
|
26 |
| |
|
27 |
6
| public Object visit(ASTMethodDeclaration node, Object data) {
|
|
28 |
6
| if (node.isSyntacticallyPublic() || node.isSyntacticallyAbstract()) {
|
|
29 |
5
| check(node, data);
|
|
30 |
| } |
|
31 |
6
| return super.visit(node, data);
|
|
32 |
| } |
|
33 |
| |
|
34 |
9
| public Object visit(ASTFieldDeclaration node, Object data) {
|
|
35 |
9
| if (node.isSyntacticallyPublic() || node.isSyntacticallyStatic() || node.isSyntacticallyFinal()) {
|
|
36 |
6
| check(node, data);
|
|
37 |
| } |
|
38 |
9
| return super.visit(node, data);
|
|
39 |
| } |
|
40 |
| |
|
41 |
11
| private void check(SimpleNode fieldOrMethod, Object data) {
|
|
42 |
| |
|
43 |
| |
|
44 |
11
| Node parent = fieldOrMethod.jjtGetParent().jjtGetParent().jjtGetParent();
|
|
45 |
11
| if (parent instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
|
|
46 |
7
| addViolation(data, fieldOrMethod);
|
|
47 |
| } |
|
48 |
| } |
|
49 |
| |
|
50 |
| } |