Skip to content

Commit

Permalink
Fix CatchCauseOnlyRethrows onlyRethrows check for multi catch state…
Browse files Browse the repository at this point in the history
…ments (#355)

* Fix CatchCauseOnlyRethrows rethrows check for multi catch statements

* Specifically check for `JavaType.MultiCatch`

---------

Co-authored-by: Niels de Bruin <niels.de.bruin@jdriven.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent 07686d2 commit d24228b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.*;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;

import java.time.Duration;
import java.util.Set;
Expand All @@ -37,7 +40,7 @@ public String getDisplayName() {
@Override
public String getDescription() {
return "A `catch` clause that only rethrows the caught exception is unnecessary. " +
"Letting the exception bubble up as normal achieves the same result with less code.";
"Letting the exception bubble up as normal achieves the same result with less code.";
}

@Override
Expand Down Expand Up @@ -105,16 +108,18 @@ private boolean hasWiderExceptionType(J.Try.Catch aCatch, J.Try.Catch next) {

private boolean onlyRethrows(J.Try.Catch aCatch) {
if (aCatch.getBody().getStatements().size() != 1 ||
!(aCatch.getBody().getStatements().get(0) instanceof J.Throw)) {
!(aCatch.getBody().getStatements().get(0) instanceof J.Throw)) {
return false;
}

Expression exception = ((J.Throw) aCatch.getBody().getStatements().get(0)).getException();
JavaType.FullyQualified catchType = TypeUtils.asFullyQualified(aCatch.getParameter().getType());
if (catchType == null || !catchType.equals(exception.getType())) {
return false;
JavaType catchParameterType = aCatch.getParameter().getType();
if (!(catchParameterType instanceof JavaType.MultiCatch)) {
JavaType.FullyQualified catchType = TypeUtils.asFullyQualified(catchParameterType);
if (catchType == null || !catchType.equals(exception.getType())) {
return false;
}
}

if (exception instanceof J.Identifier) {
return ((J.Identifier) exception).getSimpleName().equals(aCatch.getParameter().getTree().getVariables().get(0).getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,71 @@ void foo() throws IOException {
);
}

@Test
void tryCanBeRemovedWithMultiCatch() {
rewriteRun(
//language=java
java(
"""
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
class A {
void foo() throws IOException {
try {
new FileReader("").read();
} catch (FileNotFoundException e) {
throw e;
} catch(IOException | ArrayIndexOutOfBoundsException e) {
throw e;
} catch(Exception e) {
throw e;
}
}
}
""",
"""
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
class A {
void foo() throws IOException {
new FileReader("").read();
}
}
"""
)
);
}

@Test
void multiCatchPreservedOnDifferentThrow() {
rewriteRun(
//language=java
java(
"""
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
class A {
void foo() throws IOException {
try {
new FileReader("").read();
} catch (FileNotFoundException e) {
throw e;
} catch(IOException | ArrayIndexOutOfBoundsException e) {
throw new IOException("another message", e);
}
}
}
"""
)
);
}

@Test
void tryShouldBePreservedBecauseFinally() {
rewriteRun(
Expand Down

0 comments on commit d24228b

Please sign in to comment.