Skip to content

RenameExceptionInEmptyCatch might fail with IllegalStateException on a Kotlin or Groovy build script #701

@greg-at-moderne

Description

@greg-at-moderne

What version of OpenRewrite are you using?

OR current main (7edbbf01ab8a9b666ef3c2003ee7b8e0c66a4967)
rewrite-static-analysis (428d8b0)

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

Add the following test to a new file src/test/java/org/openrewrite/staticanalysis/kotlin/RenameExceptionInEmptyCatchTest.java:

    @Override
    public void defaults(RecipeSpec spec) {
        spec.recipe(new RenameExceptionInEmptyCatch());
    }

    @Test
    void justAnImport() {
        rewriteRun(
          //language=java
          kotlin(
            """
            import nebula.plugin.contacts.Contact
            """
          )
        );
    }

Observed

Expected to find a matching parent for Cursor{Identifier->FieldAccess->FieldAccess->FieldAccess->Import->JRightPadded(element=import nebula.plugin.contacts.Contact, after=Space(comments=<0 comments>, whitespace=<empty>))->CompilationUnit->root}
java.lang.IllegalStateException: Expected to find a matching parent for Cursor{Identifier->FieldAccess->FieldAccess->FieldAccess->Import->JRightPadded(element=import nebula.plugin.contacts.Contact, after=Space(comments=<0 comments>, whitespace=<empty>))->CompilationUnit->root}
	at org.openrewrite.Cursor.dropParentUntil(Cursor.java:200)
	at org.openrewrite.staticanalysis.RenameExceptionInEmptyCatch$1.getCursorToParentScope(RenameExceptionInEmptyCatch.java:164)

OSS repro

Groovy

Same story for Groovy
e.g. https://github.com/apache/maven-dependency-plugin/blob/0ff6c5330ecdb6544cf7a649f375bf4769b13488/src/it/projects/mdep-752-analyze-ignored-packaging-defaults/verify.groovy#L20
gives

java.lang.IllegalStateException: Expected to find a matching parent for Cursor{VariableDeclarations->CompilationUnit->root}
  org.openrewrite.Cursor.dropParentUntil(Cursor.java:200)
  org.openrewrite.staticanalysis.RenameExceptionInEmptyCatch$1.getCursorToParentScope(RenameExceptionInEmptyCatch.java:164)
  org.openrewrite.staticanalysis.RenameExceptionInEmptyCatch$1.visitVariableDeclarations(RenameExceptionInEmptyCatch.java:106)
  org.openrewrite.staticanalysis.RenameExceptionInEmptyCatch$1.visitVariableDeclarations(RenameExceptionInEmptyCatch.java:51)
  org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:6166)
  org.openrewrite.java.tree.J.accept(J.java:60)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
  org.openrewrite.staticanalysis.RenameExceptionInEmptyCatch$1.visit(RenameExceptionInEmptyCatch.java:60)
  org.openrewrite.staticanalysis.RenameExceptionInEmptyCatch$1.visit(RenameExceptionInEmptyCatch.java:51)
  org.openrewrite.staticanalysis.RenameExceptionInEmptyCatch_1_GroovyVisitor.visit(RenameExceptionInEmptyCatch_1_GroovyVisitor.zig:333)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions