|
40 | 40 | public class IfElseIfConstructToSwitch extends Recipe { |
41 | 41 | @Override |
42 | 42 | public String getDisplayName() { |
43 | | - return "Use pattern matching in switch cases"; |
| 43 | + return "If-elseIf-else construct to switch"; |
44 | 44 | } |
45 | 45 |
|
46 | 46 | @Override |
47 | 47 | public String getDescription() { |
48 | | - return "Enhance the Java programming language with pattern matching for switch expressions and statements. " + |
49 | | - "Extending pattern matching to switch allows an expression to be tested against a number of patterns, each with a specific action, so that complex data-oriented queries can be expressed concisely and safely."; |
| 48 | + return "Replace if-elseIf-else construct with switch statements. In order to be replaced with a switch, " + |
| 49 | + "all conditions must be on the same variable and there must be at least 3 cases."; |
50 | 50 | } |
51 | 51 |
|
52 | 52 | @Override |
@@ -100,50 +100,43 @@ private static class SwitchCandidate { |
100 | 100 | } |
101 | 101 | } |
102 | 102 |
|
103 | | - J.@Nullable If handleNullCheck(J.If ifPart, Cursor cursor) { |
104 | | - if (ifPart.getIfCondition().getTree() instanceof J.Binary) { |
105 | | - Optional<NullCheck> nullCheck = nullCheck().get(ifPart, cursor); |
106 | | - if (nullCheck.isPresent()) { |
107 | | - nullCheckedParameter = nullCheck.get().getNullCheckedParameter(); |
108 | | - nullCheckedStatement = nullCheck.get().whenNull(); |
109 | | - Statement elsePart = nullCheck.get().whenNotNull(); |
110 | | - if (elsePart instanceof J.If) { |
111 | | - ifPart = (J.If) elsePart; |
112 | | - } else { |
113 | | - elze = elsePart; |
114 | | - ifPart = null; |
115 | | - } |
| 103 | + private J.@Nullable If handleNullCheck(J.If ifPart, Cursor cursor) { |
| 104 | + Optional<NullCheck> nullCheck = nullCheck().get(ifPart, cursor); |
| 105 | + if (nullCheck.isPresent()) { |
| 106 | + nullCheckedParameter = nullCheck.get().getNullCheckedParameter(); |
| 107 | + nullCheckedStatement = nullCheck.get().whenNull(); |
| 108 | + Statement elsePart = nullCheck.get().whenNotNull(); |
| 109 | + if (elsePart instanceof J.If) { |
| 110 | + ifPart = (J.If) elsePart; |
116 | 111 | } else { |
117 | | - noPotentialCandidate(); |
| 112 | + elze = elsePart; |
| 113 | + ifPart = null; |
118 | 114 | } |
119 | | - return ifPart; |
| 115 | + } else { |
| 116 | + noPotentialCandidate(); |
120 | 117 | } |
121 | | - throw new IllegalArgumentException("Unsupported if type: " + ifPart.getIfCondition().getTree().getClass().getSimpleName()); |
| 118 | + return ifPart; |
122 | 119 | } |
123 | 120 |
|
124 | | - J.@Nullable If handleInstanceOfCheck(J.If ifPart) { |
125 | | - if (ifPart.getIfCondition().getTree() instanceof J.InstanceOf) { |
126 | | - patternMatchers.put((J.InstanceOf) ifPart.getIfCondition().getTree(), ifPart.getThenPart()); |
127 | | - J.If.Else elsePart = ifPart.getElsePart(); |
128 | | - if (elsePart != null && elsePart.getBody() instanceof J.If) { |
129 | | - ifPart = (J.If) elsePart.getBody(); |
130 | | - } else { |
131 | | - elze = elsePart != null ? elsePart.getBody() : null; |
132 | | - ifPart = null; |
133 | | - } |
134 | | - return ifPart; |
| 121 | + private J.@Nullable If handleInstanceOfCheck(J.If ifPart) { |
| 122 | + patternMatchers.put((J.InstanceOf) ifPart.getIfCondition().getTree(), ifPart.getThenPart()); |
| 123 | + J.If.Else elsePart = ifPart.getElsePart(); |
| 124 | + if (elsePart != null && elsePart.getBody() instanceof J.If) { |
| 125 | + ifPart = (J.If) elsePart.getBody(); |
| 126 | + } else { |
| 127 | + elze = elsePart != null ? elsePart.getBody() : null; |
| 128 | + ifPart = null; |
135 | 129 | } |
136 | | - throw new IllegalArgumentException("Unsupported if type: " + ifPart.getIfCondition().getTree().getClass().getSimpleName()); |
| 130 | + return ifPart; |
137 | 131 | } |
138 | 132 |
|
139 | 133 | void noPotentialCandidate() { |
140 | 134 | this.potentialCandidate = false; |
141 | 135 | } |
142 | 136 |
|
143 | 137 | boolean isValidCandidate() { |
144 | | - // all ifs in the chain must be on the same variable |
| 138 | + // all ifs in the chain must be on the same variable in order to be a candidate for switch pattern matching |
145 | 139 | if (potentialCandidate && patternMatchers.keySet().stream().map(J.InstanceOf::getExpression).map(expression -> ((J.Identifier) expression).getSimpleName()).distinct().count() != 1) { |
146 | | - // pattern matching in a switch can only happen if all if cases are on the same variable. |
147 | 140 | this.potentialCandidate = false; |
148 | 141 | return false; |
149 | 142 | } |
|
0 commit comments