Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.NoMissingTypes;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.*;
import org.openrewrite.java.search.UsesJavaVersion;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
Expand All @@ -31,6 +29,8 @@

public class MigrateCollectionsSingletonList extends Recipe {
private static final MethodMatcher SINGLETON_LIST = new MethodMatcher("java.util.Collections singletonList(..)", true);
@Nullable
private static J.MethodInvocation listOfTemplate = null;

@Override
public String getDisplayName() {
Expand Down Expand Up @@ -62,18 +62,22 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext execu
if (SINGLETON_LIST.matches(method)) {
maybeRemoveImport("java.util.Collections");
maybeAddImport("java.util.List");
return autoFormat(m.withTemplate(
JavaTemplate
.builder(this::getCursor, "List.of(#{any()})")
.imports("java.util.List")
.build(),
m.getCoordinates().replace(),
m.getArguments().get(0)
), executionContext);
return getListOfTemplate().withArguments(m.getArguments()).withPrefix(m.getPrefix());
}

return m;
}
};
}

private static J.MethodInvocation getListOfTemplate() {
if (listOfTemplate == null) {
listOfTemplate = PartProvider.buildPart("import java.util.List;" +
"class A {\n" +
" Object a=List.of(\"X\");" +
"\n}", J.MethodInvocation.class);
}

return listOfTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,58 @@ public void defaults(RecipeSpec spec) {
spec.recipe(new MigrateCollectionsSingletonList());
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/186")
@Test
void templateError() {
rewriteRun(
version(
java(
"""
import java.util.*;

interface ConnectionListener {
void onCreate();
}

class A {
public void setConnectionListeners(List<? extends ConnectionListener> listeners) {
}

public void test() {
setConnectionListeners(Collections.singletonList(new ConnectionListener() {
@Override
public void onCreate() {
}
}));
}
}
""",
"""
import java.util.List;

interface ConnectionListener {
void onCreate();
}

class A {
public void setConnectionListeners(List<? extends ConnectionListener> listeners) {
}

public void test() {
setConnectionListeners(List.of(new ConnectionListener() {
@Override
public void onCreate() {
}
}));
}
}
"""
),
9
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
@Test
void singletonList() {
Expand Down