1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package com.puppycrawl.tools.checkstyle.checks.coding; |
20 | |
|
21 | |
import com.google.common.collect.Lists; |
22 | |
import com.google.common.collect.Maps; |
23 | |
import com.puppycrawl.tools.checkstyle.api.Check; |
24 | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; |
25 | |
import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
26 | |
import com.puppycrawl.tools.checkstyle.api.Utils; |
27 | |
import java.util.BitSet; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
import java.util.Set; |
31 | |
import java.util.regex.Pattern; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class MultipleStringLiteralsCheck extends Check |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 4 | private final Map<String, List<StringInfo>> mStringMap = Maps.newHashMap(); |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 4 | private final BitSet mIgnoreOccurrenceContext = new BitSet(); |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 4 | private int mAllowedDuplicates = 1; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public void setAllowedDuplicates(int aAllowedDuplicates) |
63 | |
{ |
64 | 4 | mAllowedDuplicates = aAllowedDuplicates; |
65 | 4 | } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
private Pattern mPattern; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public MultipleStringLiteralsCheck() |
76 | 4 | { |
77 | 4 | setIgnoreStringsRegexp("^\"\"$"); |
78 | 4 | mIgnoreOccurrenceContext.set(TokenTypes.ANNOTATION); |
79 | 4 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public void setIgnoreStringsRegexp(String aIgnoreStringsRegexp) |
86 | |
{ |
87 | 6 | if ((aIgnoreStringsRegexp != null) |
88 | |
&& (aIgnoreStringsRegexp.length() > 0)) |
89 | |
{ |
90 | 5 | mPattern = Utils.getPattern(aIgnoreStringsRegexp); |
91 | |
} |
92 | |
else { |
93 | 1 | mPattern = null; |
94 | |
} |
95 | 6 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public final void setIgnoreOccurrenceContext(String[] aStrRep) |
102 | |
{ |
103 | 1 | mIgnoreOccurrenceContext.clear(); |
104 | 1 | for (final String s : aStrRep) { |
105 | 0 | final int type = TokenTypes.getTokenId(s); |
106 | 0 | mIgnoreOccurrenceContext.set(type); |
107 | |
} |
108 | 1 | } |
109 | |
|
110 | |
@Override |
111 | |
public int[] getDefaultTokens() |
112 | |
{ |
113 | 4 | return new int[] {TokenTypes.STRING_LITERAL}; |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public void visitToken(DetailAST aAST) |
118 | |
{ |
119 | 76 | if (isInIgnoreOccurrenceContext(aAST)) { |
120 | 12 | return; |
121 | |
} |
122 | 64 | final String currentString = aAST.getText(); |
123 | 64 | if ((mPattern == null) || !mPattern.matcher(currentString).find()) { |
124 | 49 | List<StringInfo> hitList = mStringMap.get(currentString); |
125 | 49 | if (hitList == null) { |
126 | 25 | hitList = Lists.newArrayList(); |
127 | 25 | mStringMap.put(currentString, hitList); |
128 | |
} |
129 | 49 | final int line = aAST.getLineNo(); |
130 | 49 | final int col = aAST.getColumnNo(); |
131 | 49 | hitList.add(new StringInfo(line, col)); |
132 | |
} |
133 | 64 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
private boolean isInIgnoreOccurrenceContext(DetailAST aAST) |
144 | |
{ |
145 | 76 | for (DetailAST token = aAST; |
146 | 512 | token.getParent() != null; |
147 | 436 | token = token.getParent()) |
148 | |
{ |
149 | 448 | final int type = token.getType(); |
150 | 448 | if (mIgnoreOccurrenceContext.get(type)) { |
151 | 12 | return true; |
152 | |
} |
153 | |
} |
154 | 64 | return false; |
155 | |
} |
156 | |
|
157 | |
@Override |
158 | |
public void beginTree(DetailAST aRootAST) |
159 | |
{ |
160 | 4 | super.beginTree(aRootAST); |
161 | 4 | mStringMap.clear(); |
162 | 4 | } |
163 | |
|
164 | |
@Override |
165 | |
public void finishTree(DetailAST aRootAST) |
166 | |
{ |
167 | 4 | final Set<String> keys = mStringMap.keySet(); |
168 | 4 | for (String key : keys) { |
169 | 25 | final List<StringInfo> hits = mStringMap.get(key); |
170 | 25 | if (hits.size() > mAllowedDuplicates) { |
171 | 7 | final StringInfo firstFinding = hits.get(0); |
172 | 7 | final int line = firstFinding.getLine(); |
173 | 7 | final int col = firstFinding.getCol(); |
174 | 7 | log(line, col, "multiple.string.literal", key, hits.size()); |
175 | |
} |
176 | 25 | } |
177 | 4 | } |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | 63 | private static final class StringInfo |
183 | |
{ |
184 | |
|
185 | |
|
186 | |
|
187 | |
private final int mLine; |
188 | |
|
189 | |
|
190 | |
|
191 | |
private final int mCol; |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
private StringInfo(int aLine, int aCol) |
198 | 49 | { |
199 | 49 | mLine = aLine; |
200 | 49 | mCol = aCol; |
201 | 49 | } |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
private int getLine() |
208 | |
{ |
209 | 7 | return mLine; |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
private int getCol() |
217 | |
{ |
218 | 7 | return mCol; |
219 | |
} |
220 | |
} |
221 | |
|
222 | |
} |