Skip to content

Conversation

@yurii-yu
Copy link
Contributor

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

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@timtebeek timtebeek self-requested a review October 25, 2024 11:40
@timtebeek
Copy link
Member

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.

@timtebeek timtebeek added enhancement New feature or request recipe and removed enhancement New feature or request labels Oct 25, 2024
yurii-yu and others added 8 commits October 25, 2024 13:49
…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>
@yurii-yu
Copy link
Contributor Author

@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.

timtebeek and others added 2 commits October 25, 2024 17:59
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek changed the title add new recipes for SonarQube Issues RSPEC-1170 and RSPEC-6202 Add new recipes for SonarQube Issues RSPEC-1170 and RSPEC-6202 Oct 25, 2024
@timtebeek
Copy link
Member

@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.

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.

@timtebeek timtebeek self-requested a review October 25, 2024 16:01
Comment on lines 79 to 89
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()));

}
Copy link
Member

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.

@timtebeek
Copy link
Member

It might make sense to take out AddStaticModifierToPublicFinalConstantsAndFields and add that in a separate PR, as for now that perhaps has a design flaw around mutable fields we'd need to fix first, which would otherwise hold up ReplaceClassIsInstanceWithInstanceof unnecessarily.

@yurii-yu
Copy link
Contributor Author

It might make sense to take out AddStaticModifierToPublicFinalConstantsAndFields and add that in a separate PR, as for now that perhaps has a design flaw around mutable fields we'd need to fix first, which would otherwise hold up ReplaceClassIsInstanceWithInstanceof unnecessarily.

Well, I agree with you. It is quite normal that we find things are more complicated than we thought before.

@yurii-yu
Copy link
Contributor Author

@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.

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.

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.
Sorry I am quite busy this weekend because my son has Eishocky Turnier. I will come back to you in details by the end of Monday.

@yurii-yu
Copy link
Contributor Author

@timtebeek I removed the Recipe AddStaticModifierToPublicFinalConstantsAndFields from this pull request and I have no more questions about the code. Please have a look at the latest code
Thank you for your great help, I always learn something new from you.

@timtebeek timtebeek changed the title Add new recipes for SonarQube Issues RSPEC-1170 and RSPEC-6202 Add recipe ReplaceClassIsInstanceWithInstanceof for SonarQube RSPEC-6202 Oct 28, 2024
Copy link
Member

@timtebeek timtebeek left a 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. :)

@timtebeek timtebeek merged commit 286d4ee into openrewrite:main Oct 28, 2024
@timtebeek
Copy link
Member

Came across these cases we don't yet handle correctly in the wider OSS commnity by running through Moderne:
https://app.moderne.io/recipes/org.openrewrite.staticanalysis.ReplaceClassIsInstanceWithInstanceof

Needs wrapping parentheses

diff --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

image

java.lang.ClassCastException: class org.openrewrite.java.tree.J$MethodInvocation cannot be cast to class org.openrewrite.java.tree.J$Identifier (org.openrewrite.java.tree.J$MethodInvocation and org.openrewrite.java.tree.J$Identifier are in unnamed module of loader 'app')
org.openrewrite.staticanalysis.ReplaceClassIsInstanceWithInstanceof$1.visitMethodInvocation(ReplaceClassIsInstanceWithInstanceof.java:63)
org.openrewrite.staticanalysis.ReplaceClassIsInstanceWithInstanceof$1.visitMethodInvocation(ReplaceClassIsInstanceWithInstanceof.java:54)
org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3905)
org.openrewrite.java.tree.J.accept(J.java:59)
org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1367)
org.openrewrite.java.JavaVisitor.visitControlParentheses(JavaVisitor.java:524)
org.openrewrite.java.tree.J$ControlParentheses.acceptJava(J.java:4828)
org.openrewrite.java.tree.J.accept(J.java:59)
org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
org.openrewrite.java.JavaVisitor.visitIf(JavaVisitor.java:714)
org.openrewrite.java.tree.J$If.acceptJava(J.java:2528)
org.openrewrite.java.tree.J.accept(J.java:59)
org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants