-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(LambdaBlockToExpression): convert lambda with method invocation in assertThrows as well #245
Draft
timo-abele
wants to merge
14
commits into
openrewrite:main
Choose a base branch
from
timo-abele:fix/convert-lambda-with-method-invocation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−32
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e02d52a
add failing test
timo-abele 2badf5a
add necessary dependencies
timo-abele e74389c
Use `recipeDependencies` for reproduction test
timtebeek 1ac607f
Only count overload if it has the same number of arguments
timtebeek 13da91d
put private methods in order of appearance
timo-abele 172f644
sketch out solution
timo-abele 758e7cf
fix build & tests
timo-abele 35f6d43
simplify interaction with RemoveRedundantTypeCast by using interface
timo-abele 31e0979
resolve potential NPE
timo-abele 6a35596
add warning
timo-abele a28c7fd
Merge branch 'openrewrite:main' into fix/convert-lambda-with-method-i…
timo-abele 3122513
Merge branch 'main' into fix/convert-lambda-with-method-invocation
timtebeek 81aab6f
Merge branch 'main' into fix/convert-lambda-with-method-invocation
timtebeek f00d7ea
Merge branch 'main' into fix/convert-lambda-with-method-invocation
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+206 KB
src/main/resources/META-INF/rewrite/classpath/junit-jupiter-api-5.10.1.jar
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.InMemoryExecutionContext; | ||
import org.openrewrite.Issue; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
|
@@ -25,11 +26,12 @@ | |
import static org.openrewrite.java.Assertions.java; | ||
|
||
class LambdaBlockToExpressionTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new LambdaBlockToExpression()) | ||
.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)); | ||
.parser(JavaParser.fromJavaVersion() | ||
.classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.10") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this dependency is only used for tests, we should make sure to not add it to |
||
.logCompilationWarningsAndErrors(true)); | ||
} | ||
|
||
@DocumentExample | ||
|
@@ -75,7 +77,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; | ||
} | ||
|
@@ -116,7 +118,7 @@ void doTest() { | |
|
||
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/236") | ||
@Test | ||
void simplifyLambdaBlockReturningVoidAsWell2() { | ||
void simplifyLambdaBlockReturningVoidAsWellSimple() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
|
@@ -145,4 +147,42 @@ public void run() { | |
); | ||
} | ||
|
||
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/236") | ||
@Test | ||
void simplifyLambdaBlockReturningVoidAsWellAssertThrows() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.math.BigDecimal; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class Main { | ||
@Test | ||
public void test() { | ||
assertThrows(ArithmeticException.class, () -> { | ||
BigDecimal.ONE.divide(BigDecimal.ZERO); | ||
}); | ||
} | ||
} | ||
""", | ||
""" | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.math.BigDecimal; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class Main { | ||
@Test | ||
public void test() { | ||
assertThrows(ArithmeticException.class, () -> | ||
BigDecimal.ONE.divide(BigDecimal.ZERO)); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than collecting all methods into a list, it would be more efficient to loop over them.
I am also saying this, because we should actually also be checking methods in super types and even default methods on implemented interfaces. But this can quickly get difficult, as I am not convinced that we will be reliable able to tell if a method is an override of some other method (and thus not an overload) when generics are involved. But once we cross that road, a recursive method which returns as soon as it finds a conflicting overload will be more efficient than first gathering all methods up front.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't find time to finish this and have unassigned me. I trust your judgement that not collecting makes a enough of a difference to justify the loss in readability. I found the previous version very hard to get into (at first I thought it
map
s over the list elements, but it is always over theOptional
):rewrite-static-analysis/src/main/java/org/openrewrite/staticanalysis/LambdaBlockToExpression.java
Lines 80 to 97 in e57dac0
MethodsOfType
can stay:rewrite-static-analysis/src/main/java/org/openrewrite/staticanalysis/LambdaBlockToExpression.java
Lines 113 to 118 in 758e7cf
getMethods
) andCollections.emptyList()
is a constant, I think it makes no difference in performance while neatly separating the optional stream from the loop over the elements.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right, there is no additional list being created here. I was already thinking ahead for the use case with inherited method declarations. But let's forget about that for the moment then (a bug will probably get reported at some point).