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