Skip to content

Commit af53ce1

Browse files
authored
Run UnnecessaryCatch after UnnecessaryThrows to remove unused catch clauses (#785)
* Run `UnnecessaryCatch` after `UnnecessaryThrows` to remove unused catch clauses - Fixes #605 * Update test name and minimize elements
1 parent b670b5a commit af53ce1

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

src/main/java/org/openrewrite/staticanalysis/UnnecessaryThrows.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openrewrite.java.JavaIsoVisitor;
2525
import org.openrewrite.java.JavaVisitor;
2626
import org.openrewrite.java.JavadocVisitor;
27+
import org.openrewrite.java.MethodMatcher;
2728
import org.openrewrite.java.tree.*;
2829

2930
import java.util.Comparator;
@@ -121,17 +122,38 @@ private void removeThrownTypes(JavaType.@Nullable Method type) {
121122
}
122123
}
123124
}
124-
}.visit(m, ctx);
125+
}.visit(m, ctx, getCursor().getParent());
125126

126127
if (!unusedThrows.isEmpty()) {
128+
MethodMatcher originalMethodMatcher = new MethodMatcher(m);
129+
130+
JavaType.Method replacementMethodType = m.getMethodType().withThrownExceptions(ListUtils.map(m.getMethodType().getThrownExceptions(), t -> {
131+
JavaType.FullyQualified type = TypeUtils.asFullyQualified(t);
132+
return type != null && unusedThrows.contains(type) ? null : t;
133+
}));
127134
m = m.withThrows(ListUtils.map(m.getThrows(), t -> {
128-
JavaType.FullyQualified type = TypeUtils.asFullyQualified(t.getType());
129-
if (type != null && unusedThrows.contains(type)) {
130-
maybeRemoveImport(type);
131-
return null;
135+
JavaType.FullyQualified type = TypeUtils.asFullyQualified(t.getType());
136+
if (type != null && unusedThrows.contains(type)) {
137+
maybeRemoveImport(type);
138+
return null;
139+
}
140+
return t;
141+
}))
142+
.withMethodType(replacementMethodType)
143+
.withName(m.getName().withType(replacementMethodType));
144+
145+
// Remove the thrown exceptions from the method type, such that UnnecessaryCatch can continue
146+
doAfterVisit(new JavaIsoVisitor<ExecutionContext>() {
147+
@Override
148+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation invocation, ExecutionContext ctx) {
149+
if (originalMethodMatcher.matches(invocation)) {
150+
invocation = invocation.withMethodType(replacementMethodType)
151+
.withName(invocation.getName().withType(replacementMethodType));
152+
}
153+
return super.visitMethodInvocation(invocation, ctx);
132154
}
133-
return t;
134-
}));
155+
});
156+
doAfterVisit(new UnnecessaryCatch(true, false).getVisitor());
135157
}
136158
}
137159

src/test/java/org/openrewrite/staticanalysis/UnnecessaryThrowsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,36 @@ protected void method() {
457457
)
458458
);
459459
}
460+
461+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/605")
462+
@Test
463+
void removeUnnecessaryCatchAfterRemovingThrows() {
464+
rewriteRun(
465+
java(
466+
"""
467+
class UnnecessaryThrowsTest {
468+
469+
void methodThrowing() throws NoSuchMethodException { }
470+
471+
void methodCatching() {
472+
try {
473+
methodThrowing();
474+
}
475+
catch (NoSuchMethodException e) { }
476+
}
477+
}
478+
""",
479+
"""
480+
class UnnecessaryThrowsTest {
481+
482+
void methodThrowing() { }
483+
484+
void methodCatching() {
485+
methodThrowing();
486+
}
487+
}
488+
"""
489+
)
490+
);
491+
}
460492
}

0 commit comments

Comments
 (0)