Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChangeStaticFieldToMethod: ternary expression parsing issue #4390

Open
crankydillo opened this issue Aug 6, 2024 · 1 comment
Open

ChangeStaticFieldToMethod: ternary expression parsing issue #4390

crankydillo opened this issue Aug 6, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@crankydillo
Copy link
Contributor

crankydillo commented Aug 6, 2024

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?

@crankydillo crankydillo added the bug Something isn't working label Aug 6, 2024
@timtebeek
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

2 participants