diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeStaticFieldToMethodTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeStaticFieldToMethodTest.java index 617379a4942..8559c3583e7 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeStaticFieldToMethodTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeStaticFieldToMethodTest.java @@ -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( @@ -237,7 +265,7 @@ void leavesOwnerAlone() { java( """ package com.example; - + class Test { public static Object EXAMPLE = null; } @@ -276,7 +304,7 @@ public class HttpResponseStatus { private HttpResponseStatus(int code) { this.code = code; } - + String codeAsText() { return String.valueOf(code); } diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/marker/JavaSourceSetTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/marker/JavaSourceSetTest.java index 53befc08176..f9b4e76f0ec 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/marker/JavaSourceSetTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/marker/JavaSourceSetTest.java @@ -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 diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ChangeStaticFieldToMethod.java b/rewrite-java/src/main/java/org/openrewrite/java/ChangeStaticFieldToMethod.java index be4578b7445..9152f8a45f6 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ChangeStaticFieldToMethod.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ChangeStaticFieldToMethod.java @@ -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."); }