-
Notifications
You must be signed in to change notification settings - Fork 93
Add recipe ReplaceClassIsInstanceWithInstanceof for SonarQube RSPEC-6202
#381
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
Add recipe ReplaceClassIsInstanceWithInstanceof for SonarQube RSPEC-6202
#381
Conversation
...ava/org/openrewrite/staticanalysis/AddStaticModifierToPublicFinalConstantsAndFieldsTest.java
Outdated
Show resolved
Hide resolved
|
Much appreciated @yurii-yu ! Let's see if our bot has some automated suggestions already, before I dive in later today or over the weekend. |
...in/java/org/openrewrite/staticanalysis/AddStaticModifierToPublicFinalConstantsAndFields.java
Outdated
Show resolved
Hide resolved
...in/java/org/openrewrite/staticanalysis/AddStaticModifierToPublicFinalConstantsAndFields.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceofTest.java
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceofTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceofTest.java
Outdated
Show resolved
Hide resolved
…tanceWithInstanceofTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…tanceWithInstanceofTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…tanceWithInstanceofTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…tanceWithInstanceof.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…tanceWithInstanceof.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…tanceWithInstanceof.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ToPublicFinalConstantsAndFields.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ToPublicFinalConstantsAndFields.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
@timtebeek thank you for the insights. I overlooked that point before. I guess I can fix that and add the corresponding test on the weekend. |
src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Thanks @yurii-yu ; I've already pushed up a few quick polishing commits with the changes I was expecting; let me know if there's anything you'd still like to add and otherwise we do a final review and merge early next week. |
| if (multiVariable.hasModifier(Modifier.Type.Public) && | ||
| multiVariable.hasModifier(Modifier.Type.Final) && | ||
| !multiVariable.hasModifier(Modifier.Type.Static)) { | ||
|
|
||
| multiVariable.getModifiers().add(new J.Modifier(Tree.randomId(), | ||
| Space.format(" "), Markers.EMPTY, " ", | ||
| Modifier.Type.Static, emptyList())); | ||
|
|
||
| return v.withModifiers(ModifierOrder.sortModifiers(multiVariable.getModifiers())); | ||
|
|
||
| } |
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.
This might be too simplistic. As seen on for instance this class, making the instance fields static could drastically change behavior if there's more than one instance, if combined with mutable fields. This operation should only be safe for immutable types like primitives.
|
It might make sense to take out |
Well, I agree with you. It is quite normal that we find things are more complicated than we thought before. |
Thank you for your great help. I need to admit that sometimes it's very frustrating to know "how to do it in the OpenRewrite" way, and I really appreciate your help. I always learn a lot from you. |
…nsideration, so we will do it in another PR
|
@timtebeek I removed the Recipe |
ReplaceClassIsInstanceWithInstanceof for SonarQube RSPEC-6202
timtebeek
left a comment
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.
Thanks a lot @yurii-yu ! Great to see this come together. I've added a last polishing commit and with that we're good to merge. :)
|
Came across these cases we don't yet handle correctly in the wider OSS commnity by running through Moderne: Needs wrapping parenthesesdiff --git a/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java b/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
index e9d06af..78a264e 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
@@ -321,7 +321,7 @@ org.openrewrite.staticanalysis.ReplaceClassIsInstanceWithInstanceof
}
private void doClose(final CompiledScript compile) {
- if (!AutoCloseable.class.isInstance(compile)) {
+ if (!compile instanceof AutoCloseable) {
return;
}
try {Cause class cast exception |

What's changed?
Add two new recipes for SonarQube Issue RSPEC-1170 and RSPEC-6202
What's your motivation?
Benefited a lot from the OpenRewrite project, it is our responsibility to make some contribution.
Anything in particular you'd like reviewers to focus on?
This is the first time I submit recipes, I tried my best to follow the convention of existing recipes.
Anyone you would like to review specifically?
maybe @timtebeek
Have you considered any alternatives or workarounds?
I do not think there are better alternatives.
Any additional context
Checklist