openrewrite / rewrite

Automated mass refactoring of source code.

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Groovy Enum is throwing "Failed to parse" "UnsupportedOperationException: enum fields are not implemented."

gtiwari333 opened this issue · comments

What version of OpenRewrite are you using?

I am using https://github.com/gtiwari333/openrewrite-groovy-bug/blob/main/init.gradle

  • org.openrewrite.recipe:rewrite-recipe-bom - 2.1.20
  • org.openrewrite:rewrite-gradle-plugin - latest.release
  • groovy 4.0.21
  • jdk 17/21
  • gradle 8.8

How are you running OpenRewrite?

%./gradlew clean rewriteRun --init-script init.gradle --info

What is the smallest, simplest way to reproduce the problem?

Checkout the following project and run ./gradlew clean rewriteRun --init-script init.gradle --info

Im seeing similar behavior with both jdk17 and jdk21

https://github.com/gtiwari333/openrewrite-groovy-bug/blob/main/README.md

What did you expect to see?

No failures. AutoFormat recipe should run on the code.

What did you see instead?

org.openrewrite.groovy.GroovyParsingException: Failed to parse src/main/groovy/com/example/demo/Status.groovy at cursor position 39. The next 10 characters in the original source are ` //enum al`
       at org.openrewrite.groovy.GroovyParserVisitor.visit(GroovyParserVisitor.java:175)
....
 Caused by: java.lang.UnsupportedOperationException: enum fields are not implemented.
       at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyClassVisitor.visitEnumField(GroovyParserVisitor.java:357)
       at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyClassVisitor.visitField(GroovyParserVisitor.java:348)
       at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyClassVisitor.lambda$visitClassBlock$3(GroovyParserVisitor.java:332)
       at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
       at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)

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

Run the ./gradlew clean rewriteRun --init-script init.gradle --info command to get full stacktrace. I've provided relevant bits above.

Are you interested in contributing a fix to OpenRewrite?

Not too familiar how AST works. But i can try.

Thanks for the helpful sample application @gtiwari333 ! Looks like we're indeed running up against this warning when processing the enum.

private void visitEnumField(@SuppressWarnings("unused") FieldNode fieldNode) {
// Requires refactoring visitClass to use a similar pattern as Java11ParserVisitor.
// Currently, each field is visited one at a time, so we cannot construct the EnumValueSet.
throw new UnsupportedOperationException("enum fields are not implemented.");
}

We can replicate this on a smaller scale by adding this test to

class AutoFormatVisitorTest implements RewriteTest {

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4252")
void skipEnum() {
    rewriteRun(
      groovy(
        """
          package com.example.demo

          enum Status {
              WaitingToBeProcessed,
              InProcess,
              Complete,
              Failed
          }
          """
      )
    );
}

There's a hint in visitEnumField on how this could be achieved; you're welcome to give this a go if you'd like. It'd be kind of hard to fit in for me at this moment; we'd mostly added Groovy support for Gradle, such that enums or running autoformat there are rare, which is why you're only encountering this now.