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

file stats: | LOC: | 69 |  | Methods: | 3
| NCLOC: | 54 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL
UnnecessaryCaseChange.java | 55% | 70% | 100% | 66% |

1 |  | package net.sourceforge.pmd.rules.strings;
2 |  |
3 |  | import net.sourceforge.pmd.AbstractRule;
4 |  | import net.sourceforge.pmd.ast.ASTName;
5 |  | import net.sourceforge.pmd.ast.ASTPrimaryExpression;
6 |  | import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
7 |  | import net.sourceforge.pmd.ast.ASTPrimarySuffix;
8 |  |
9 |  | public class UnnecessaryCaseChange extends AbstractRule {
10 |  |
11 | 4 | public Object visit(ASTPrimaryExpression exp, Object data) {
12 | 4 | if (exp.jjtGetNumChildren() < 4) {
13 | 0 | return data;
14 |  | }
15 |  |
16 | 4 | String first = getBadPrefixOrNull(exp);
17 | 4 | if (first == null) {
18 | 0 | return data;
19 |  | }
20 |  |
21 | 4 | String second = getBadSuffixOrNull(exp);
22 | 4 | if (second == null) {
23 | 0 | return data;
24 |  | }
25 |  |
26 | 4 | if (!(exp.jjtGetChild(1) instanceof ASTPrimarySuffix)) {
27 | 0 | return data;
28 |  | }
29 | 4 | ASTPrimarySuffix methodCall = (ASTPrimarySuffix)exp.jjtGetChild(1);
30 | 4 | if (!methodCall.isArguments() || methodCall.getArgumentCount() > 0) {
31 | 1 | return data;
32 |  | }
33 |  |
34 | 3 | addViolation(data, exp);
35 | 3 | return data;
36 |  | }
37 |  |
38 | 4 | private String getBadPrefixOrNull(ASTPrimaryExpression exp) {
39 |  | // verify PrimaryPrefix/Name[ends-with(@Image, 'toUpperCase']
40 | 4 | if (!(exp.jjtGetChild(0) instanceof ASTPrimaryPrefix)) {
41 | 0 | return null;
42 |  | }
43 |  |
44 | 4 | ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) exp.jjtGetChild(0);
45 | 4 | if (prefix.jjtGetNumChildren() != 1 || !(prefix.jjtGetChild(0) instanceof ASTName)) {
46 | 0 | return null;
47 |  | }
48 |  |
49 | 4 | ASTName name = (ASTName) prefix.jjtGetChild(0);
50 | 4 | if (name.getImage() == null || !(name.getImage().endsWith("toUpperCase") || name.getImage().endsWith("toLowerCase"))) {
51 | 0 | return null;
52 |  | }
53 | 4 | return name.getImage();
54 |  | }
55 |  |
56 | 4 | private String getBadSuffixOrNull(ASTPrimaryExpression exp) {
57 |  | // verify PrimarySuffix[@Image='equals']
58 | 4 | if (!(exp.jjtGetChild(2) instanceof ASTPrimarySuffix)) {
59 | 0 | return null;
60 |  | }
61 |  |
62 | 4 | ASTPrimarySuffix suffix = (ASTPrimarySuffix) exp.jjtGetChild(2);
63 | 4 | if (suffix.getImage() == null || !(suffix.getImage().equals("equals") || suffix.getImage().equals("equalsIgnoreCase"))) {
64 | 0 | return null;
65 |  | }
66 | 4 | return suffix.getImage();
67 |  | }
68 |  |
69 |  | }