|
32 | 32 |
|
33 | 33 | import java.util.Comparator; |
34 | 34 | import java.util.List; |
35 | | -import java.util.Objects; |
36 | 35 | import java.util.Set; |
37 | 36 |
|
38 | | -import static java.util.Collections.emptyList; |
39 | | - |
40 | 37 | public class UpdateTestAnnotation extends Recipe { |
41 | 38 |
|
42 | 39 | @Getter |
@@ -185,34 +182,15 @@ private J.MethodDeclaration removeSpecificThrowsClause(J.MethodDeclaration metho |
185 | 182 | return method; |
186 | 183 | } |
187 | 184 |
|
188 | | - // Extract the exception type from the expected exception expression |
189 | 185 | // This should always be a `J.FieldAccess` of the form MyException.class |
190 | | - JavaType expectedType = null; |
191 | | - if (expectedException instanceof J.FieldAccess) { |
192 | | - J.FieldAccess fa = (J.FieldAccess) expectedException; |
193 | | - expectedType = fa.getTarget().getType(); |
| 186 | + if (!(expectedException instanceof J.FieldAccess)) { |
| 187 | + return method; |
194 | 188 | } |
195 | | - |
| 189 | + JavaType expectedType = ((J.FieldAccess) expectedException).getTarget().getType(); |
196 | 190 | if (expectedType == null) { |
197 | 191 | return method; |
198 | 192 | } |
199 | | - |
200 | | - // Filter out only the exception that matches the expected exception |
201 | | - final JavaType finalExpectedType = expectedType; |
202 | | - List<NameTree> filteredThrows = ListUtils.map(thrown, throwClause -> { |
203 | | - JavaType throwType = throwClause.getType(); |
204 | | - if (throwType != null && TypeUtils.isAssignableTo(finalExpectedType, throwType)) { |
205 | | - return null; |
206 | | - } |
207 | | - return throwClause; |
208 | | - }); |
209 | | - |
210 | | - // If all throws were removed, return empty list, otherwise keep the remaining ones |
211 | | - if (filteredThrows.isEmpty() || filteredThrows.stream().allMatch(Objects::isNull)) { |
212 | | - return method.withThrows(emptyList()); |
213 | | - } |
214 | | - |
215 | | - return method.withThrows(filteredThrows); |
| 193 | + return method.withThrows(ListUtils.filter(thrown, throwClause -> throwClause.getType() == null || !TypeUtils.isAssignableTo(expectedType, throwClause.getType()))); |
216 | 194 | } |
217 | 195 |
|
218 | 196 | private static class ChangeTestAnnotation extends JavaIsoVisitor<ExecutionContext> { |
|
0 commit comments