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 has issue when changing nested code #4382

Closed
crankydillo opened this issue Aug 5, 2024 · 0 comments · Fixed by #4383
Closed

ChangeStaticFieldToMethod has issue when changing nested code #4382

crankydillo opened this issue Aug 5, 2024 · 0 comments · Fixed by #4383
Assignees
Labels
bug Something isn't working

Comments

@crankydillo
Copy link
Contributor

I'll submit a test for this, but we uncovered the following situation when using the apache commons-collection 3->4 upgrade recipe.

We expect that ChangeStaticFieldToMethod will be able to change this:

import java.util.Collections;
class Foo {
    final Object o = java.util.Arrays(Collections.EMPTY_LIST);
}

into

import java.util.Lists;
class Foo {
    final Object o = java.util.Arrays(Lists.of());
}

However, when we use the recipe (via that composite recipe), we get a ClassCastException here:

Caused by: org.openrewrite.internal.RecipeRunException: java.lang.ClassCastException: class org.openrewrite.java.tree.J$NewArray cannot be cast to class org.openrewrite.java.tree.J$Block (org.openrewrite.java.tree.J$NewArray and org.openrewrite.java.tree.J$Block are in unnamed module of loader 'app')
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:287)
	at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:317)
[snip]
Caused by: java.lang.ClassCastException: class org.openrewrite.java.tree.J$NewArray cannot be cast to class org.openrewrite.java.tree.J$Block (org.openrewrite.java.tree.J$NewArray and org.openrewrite.java.tree.J$Block are in unnamed module of loader 'app')
	at org.openrewrite.java.ChangeStaticFieldToMethod$1.useNewMethod(ChangeStaticFieldToMethod.java:119)
	at org.openrewrite.java.ChangeStaticFieldToMethod$1.visitFieldAccess(ChangeStaticFieldToMethod.java:95)
	at org.openrewrite.java.ChangeStaticFieldToMethod$1.visitFieldAccess(ChangeStaticFieldToMethod.java:80)
	at org.openrewrite.java.tree.J$FieldAccess.acceptJava(J.java:1943)
	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?

rewrite's main branch

How are you running OpenRewrite?

IDE - running new test in ChangeStaticFieldToMethodTest. I'll push a draft PR soon.

@crankydillo crankydillo added the bug Something isn't working label Aug 5, 2024
crankydillo pushed a commit to crankydillo/rewrite that referenced this issue Aug 5, 2024
crankydillo pushed a commit to crankydillo/rewrite that referenced this issue Aug 5, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Aug 5, 2024
timtebeek added a commit that referenced this issue Aug 11, 2024
…4383)

* [4382] Add test showing issue with ChangeStaticFieldToMethod recipe..

and nested code.

 #4382

* Check for type returned from JavaTemplate to fix test

* Fix compilation issue

---------

Co-authored-by: Samuel Cox <sacox@paypal.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenRewrite Aug 11, 2024
@timtebeek timtebeek self-assigned this Aug 11, 2024
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
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants