Skip to content

Conversation

ranuradh
Copy link
Contributor

@ranuradh ranuradh commented Mar 21, 2024

What's changed?

Recipe :org.openrewrite.java.migrate.DeprecatedCountStackFramesMethod
image

Any additional context

Thread.countStackFrames()has been removed in Java SE 14 and has been changed in this release to unconditionally throwUnsupportedOperationException`. This recipe removes the usage of this method in your application as long as the method is not assigned to a variable.

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

@ranuradh ranuradh requested a review from cjobinabo March 21, 2024 20:13
@ranuradh ranuradh self-assigned this Mar 22, 2024
@ranuradh ranuradh requested a review from timtebeek March 24, 2024 21:50
@timtebeek
Copy link
Member

Hi @ranuradh ; I'm still out for another couple weeks. Probably good to tag Knut for review once @cjobinabo has gone over the changes.

@Override
public String getDescription() {
return "`Thread.countStackFrames()` has been removed in Java SE 14." +
"It is now replaced by \"Integer.valueOf(\\\"0\\\")\" to return 0." +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably meant for \\\"0\\\" to be \"0\". But the description it's is actually misleading. Thread.countStackFrames() was not replaced with Integer.valueOf("0"). Integer.valueOf("0") is just a placeholder that allows us to safely remove Thread.countStackFrames(). As I mentioned earlier when we spoke last week, we will need to update this recipe to have a comment before or after the updated code to let the user know they should review and remove the Integer.valueOf("0") placeholder. @knutwannheden, do you have any examples you could point Anu to on how this comment can be provided, or perhaps a better idea of how Thread.countStackFrames() could be safely removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how the extra \ got in the description, removed that in the next commit. Based on our conversation I assumed that for now we will be using this format for the recipe, hence added the line "The updated line is dead code and should be eventually removed." I can wait for what @knutwannheden has to say and can update the recipe accordingly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@ranuradh ranuradh Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjobinabo the recipe only removes the MethodCall where the call's return value is unused. I tried this to remove a constructor. openrewrite/rewrite-static-analysis#273. Hence

import java.lang.Thread;
               
                 public class Test {
               	        public static void main(String args[]) {
               		        Thread t1 = new Thread();
               		        Thread t2 = new Thread();
               		        t1.countStackFrames();
               		        t2.countStackFrames();
               	  }              
              }   

will work but the following wont work:

public class Test {
               	        public static void main(String args[]) {
               		        Thread t1 = new Thread();
               		        Thread t2 = new Thread();
               		        int x = t1.countStackFrames();
               		        int y = t2.countStackFrames();
               	  }        

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjobinabo as discussed I updated the recipe to now use RemoveMethodInvocation

@cjobinabo cjobinabo removed the request for review from timtebeek March 25, 2024 14:00
@ranuradh ranuradh requested review from knutwannheden and removed request for knutwannheden March 25, 2024 14:13
@ranuradh ranuradh added the recipe Recipe requested label Mar 26, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java
    • lines 412-412

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java
    • lines 412-412

@ranuradh
Copy link
Contributor Author

Hi @timtebeek this recipe is ready for merge

@ranuradh ranuradh requested a review from timtebeek April 23, 2024 14:33
@timtebeek timtebeek changed the title Adding recipe forJSE 17 migrations - DeprecatedCountStackFramesMethod Recipe to removed Thread.countStackFrames(). Apr 23, 2024
@timtebeek timtebeek changed the title Recipe to removed Thread.countStackFrames(). Recipe to remove Thread.countStackFrames(). Apr 23, 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.

Nice and minimal; wondering if we should do anything for the cases where it is assigned to a variable, although I'd think those are rare.

@timtebeek timtebeek requested a review from cjobinabo April 23, 2024 19:24
@timtebeek
Copy link
Member

Somehow this now fails with

UpgradeToJava17Test > replaceLogRecordSetThreadID() FAILED
    java.lang.AssertionError: [Recipe validation must have no failures] 
    Expecting no elements of:
      [[Valid{property='initialization', value='org.openrewrite.config.DeclarativeRecipe@2197c885'},
        Invalid{property='org.openrewrite.java.migrate.UpgradeToJava17.recipeList[19] (in file:///home/runner/work/rewrite-migrate-java/rewrite-migrate-java/build/resources/main/META-INF/rewrite/java-version-17.yml)', value='org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods', message='recipe 'org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods' does not exist.'},
        Valid{property='initialization', value='org.openrewrite.config.DeclarativeRecipe@2197c885'},
        Invalid{property='org.openrewrite.java.migrate.UpgradeToJava17.recipeList[19] (in file:///home/runner/work/rewrite-migrate-java/rewrite-migrate-java/build/resources/main/META-INF/rewrite/java-version-17.yml)', value='org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods', message='recipe 'org.openrewrite.java.migrate.RemovedFileIOFinalizeMethods' does not exist.'},

@timtebeek timtebeek merged commit e340d96 into openrewrite:main Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

recipe Recipe requested

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants