openrewrite / rewrite

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

ChangeStaticFieldToMethod: ternary expression parsing issue #4390

Open crankydillo opened 3 months ago

crankydillo commented 3 months ago

I may add a draft PR, but here's the reproducer for ChangeStaticFieldToMethodTest:

    @Test
    void migratesTernaryOperator() {
        rewriteRun(
          java(acmeLists),
          java(
            """
              import java.util.Collections;

              class A {
                  static Object empty() {
                      return (1 == 1) ? Collections.EMPTY_LIST : new RuntimeException("what universe is this?");

                  }
              }
              """,
            """
              import com.acme.Lists;

              class A {
                  static Object empty() {
                      return (1 == 1) ? Lists.of() : new RuntimeException("what universe is this?");
                  }
              }
              """
          )
        );
    }

Unexpected behavior

ChangeStaticFieldToMethodTest > migratesTernaryOperator() FAILED
    java.lang.AssertionError: Failed to parse sources or run recipe
        at org.openrewrite.test.RewriteTest.lambda$defaultExecutionContext$15(RewriteTest.java:632)
        at org.openrewrite.test.RewriteTest$$Lambda$490/0x000000900124dc38.accept(Unknown Source)
        at org.openrewrite.scheduling.RecipeRunCycle.handleError(RecipeRunCycle.java:242)
        at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$6(RecipeRunCycle.java:184)
[snip]
Caused by:
        org.openrewrite.internal.RecipeRunException: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
            at app//org.openrewrite.TreeVisitor.visit(TreeVisitor.java:287)
            at app//org.openrewrite.TreeVisitor.visit(TreeVisitor.java:147)
            at app//org.openrewrite.java.JavaTemplate.apply(JavaTemplate.java:115)
            at app//org.openrewrite.java.ChangeStaticFieldToMethod$1.useNewMethod(ChangeStaticFieldToMethod.java:119)
            at app//org.openrewrite.java.ChangeStaticFieldToMethod$1.visitFieldAccess(ChangeStaticFieldToMethod.java:95)
            at app//org.openrewrite.java.ChangeStaticFieldToMethod$1.visitFieldAccess(ChangeStaticFieldToMethod.java:80)
            at app//org.openrewrite.java.tree.J$FieldAccess.acceptJava(J.java:1943)
            at app//org.openrewrite.java.tree.J.accept(J.java:59)
[snip]
Caused by:
            java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
                at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
                at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
                at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
                at java.base/java.util.Objects.checkIndex(Objects.java:361)
                at java.base/java.util.ArrayList.get(ArrayList.java:427)
                at org.openrewrite.java.internal.template.JavaTemplateParser.parseExpression(JavaTemplateParser.java:118)
                at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitExpression(JavaTemplateJavaExtension.java:232)
                at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitExpression(JavaTemplateJavaExtension.java:56)
                at org.openrewrite.java.JavaVisitor.visitTernary(JavaVisitor.java:1151)
                at org.openrewrite.java.tree.J$Ternary.acceptJava(J.java:5163)
                at org.openrewrite.java.tree.J.accept(J.java:59)
                at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)

What version of OpenRewrite are you using?

main branch _ v1.2.3

How are you running OpenRewrite?

gradle build

Are you interested in contributing a fix to OpenRewrite?

timtebeek commented 3 months ago

Thanks for the runnable example! Indeed not covered yet, but the test should help uncover what needs to be done here.