openrewrite / rewrite

Automated mass refactoring of source code.

Home Page:https://docs.openrewrite.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

java file "is not print idempotent" - case vs defaultcase

fprochazka opened this issue · comments

What version of OpenRewrite are you using?

I am using

  • JDK Temurin 21.0.1
  • OpenRewrite 8.27.1
  • Maven 3.9.5
  • Maven/Gradle plugin 5.32.1
  • logging module 2.7.2
  • static-analysis module 1.8.1
  • testing-frameworks module 2.10.1

How are you running OpenRewrite?

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

pom.xml
  <plugin>
      <groupId>org.openrewrite.maven</groupId>
      <artifactId>rewrite-maven-plugin</artifactId>
      <version>${maven-rewrite-plugin.version}</version>
      <dependencies>
          <dependency>
              <groupId>com.shipmonk.tooling</groupId>
              <artifactId>shipmonk-tooling-openrewrite</artifactId>
              <version>${project.version}</version>
          </dependency>
      </dependencies>
      <configuration>
          <configLocation>${shipmonk-framework.basedir}/src/openrewrite/rewrite.yml</configLocation>
          <activeRecipes>
              <recipe>com.shipmonk.Cleanup</recipe>
              <recipe>org.openrewrite.java.logging.slf4j.ParameterizedLogging</recipe>
              <recipe>com.shipmonk.tooling.openrewrite.cleanup.ThrowsFormattingRecipe</recipe>
              <recipe>com.shipmonk.tooling.openrewrite.cleanup.MethodDeclarationParametersWrappingRecipe</recipe>
          </activeRecipes>
          <checkstyleDetectionEnabled>false</checkstyleDetectionEnabled>
          <activeStyles>
              <style>com.shipmonk.Style</style>
              <style>com.shipmonk.tooling.openrewrite.style.ShipMonkCheckstyle</style>
          </activeStyles>
          <failOnInvalidActiveRecipes>true</failOnInvalidActiveRecipes>
          <skipMavenParsing>true</skipMavenParsing>
          <exclusions>
            <!-- relative from git root dir -->
            <exclusion>**/.data</exclusion>
            <exclusion>**/.idea</exclusion>
            <exclusion>**/.cache</exclusion>
            <exclusion>**/.mvn</exclusion>
            <exclusion>**/docs</exclusion>
            <exclusion>**/docker</exclusion>
            <exclusion>**/*.iml</exclusion>
            <exclusion>**/*.json</exclusion>
            <exclusion>**/*.xml</exclusion>
          </exclusions>
      </configuration>
  </plugin>
rewrite.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
        - <blank line>
        - import jakarta.*
        - import javax.*
        - import java.*
        - <blank line>
        - 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)

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