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 |
| import org.apache.oro.text.perl.Perl5Util; |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| public class AppendCharacterWithChar extends AbstractRule { |
23 |
| |
24 |
| private static final String REGEX = "/\"[\\\\]?[\\s\\S]\"/i"; |
25 |
| |
26 |
14
| public Object visit(ASTLiteral node, Object data) {
|
27 |
14
| ASTBlockStatement bs = (ASTBlockStatement) node
|
28 |
| .getFirstParentOfType(ASTBlockStatement.class); |
29 |
14
| if (bs == null) {
|
30 |
0
| return data;
|
31 |
| } |
32 |
| |
33 |
14
| String str = node.getImage();
|
34 |
14
| if (str == null || str.length() < 3 || str.length() > 4) {
|
35 |
2
| return data;
|
36 |
| } |
37 |
| |
38 |
| |
39 |
| |
40 |
12
| Perl5Util regexp = new Perl5Util();
|
41 |
12
| if (regexp.match(REGEX, str)) {
|
42 |
8
| if (!InefficientStringBuffering.isInStringBufferAppend(node, 8)) {
|
43 |
2
| return data;
|
44 |
| } |
45 |
6
| addViolation(data, node);
|
46 |
| } |
47 |
10
| return data;
|
48 |
| } |
49 |
| } |