openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.15k stars 320 forks source link

Support switch expressions in Java 21; currently fails with "is not print idempotent" #4223

Open fprochazka opened 4 months ago

fprochazka commented 4 months ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a multi-module project

pom.xml ```xml org.openrewrite.maven rewrite-maven-plugin ${maven-rewrite-plugin.version} com.shipmonk.tooling shipmonk-tooling-openrewrite ${project.version} ${shipmonk-framework.basedir}/src/openrewrite/rewrite.yml com.shipmonk.Cleanup org.openrewrite.java.logging.slf4j.ParameterizedLogging com.shipmonk.tooling.openrewrite.cleanup.ThrowsFormattingRecipe com.shipmonk.tooling.openrewrite.cleanup.MethodDeclarationParametersWrappingRecipe false true true **/.data **/.idea **/.cache **/.mvn **/docs **/docker **/*.iml **/*.json **/*.xml ```
rewrite.yml ```yml --- type: specs.openrewrite.org/v1beta/recipe name: com.shipmonk.Cleanup recipeList: - org.openrewrite.java.OrderImports: removeUnused: false - org.openrewrite.java.RemoveObjectsIsNull: { } - org.openrewrite.java.format.OperatorWrap: { } - org.openrewrite.java.format.TypecastParenPad: { } - org.openrewrite.java.format.BlankLines: { } - org.openrewrite.java.format.EmptyNewlineAtEndOfFile: { } - org.openrewrite.java.format.SingleLineComments: { } - org.openrewrite.java.logging.slf4j.LoggersNamedForEnclosingClass: { } - org.openrewrite.java.logging.slf4j.Slf4jLogShouldBeConstant: { } - org.openrewrite.staticanalysis.DefaultComesLast: { } - org.openrewrite.staticanalysis.ModifierOrder: { } - org.openrewrite.staticanalysis.StringLiteralEquality: { } - org.openrewrite.staticanalysis.CompareEnumsWithEqualityOperator: { } - org.openrewrite.staticanalysis.EqualsAvoidsNull: { } - com.shipmonk.tooling.openrewrite.cleanup.ThrowsFormattingRecipe: { } - com.shipmonk.tooling.openrewrite.cleanup.MethodDeclarationParametersWrappingRecipe: { } --- type: specs.openrewrite.org/v1beta/style name: com.shipmonk.Style styleConfigs: - org.openrewrite.java.style.ImportLayoutStyle: classCountToUseStarImport: 9999 nameCountToUseStarImport: 9999 layout: - import all other imports - - import jakarta.* - import javax.* - import java.* - - import static all other imports - org.openrewrite.java.style.TabsAndIndentsStyle: useTabCharacter: false tabSize: 4 indentSize: 4 continuationIndent: 4 indentsRelativeToExpressionStart: false methodDeclarationParameters: alignWhenMultiple: false ```

(The problem persists even if I disable my own recipes)

What is the full stack trace of any errors you encountered?

It doesn't like some of the switch statements, but only some and I don't see why. I was able to obtain these details by adding --errors to the mvn command

[WARNING] There were problems parsing relative-path/NotificationResultHandlingIntegrationManager.java
[WARNING] java.lang.IllegalStateException: relative-path/NotificationResultHandlingIntegrationManager.java is not print idempotent. 
diff --git a/absolute-path/NotificationResultHandlingIntegrationManager.java b/absolute-path/NotificationResultHandlingIntegrationManager.java
index d316ac8..9fcbe95 100644
--- a/absolute-path/NotificationResultHandlingIntegrationManager.java
+++ b/absolute-path/NotificationResultHandlingIntegrationManager.java
@@ -33,7 +33,7 @@ 

         for (PostSendActionResult operation : postSendHookOperations) {
             switch (operation) {
-                case PostSendActionResult.DocumentAsyncRequestCreation documentAsyncRequestCreation -> {
+                defaultcase PostSendActionResult.DocumentAsyncRequestCreation documentAsyncRequestCreation -> {
                     entityManager.persist(documentAsyncRequestCreation.documentAsyncRequest());
                 }
             }

  org.openrewrite.Parser.requirePrintEqualsInput(Parser.java:52)
knutwannheden commented 4 months ago

I think the issue is that OpenRewrite does not yet fully support Java's pattern matching for switch expressions and statements as previewed starting with Java 17 and then released with Java 21. This needs to be fixed in the parser (and printer).

gsmet commented 1 month ago

@timtebeek did this one fall through the cracks? I stumbled upon an old Quarkus issue complaining about it and this is a pattern that becomes more and more common.

In our case, the example was:

          return switch(rand) {
-            case String s -> s;
-            case Object o -> String.valueOf(o);
+            defaultcase String s -> s;
+            defaultcase Object o -> String.valueOf(o);
         };
timtebeek commented 1 month ago

hi @gsmet Thanks for the tag! Indeed we haven't yet built in support for switch expressions into our Java 21 parser. Definitely should though; especially as we're eyeing a Java 21 upgrade internally too, which will make us feel this pain too.