1
2
3
4 package net.sourceforge.pmd.lang.java.rule.design;
5
6 import net.sourceforge.pmd.lang.ast.Node;
7 import net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression;
8 import net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression;
9 import net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression;
10 import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
11 import net.sourceforge.pmd.lang.java.ast.ASTExpression;
12 import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
13 import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
14 import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
15 import net.sourceforge.pmd.lang.java.ast.ASTUnaryExpressionNotPlusMinus;
16 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class ConfusingTernaryRule extends AbstractJavaRule {
40
41 public Object visit(ASTIfStatement node, Object data) {
42
43 if (node.jjtGetNumChildren() == 3) {
44 Node inode = node.jjtGetChild(0);
45 if (inode instanceof ASTExpression &&
46 inode.jjtGetNumChildren() == 1) {
47 Node jnode = inode.jjtGetChild(0);
48 if (isMatch(jnode)) {
49 addViolation(data, node);
50 }
51 }
52 }
53 return super.visit(node, data);
54 }
55
56 public Object visit(ASTConditionalExpression node, Object data) {
57
58 if (node.jjtGetNumChildren() > 0) {
59 Node inode = node.jjtGetChild(0);
60 if (isMatch(inode)) {
61 addViolation(data, node);
62 }
63 }
64 return super.visit(node, data);
65 }
66
67
68 private static boolean isMatch(Node node) {
69 return
70 isUnaryNot(node) ||
71 isNotEquals(node) ||
72 isConditionalWithAllMatches(node) ||
73 isParenthesisAroundMatch(node);
74 }
75
76 private static boolean isUnaryNot(Node node) {
77
78 return
79 node instanceof ASTUnaryExpressionNotPlusMinus &&
80 "!".equals(node.getImage());
81 }
82
83 private static boolean isNotEquals(Node node) {
84
85 return
86 node instanceof ASTEqualityExpression &&
87 "!=".equals(node.getImage());
88 }
89
90 private static boolean isConditionalWithAllMatches(Node node) {
91
92 if (!(node instanceof ASTConditionalAndExpression) &&
93 !(node instanceof ASTConditionalOrExpression)) {
94 return false;
95 }
96 int n = node.jjtGetNumChildren();
97 if (n <= 0) {
98 return false;
99 }
100 for (int i = 0; i < n; i++) {
101 Node inode = node.jjtGetChild(i);
102
103 if (!isMatch(inode)) {
104 return false;
105 }
106 }
107
108 return true;
109 }
110
111 private static boolean isParenthesisAroundMatch(Node node) {
112
113 if (!(node instanceof ASTPrimaryExpression) ||
114 (node.jjtGetNumChildren() != 1)) {
115 return false;
116 }
117 Node inode = node.jjtGetChild(0);
118 if (!(inode instanceof ASTPrimaryPrefix) ||
119 (inode.jjtGetNumChildren() != 1)) {
120 return false;
121 }
122 Node jnode = inode.jjtGetChild(0);
123 if (!(jnode instanceof ASTExpression) ||
124 (jnode.jjtGetNumChildren() != 1)) {
125 return false;
126 }
127 Node knode = jnode.jjtGetChild(0);
128
129 return isMatch(knode);
130 }
131 }