Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |

file stats: | LOC: | 152 |  | Methods: | 9
| NCLOC: | 124 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL
VariableNamingConventions.java | 71.4% | 83.1% | 100% | 80.2% |

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