Skip to content

Commit

Permalink
Combine semantically equal catch blocks with same var declarations (#280
Browse files Browse the repository at this point in the history
)

* Add test for CombineSemanticallyEqualCatchBlocks having equal catch
blocks with variable declaration

* Do not try to compare nullable varargs in catch blocks for
CombineSemanticallyEqualCatchBlocks recipe. Fixes NPE.

* Revert "Do not try to compare nullable varargs in catch blocks for"

This reverts commit a248d1d.

* Fix NPE in CombineSemanticallyEqualCatchBlocks recipe when trying to determine if catch blocks do not contain same comments

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
Marklinzi and timtebeek authored Apr 19, 2024
1 parent d8a0f02 commit 11a7706
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ private boolean nullMissMatch(Object obj1, Object obj2) {
}

private boolean doesNotContainSameComments(Space space1, Space space2) {
if (space1.getComments().size() != space2.getComments().size()) {
if (space1 == null && space2 == null) {
return false;
}

if (space1 == null || space2 == null || space1.getComments().size() != space2.getComments().size()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,40 @@ void log(String msg) {}
)
);
}


@Test
void combineSameCatchBlocksWithVariableDeclaration() {
rewriteRun(
//language=java
java("class A extends RuntimeException {}"),
//language=java
java("class B extends RuntimeException {}"),
//language=java
java(
"""
class Test {
void method() {
try {
} catch (A ex) {
String s = "foo";
} catch (B ex) {
String s = "foo";
}
}
}
""",
"""
class Test {
void method() {
try {
} catch (A | B ex) {
String s = "foo";
}
}
}
"""
)
);
}
}

0 comments on commit 11a7706

Please sign in to comment.