|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package net.sourceforge.pmd.rules; |
|
8 |
| |
|
9 |
| import net.sourceforge.pmd.AbstractRule; |
|
10 |
| import net.sourceforge.pmd.RuleContext; |
|
11 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
|
12 |
| import net.sourceforge.pmd.ast.ASTConstructorDeclaration; |
|
13 |
| import net.sourceforge.pmd.ast.ASTFieldDeclaration; |
|
14 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
|
15 |
| import net.sourceforge.pmd.ast.ASTMethodDeclarator; |
|
16 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
|
17 |
| import org.jaxen.JaxenException; |
|
18 |
| |
|
19 |
| import java.text.MessageFormat; |
|
20 |
| import java.util.List; |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| public class SingularField extends AbstractRule { |
|
27 |
| |
|
28 |
7
| public Object visit(ASTFieldDeclaration node, Object data) {
|
|
29 |
7
| if (node.isPrivate() && !node.isStatic()) {
|
|
30 |
3
| List list = node.findChildrenOfType(ASTVariableDeclaratorId.class);
|
|
31 |
3
| ASTVariableDeclaratorId decl = (ASTVariableDeclaratorId)list.get(0);
|
|
32 |
3
| String name = decl.getImage();
|
|
33 |
3
| String path = "//MethodDeclaration[.//PrimaryExpression[.//Name[@Image = \""+name+"\" or substring-before(@Image, \".\") = \""+name+"\"] or .//PrimarySuffix[@Image = \""+name+"\"]]] |" +
|
|
34 |
| "//ConstructorDeclaration[.//PrimaryExpression[.//Name[@Image = \""+name+"\" or substring-before(@Image, \".\") = \""+name+"\"] or .//PrimarySuffix[@Image = \""+name+"\"]]]"; |
|
35 |
3
| try {
|
|
36 |
3
| List nodes = node.findChildNodesWithXPath(path);
|
|
37 |
3
| if (nodes.size() == 1) {
|
|
38 |
1
| String method;
|
|
39 |
1
| if (nodes.get(0) instanceof ASTMethodDeclaration) {
|
|
40 |
1
| method = ((ASTMethodDeclarator)((ASTMethodDeclaration)nodes.get(0)).findChildrenOfType(ASTMethodDeclarator.class).get(0)).getImage();
|
|
41 |
| } else { |
|
42 |
0
| method = ((ASTClassOrInterfaceDeclaration)((ASTConstructorDeclaration)nodes.get(0)).getFirstParentOfType(ASTClassOrInterfaceDeclaration.class)).getImage();
|
|
43 |
| } |
|
44 |
1
| ((RuleContext)data).getReport().addRuleViolation(createRuleViolation((RuleContext) data, decl, MessageFormat.format(getMessage(), new Object[]{name, method})));
|
|
45 |
| } |
|
46 |
| } catch (JaxenException je) { |
|
47 |
0
| je.printStackTrace();
|
|
48 |
| } |
|
49 |
| } |
|
50 |
7
| return data;
|
|
51 |
| } |
|
52 |
| |
|
53 |
| } |