Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.JavadocVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.*;

import java.util.Comparator;
Expand Down Expand Up @@ -121,17 +122,38 @@ private void removeThrownTypes(JavaType.@Nullable Method type) {
}
}
}
}.visit(m, ctx);
}.visit(m, ctx, getCursor().getParent());

if (!unusedThrows.isEmpty()) {
MethodMatcher originalMethodMatcher = new MethodMatcher(m);

JavaType.Method replacementMethodType = m.getMethodType().withThrownExceptions(ListUtils.map(m.getMethodType().getThrownExceptions(), t -> {
JavaType.FullyQualified type = TypeUtils.asFullyQualified(t);
return type != null && unusedThrows.contains(type) ? null : t;
}));
m = m.withThrows(ListUtils.map(m.getThrows(), t -> {
JavaType.FullyQualified type = TypeUtils.asFullyQualified(t.getType());
if (type != null && unusedThrows.contains(type)) {
maybeRemoveImport(type);
return null;
JavaType.FullyQualified type = TypeUtils.asFullyQualified(t.getType());
if (type != null && unusedThrows.contains(type)) {
maybeRemoveImport(type);
return null;
}
return t;
}))
.withMethodType(replacementMethodType)
.withName(m.getName().withType(replacementMethodType));

// Remove the thrown exceptions from the method type, such that UnnecessaryCatch can continue
doAfterVisit(new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation invocation, ExecutionContext ctx) {
if (originalMethodMatcher.matches(invocation)) {
invocation = invocation.withMethodType(replacementMethodType)
.withName(invocation.getName().withType(replacementMethodType));
}
return super.visitMethodInvocation(invocation, ctx);
}
return t;
}));
});
doAfterVisit(new UnnecessaryCatch(true, false).getVisitor());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I know this won't fix the case where the usage is in any other files, but I figured we can already improve the situation for known cases within the same file, with relatively little effort.

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,36 @@ protected void method() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/605")
@Test
void removeUnnecessaryCatchAfterRemovingThrows() {
rewriteRun(
java(
"""
class UnnecessaryThrowsTest {

void methodThrowing() throws NoSuchMethodException { }

void methodCatching() {
try {
methodThrowing();
}
catch (NoSuchMethodException e) { }
}
}
""",
"""
class UnnecessaryThrowsTest {

void methodThrowing() { }

void methodCatching() {
methodThrowing();
}
}
"""
)
);
}
}