1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules; |
5 |
| |
6 |
| import net.sourceforge.pmd.AbstractRule; |
7 |
| import net.sourceforge.pmd.RuleContext; |
8 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
9 |
| import net.sourceforge.pmd.ast.ASTCompilationUnit; |
10 |
| import net.sourceforge.pmd.ast.ASTFieldDeclaration; |
11 |
| import net.sourceforge.pmd.ast.ASTName; |
12 |
| import net.sourceforge.pmd.ast.ASTPrimitiveType; |
13 |
| import net.sourceforge.pmd.ast.ASTType; |
14 |
| import net.sourceforge.pmd.ast.ASTVariableDeclarator; |
15 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
16 |
| import net.sourceforge.pmd.ast.AccessNode; |
17 |
| |
18 |
| import java.util.ArrayList; |
19 |
| import java.util.List; |
20 |
| |
21 |
| public class VariableNamingConventions extends AbstractRule { |
22 |
| |
23 |
| public static final String SEPARATOR = ","; |
24 |
| |
25 |
| private String[] staticPrefix; |
26 |
| private String[] staticSuffix; |
27 |
| private String[] memberPrefix; |
28 |
| private String[] memberSuffix; |
29 |
| |
30 |
10
| public Object visit(ASTCompilationUnit node, Object data) {
|
31 |
10
| init();
|
32 |
10
| return super.visit(node, data);
|
33 |
| } |
34 |
| |
35 |
10
| protected void init() {
|
36 |
10
| staticPrefix = split(getStringProperty("staticPrefix"));
|
37 |
10
| staticSuffix = split(getStringProperty("staticSuffix"));
|
38 |
10
| memberPrefix = split(getStringProperty("memberPrefix"));
|
39 |
10
| memberSuffix = split(getStringProperty("memberSuffix"));
|
40 |
| } |
41 |
| |
42 |
10
| public Object visit(ASTFieldDeclaration node, Object data) {
|
43 |
10
| return checkNames(node, data);
|
44 |
| } |
45 |
| |
46 |
10
| public Object checkNames(AccessNode node, Object data) {
|
47 |
10
| ASTType childNodeType = (ASTType) node.jjtGetChild(0);
|
48 |
10
| String varType = "";
|
49 |
10
| if (childNodeType.jjtGetChild(0) instanceof ASTName) {
|
50 |
0
| varType = ((ASTName) childNodeType.jjtGetChild(0)).getImage();
|
51 |
10
| } else if (childNodeType.jjtGetChild(0) instanceof ASTPrimitiveType) {
|
52 |
10
| varType = ((ASTPrimitiveType) childNodeType.jjtGetChild(0)).getImage();
|
53 |
| } |
54 |
10
| if (varType != null && varType.length() > 0) {
|
55 |
| |
56 |
10
| ASTVariableDeclarator childNodeName = (ASTVariableDeclarator) node.jjtGetChild(1);
|
57 |
10
| ASTVariableDeclaratorId childNodeId = (ASTVariableDeclaratorId) childNodeName.jjtGetChild(0);
|
58 |
10
| String varName = childNodeId.getImage();
|
59 |
| |
60 |
10
| if (varName.equals("serialVersionUID")) {
|
61 |
1
| return data;
|
62 |
| } |
63 |
| |
64 |
| |
65 |
9
| if (node.isFinal() && !node.isStatic()) {
|
66 |
1
| if (node.jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
|
67 |
1
| ASTClassOrInterfaceDeclaration c = (ASTClassOrInterfaceDeclaration)node.jjtGetParent().jjtGetParent().jjtGetParent();
|
68 |
1
| if (!c.isInterface()) {
|
69 |
1
| return data;
|
70 |
| } |
71 |
| } |
72 |
| } |
73 |
| |
74 |
| |
75 |
| |
76 |
8
| if ((node.isStatic() && node.isFinal()) || (node.jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration)node.jjtGetParent().jjtGetParent().jjtGetParent()).isInterface())) {
|
77 |
3
| if (!varName.equals(varName.toUpperCase())) {
|
78 |
2
| RuleContext ctx = (RuleContext) data;
|
79 |
2
| ctx.getReport().addRuleViolation(createRuleViolation(ctx, childNodeName, "Variables that are final and static should be in all caps."));
|
80 |
| } |
81 |
3
| return data;
|
82 |
| } |
83 |
| |
84 |
5
| String strippedVarName = null;
|
85 |
5
| if (node.isStatic()) {
|
86 |
2
| strippedVarName = normalizeStaticVariableName(varName);
|
87 |
| } else { |
88 |
3
| strippedVarName = normalizeMemberVariableName(varName);
|
89 |
| } |
90 |
| |
91 |
5
| if (strippedVarName.indexOf("_") >= 0) {
|
92 |
1
| RuleContext ctx = (RuleContext) data;
|
93 |
1
| ctx.getReport().addRuleViolation(createRuleViolation(ctx, childNodeName, "Variables that are not final should not contain underscores (except for underscores in standard prefix/suffix)."));
|
94 |
| } |
95 |
5
| if (Character.isUpperCase(varName.charAt(0))) {
|
96 |
1
| RuleContext ctx = (RuleContext) data;
|
97 |
1
| ctx.getReport().addRuleViolation(createRuleViolation(ctx, childNodeName, "Variables should start with a lowercase character"));
|
98 |
| } |
99 |
| } |
100 |
5
| return data;
|
101 |
| } |
102 |
| |
103 |
3
| private String normalizeMemberVariableName(String varName) {
|
104 |
3
| String name = stripPrefix(varName, memberPrefix);
|
105 |
3
| return stripSuffix(name, memberSuffix);
|
106 |
| } |
107 |
| |
108 |
2
| private String normalizeStaticVariableName(String varName) {
|
109 |
2
| String name = stripPrefix(varName, staticPrefix);
|
110 |
2
| return stripSuffix(name, staticSuffix);
|
111 |
| } |
112 |
| |
113 |
5
| private String stripSuffix(String varName, String[] suffix) {
|
114 |
5
| if (suffix != null) {
|
115 |
1
| for (int i = 0; i < suffix.length; i++) {
|
116 |
1
| if (varName.endsWith(suffix[i])) {
|
117 |
1
| varName = varName.substring(0, varName.length() - suffix[i].length());
|
118 |
1
| break;
|
119 |
| } |
120 |
| } |
121 |
| } |
122 |
5
| return varName;
|
123 |
| } |
124 |
| |
125 |
5
| private String stripPrefix(String varName, String[] prefix) {
|
126 |
5
| if (prefix == null) {
|
127 |
4
| return varName;
|
128 |
| } |
129 |
1
| for (int i = 0; i < prefix.length; i++) {
|
130 |
1
| if (varName.startsWith(prefix[i])) {
|
131 |
1
| return varName.substring(prefix[i].length());
|
132 |
| } |
133 |
| } |
134 |
0
| return varName;
|
135 |
| } |
136 |
| |
137 |
40
| protected String[] split(String str) {
|
138 |
40
| if (str == null || str.length() == 0) {
|
139 |
38
| return null;
|
140 |
| } |
141 |
| |
142 |
2
| int index = str.indexOf(SEPARATOR);
|
143 |
2
| if (index == -1) {
|
144 |
2
| return new String[]{str};
|
145 |
| } |
146 |
| |
147 |
0
| List list = new ArrayList();
|
148 |
0
| int currPos = 0;
|
149 |
0
| int len = SEPARATOR.length();
|
150 |
0
| while (index != -1) {
|
151 |
0
| list.add(str.substring(currPos, index));
|
152 |
0
| currPos = index + len;
|
153 |
0
| index = str.indexOf(SEPARATOR, currPos);
|
154 |
| } |
155 |
0
| list.add(str.substring(currPos));
|
156 |
0
| return (String[]) list.toArray(new String[list.size()]);
|
157 |
| } |
158 |
| } |