1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules.strings; |
5 |
| |
6 |
| import net.sourceforge.pmd.AbstractRule; |
7 |
| import net.sourceforge.pmd.ast.ASTBlockStatement; |
8 |
| import net.sourceforge.pmd.ast.ASTLiteral; |
9 |
| |
10 |
| import java.util.regex.Pattern; |
11 |
| import java.util.regex.Matcher; |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| public class AppendCharacterWithChar extends AbstractRule { |
25 |
| |
26 |
| private static final Pattern REGEX = Pattern.compile("\"[\\\\]?[\\s\\S]\""); |
27 |
| |
28 |
14
| public Object visit(ASTLiteral node, Object data) {
|
29 |
14
| ASTBlockStatement bs = (ASTBlockStatement) node
|
30 |
| .getFirstParentOfType(ASTBlockStatement.class); |
31 |
14
| if (bs == null) {
|
32 |
0
| return data;
|
33 |
| } |
34 |
| |
35 |
14
| String str = node.getImage();
|
36 |
14
| if (str == null || str.length() < 3 || str.length() > 4) {
|
37 |
2
| return data;
|
38 |
| } |
39 |
| |
40 |
12
| Matcher matcher = REGEX.matcher(str);
|
41 |
12
| if (matcher.find()) {
|
42 |
8
| if (!InefficientStringBuffering.isInStringBufferOperation(node, 8, "append")) {
|
43 |
2
| return data;
|
44 |
| } |
45 |
6
| addViolation(data, node);
|
46 |
| } |
47 |
10
| return data;
|
48 |
| } |
49 |
| } |