|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.rules; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.AbstractRule; |
|
7 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
|
8 |
| import net.sourceforge.pmd.symboltable.NameOccurrence; |
|
9 |
| import net.sourceforge.pmd.symboltable.VariableNameDeclaration; |
|
10 |
| |
|
11 |
| import java.util.Iterator; |
|
12 |
| import java.util.List; |
|
13 |
| import java.util.Map; |
|
14 |
| |
|
15 |
| public class UnusedPrivateFieldRule extends AbstractRule { |
|
16 |
| |
|
17 |
28
| public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
|
18 |
28
| Map vars = node.getScope().getVariableDeclarations();
|
|
19 |
28
| for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
|
|
20 |
25
| Map.Entry entry = (Map.Entry) i.next();
|
|
21 |
25
| VariableNameDeclaration decl = (VariableNameDeclaration) entry.getKey();
|
|
22 |
25
| if (!decl.getAccessNodeParent().isPrivate() || isOK(decl.getImage())) {
|
|
23 |
4
| continue;
|
|
24 |
| } |
|
25 |
21
| if (!actuallyUsed((List) entry.getValue())) {
|
|
26 |
10
| addViolation(data, decl.getNode(), decl.getImage());
|
|
27 |
| } |
|
28 |
| } |
|
29 |
28
| return super.visit(node, data);
|
|
30 |
| } |
|
31 |
| |
|
32 |
21
| private boolean actuallyUsed(List usages) {
|
|
33 |
21
| for (Iterator j = usages.iterator(); j.hasNext();) {
|
|
34 |
18
| NameOccurrence nameOccurrence = (NameOccurrence) j.next();
|
|
35 |
18
| if (!nameOccurrence.isOnLeftHandSide()) {
|
|
36 |
11
| return true;
|
|
37 |
| } |
|
38 |
| } |
|
39 |
10
| return false;
|
|
40 |
| } |
|
41 |
| |
|
42 |
22
| private boolean isOK(String image) {
|
|
43 |
22
| return image.equals("serialVersionUID") || image.equals("serialPersistentFields") || image.equals("IDENT");
|
|
44 |
| } |
|
45 |
| } |