-
Notifications
You must be signed in to change notification settings - Fork 353
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
New InstanceOfPatternMatch
recipe
#2690
New InstanceOfPatternMatch
recipe
#2690
Conversation
This is still a draft. There are still a few TODOs and the code is probably not very OpenRewrite-idiomatic, as I probably missed out on some helper utilities or misunderstood how to use all available constructs. A critical review would be most appreciated! |
ca849eb
to
6627fd9
Compare
rewrite-java/src/main/java/org/openrewrite/java/InstanceOfPatternMatch.java
Outdated
Show resolved
Hide resolved
6627fd9
to
73f101d
Compare
bafd93a
to
595be0a
Compare
An interesting enhancement could be to inline the expression, if it isn't referenced anywhere else. E.g. Object o = foo.bar();
if (o instanceof String && ((String) o).isEmpty()) {
// ...
} would become: if (foo.bar() instanceof String o && o.isEmpty()) {
// ...
} if Note that since Java isn't a lazily evaluated language, this "optimization" is only possible when the variable declaration immediately precedes the |
d892aea
to
7484889
Compare
@sambsnyd I consider this PR to be ready to be reviewed now. I still think that some things may be a bit non-idiomatic and would be glad for some tips on how to improve the code. (I am for instance wondering if it would be more idiomatic to schedule a separate after-recipe for each occurrence, rather than try to collect them all into a single input for the visitor or possibly it is even considered better practice to directly "inline" the visitor into the respective To be on the safe side, I currently exclude any expressions, which may have side effects (as determined by |
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.
Nice work. The recipe is complex, but the operation being performed is complex, so I don't see an obvious way to simplify things.
rewrite-java-test/src/test/java/org/openrewrite/java/InstanceOfPatternMatchTest.java
Outdated
Show resolved
Hide resolved
rewrite-java-test/src/test/java/org/openrewrite/java/InstanceOfPatternMatchTest.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/InstanceOfPatternMatch.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/InstanceOfPatternMatch.java
Show resolved
Hide resolved
7484889
to
e1d1f9d
Compare
Collecting them all into a single input for the visitor is definitely better for performance than separate after-recipe for each. Better to pay the cost of traversing the LST once rather than
Those sound like good improvements |
I did figure out a way to slightly simplify things. Since I already used the cursor for the containing statement to "communicate" between the I was looking for a mechanism to instruct a cursor to perform some post-processing action, once the visitor leaves that scope. As it turns out the |
56d6649
to
4c37972
Compare
@sambsnyd Thanks for your review feedback! I did find and fix one more bug and added a few more tests. There are probably more corner cases which should be tested, but I can't think of them right now. |
4c37972
to
92b9dde
Compare
rewrite-java/src/main/java/org/openrewrite/java/InstanceOfPatternMatch.java
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/InstanceOfPatternMatch.java
Show resolved
Hide resolved
Implement a recipe which for Java 17+ transforms `instanceof` expressions which have a corresponding type cast within their flow scope (see https://openjdk.org/jeps/394 for details) to an `instanceof` with a pattern variable and then also substitutes the type cast expressions accordingly. Currently, this works inside any binary boolean expressions and the then-block of `if` statements and ternary operator expressions.
92b9dde
to
01b2070
Compare
Over on this issue the question came up if Java 9+ recipes should go into rewrite-migrate-java rather than openrewrite/rewrite. Intuitively I'd think any "new"-ish Java features would best fit into What are your thoughts on that @sambsnyd ? |
@timtebeek I see rewrite-migrate-java as being for recipes about migrating to new versions of java. I don't think we need to put all recipes applicable only to newer versions of java there. This feels like a cleanup recipe more than a migration recipe, so putting it into rewrite-java feels reasonable to me |
@sambsnyd Should the recipe be moved to the |
* Add `InstanceOfPatternMatch` recipe to `java-version-17.yml` Related: openrewrite/rewrite#2690 * Move InstanceOfPatternMatch under UpgradeToJava17 * Add test for `InstanceOfPatternMatch` recipe --------- Co-authored-by: Tim te Beek <tim@moderne.io>
Implement a recipe which for Java 17+ transforms
instanceof
expressions which have a corresponding type cast within their flow scope (see https://openjdk.org/jeps/394 for details) to aninstanceof
with a pattern variable and then also substitutes the type cast expressions accordingly.