Skip to content

Commit

Permalink
Convert additional lambdas to expressions based on number of args (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Aug 16, 2024
1 parent 12b323a commit 3baf39a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private static boolean hasMethodOverloading(J.MethodInvocation method) {
// TODO this is actually more complex in the presence of generics and inheritance
static boolean hasMethodOverloading(JavaType.Method methodType) {
String methodName = methodType.getName();
int numberOfParameters = methodType.getParameterNames().size();
return Optional.of(methodType)
.map(JavaType.Method::getDeclaringType)
.filter(JavaType.Class.class::isInstance)
Expand All @@ -97,7 +98,8 @@ static boolean hasMethodOverloading(JavaType.Method methodType) {
.map(methods -> {
int overloadingCount = 0;
for (JavaType.Method dm : methods) {
if (dm.getName().equals(methodName)) {
if (methodName.equals(dm.getName()) &&
numberOfParameters == dm.getParameterNames().size()) {
if (++overloadingCount > 1) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Test {
"""
import java.util.function.Function;
class Test {
Function<Integer, Integer> f = n ->
Function<Integer, Integer> f = n ->
// The buttonType will always be "cancel", even if we pressed one of the entry type buttons
n + 1;
}
Expand Down Expand Up @@ -145,4 +145,41 @@ public void run() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/pull/582")
void simplifyAssertThrows() {
rewriteRun(
spec-> spec.parser(JavaParser.fromJavaVersion().classpath("junit")),
//language=java
java(
"""
import static org.junit.jupiter.api.Assertions.assertThrows;
class Test {
void test() {
assertThrows(IllegalArgumentException.class, () -> {
foo();
});
}
void foo() {
throw new IllegalArgumentException("boom");
}
}
""",
"""
import static org.junit.jupiter.api.Assertions.assertThrows;
class Test {
void test() {
assertThrows(IllegalArgumentException.class, () ->
foo());
}
void foo() {
throw new IllegalArgumentException("boom");
}
}
"""
)
);
}
}

0 comments on commit 3baf39a

Please sign in to comment.