Skip to content

Commit

Permalink
Fix issue in ChangeStaticFieldToMethod when used in (implied) array (#…
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
3 people authored Aug 11, 2024
1 parent e73cb6a commit edd7d42
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,34 @@ class A {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4382")
void migratesNestedFieldInitializer() {
rewriteRun(
java(acmeLists),
java(
"""
import java.util.Collections;
class Foo {
void bar() {
final Object collection = java.util.Arrays.asList(Collections.EMPTY_LIST);
}
}
""",
"""
import com.acme.Lists;
class Foo {
void bar() {
final Object collection = java.util.Arrays.asList(Lists.of());
}
}
"""
)
);
}

@Test
void ignoresUnrelatedFields() {
rewriteRun(
Expand Down Expand Up @@ -237,7 +265,7 @@ void leavesOwnerAlone() {
java(
"""
package com.example;
class Test {
public static Object EXAMPLE = null;
}
Expand Down Expand Up @@ -276,7 +304,7 @@ public class HttpResponseStatus {
private HttpResponseStatus(int code) {
this.code = code;
}
String codeAsText() {
return String.valueOf(code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void shadedJar() {
.filter(o -> o.getFullyQualifiedName().startsWith("org.apache.hadoop.hbase.CacheEvictionStats"))
.findAny();
assertThat(shaded).isPresent();
assertThat(jss.getTypeToGav().get(shaded.get())).isEqualTo("org.apache.hbase:hbase-shaded-client:2.4.11");
assertThat(jss.getGavToTypes().get("org.apache.hbase:hbase-shaded-client:2.4.11")).contains(shaded.get());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,17 @@ private J useNewMethod(TypeTree tree) {

Cursor statementCursor = getCursor().dropParentUntil(Statement.class::isInstance);
Statement statement = statementCursor.getValue();
J.Block block = makeNewMethod(newClass).apply(statementCursor, statement.getCoordinates().replace());
J.MethodInvocation method = block.getStatements().get(0).withPrefix(tree.getPrefix());

if (method.getMethodType() == null) {
J applied = makeNewMethod(newClass).apply(statementCursor, statement.getCoordinates().replace());

J.MethodInvocation method = null;
if (applied instanceof J.Block) {
J.Block block = (J.Block) applied;
method = block.getStatements().get(0).withPrefix(tree.getPrefix());
} else if (applied instanceof J.NewArray) {
J.NewArray newArray = (J.NewArray) applied;
method = (J.MethodInvocation) newArray.getInitializer().get(0);
}
if (method == null || method.getMethodType() == null) {
throw new IllegalArgumentException("Error while changing a static field to a method. The generated template using a the new class ["
+ newClass + "] and the method [" + newMethodName + "] resulted in a null method type.");
}
Expand Down

0 comments on commit edd7d42

Please sign in to comment.