Skip to content

Commit 64651c2

Browse files
committed
RemoveTryCatchFailBlocks removes imports if possible
Fixes: #421
1 parent 47ccd37 commit 64651c2

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525
import org.openrewrite.java.JavaVisitor;
2626
import org.openrewrite.java.MethodMatcher;
2727
import org.openrewrite.java.search.UsesMethod;
28-
import org.openrewrite.java.tree.Expression;
29-
import org.openrewrite.java.tree.J;
30-
import org.openrewrite.java.tree.Statement;
31-
import org.openrewrite.java.tree.TypeUtils;
28+
import org.openrewrite.java.tree.*;
3229

3330
import java.util.Collections;
31+
import java.util.Objects;
3432
import java.util.Set;
33+
import java.util.stream.Stream;
3534

3635
public class RemoveTryCatchFailBlocks extends Recipe {
3736
private static final MethodMatcher ASSERT_FAIL_NO_ARG = new MethodMatcher("org.junit.jupiter.api.Assertions fail()");
@@ -120,6 +119,7 @@ private static boolean isException(Expression expression) {
120119
@NotNull
121120
private J.MethodInvocation replaceWithAssertDoesNotThrowWithoutStringExpression(ExecutionContext ctx, J.Try try_) {
122121
maybeAddImport("org.junit.jupiter.api.Assertions");
122+
maybeRemoveCatchTypes(try_);
123123
return JavaTemplate.builder("Assertions.assertDoesNotThrow(() -> #{any()})")
124124
.contextSensitive()
125125
.imports("org.junit.jupiter.api.Assertions")
@@ -128,10 +128,22 @@ private J.MethodInvocation replaceWithAssertDoesNotThrowWithoutStringExpression(
128128
.apply(getCursor(), try_.getCoordinates().replace(), try_.getBody());
129129
}
130130

131+
private void maybeRemoveCatchTypes(J.Try try_) {
132+
JavaType catchType = try_.getCatches().get(0).getParameter().getTree().getType();
133+
if (catchType != null) {
134+
Stream.of(catchType)
135+
.flatMap(t -> t instanceof JavaType.MultiCatch ? ((JavaType.MultiCatch) t).getThrowableTypes().stream() : Stream.of(t))
136+
.map(TypeUtils::asFullyQualified)
137+
.filter(Objects::nonNull)
138+
.forEach(t -> maybeRemoveImport(t.getFullyQualifiedName()));
139+
}
140+
}
141+
131142
@NotNull
132143
private J.MethodInvocation replaceWithAssertDoesNotThrowWithStringExpression(ExecutionContext ctx, J.Try try_, Expression failCallArgument) {
133144
// Retain the fail(String) call argument
134145
maybeAddImport("org.junit.jupiter.api.Assertions");
146+
maybeRemoveCatchTypes(try_);
135147
return JavaTemplate.builder("Assertions.assertDoesNotThrow(() -> #{any()}, #{any(String)})")
136148
.contextSensitive()
137149
.imports("org.junit.jupiter.api.Assertions")

src/test/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocksTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ public String anotherMethod() {
535535
);
536536
}
537537

538+
@SuppressWarnings("resource")
538539
@Test
539540
void failHasBinaryWithGetMessage() {
540541
//language=java
@@ -543,13 +544,15 @@ void failHasBinaryWithGetMessage() {
543544
"""
544545
import org.junit.jupiter.api.Assertions;
545546
import org.junit.jupiter.api.Test;
547+
import java.io.FileOutputStream;
548+
import java.io.IOException;
546549
547550
class MyTest {
548551
@Test
549552
public void testMethod() {
550553
try {
551-
int divide = 50 / 0;
552-
} catch (Exception e) {
554+
FileOutputStream outputStream = new FileOutputStream("test.txt");
555+
} catch (IOException | RuntimeException e) {
553556
Assertions.fail("The error is: " + e.getMessage());
554557
}
555558
}
@@ -558,12 +561,13 @@ public void testMethod() {
558561
"""
559562
import org.junit.jupiter.api.Assertions;
560563
import org.junit.jupiter.api.Test;
564+
import java.io.FileOutputStream;
561565
562566
class MyTest {
563567
@Test
564568
public void testMethod() {
565569
Assertions.assertDoesNotThrow(() -> {
566-
int divide = 50 / 0;
570+
FileOutputStream outputStream = new FileOutputStream("test.txt");
567571
}, "The error is: ");
568572
}
569573
}

0 commit comments

Comments
 (0)