Skip to content

Commit

Permalink
Fully remove part provider and last remaining references (#401)
Browse files Browse the repository at this point in the history
* Removed deprecated class references, replaced with new template. Need to test implementation.

* Apply suggestions from code review

* Update src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonList.java

* First batch of improvements

* Drop JavaTemplate; only change names & types

---------

Co-authored-by: Michel Gonzalez <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
4 people authored Feb 3, 2024
1 parent 02b5c6e commit 75000d1
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@
*/
package org.openrewrite.java.migrate.util;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.NoMissingTypes;
import org.openrewrite.java.PartProvider;
import org.openrewrite.java.search.UsesJavaVersion;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.JavaType.ShallowClass;
import org.openrewrite.java.tree.Space;

import java.util.Collections;

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

@Override
public String getDisplayName() {
Expand All @@ -47,29 +45,26 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
TreeVisitor<?, ExecutionContext> check = Preconditions.and(new UsesJavaVersion<>(9),
new UsesMethod<>(SINGLETON_LIST), new NoMissingTypes());
return Preconditions.check(check, new JavaVisitor<ExecutionContext>() {
return Preconditions.check(check, new JavaIsoVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (SINGLETON_LIST.matches(method)) {
maybeRemoveImport("java.util.Collections");
maybeAddImport("java.util.List");
return getListOfTemplate().withArguments(m.getArguments()).withPrefix(m.getPrefix());
}

JavaType.Class classType = ShallowClass.build("java.util.List");
JavaType.Method methodType = m.getMethodType().withName("of").withDeclaringType(classType);
m = m.withName(m.getName().withSimpleName("of").withType(methodType));
if (m.getSelect() instanceof J.Identifier) {
return m.withSelect(((J.Identifier) m.getSelect()).withSimpleName("List").withType(classType));
}
return m.withSelect(new J.Identifier(
Tree.randomId(), m.getPrefix(), m.getMarkers(), Collections.emptyList(), "List", classType, null))
.withPrefix(Space.EMPTY);
}
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 @@ -23,119 +23,137 @@
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;
import static org.openrewrite.java.Assertions.javaVersion;

class MigrateCollectionsSingletonListTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new MigrateCollectionsSingletonList());
spec.recipe(new MigrateCollectionsSingletonList())
.allSources(s -> s.markers(javaVersion(9)));
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/186")
@Test
void templateError() {
rewriteRun(
version(
java(
"""
import java.util.*;
interface ConnectionListener {
void onCreate();
}
//language=java
java(
"""
import java.util.*;
class A {
public void setConnectionListeners(List<? extends ConnectionListener> listeners) {
}
interface ConnectionListener {
void onCreate();
}
public void test() {
setConnectionListeners(Collections.singletonList(new ConnectionListener() {
@Override
public void onCreate() {
}
}));
}
}
""",
"""
import java.util.List;
class A {
public void setConnectionListeners(List<? extends ConnectionListener> listeners) {
}
interface ConnectionListener {
void onCreate();
}
public void test() {
setConnectionListeners(Collections.singletonList(new ConnectionListener() {
@Override
public void onCreate() {
}
}));
}
}
""",
"""
import java.util.List;
class A {
public void setConnectionListeners(List<? extends ConnectionListener> listeners) {
}
interface ConnectionListener {
void onCreate();
}
public void test() {
setConnectionListeners(List.of(new ConnectionListener() {
@Override
public void onCreate() {
}
}));
}
}
"""
),
9
class A {
public void setConnectionListeners(List<? extends ConnectionListener> listeners) {
}
public void test() {
setConnectionListeners(List.of(new ConnectionListener() {
@Override
public void onCreate() {
}
}));
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
@Test
void singletonList() {
//language=java
rewriteRun(
version(
java(
//language=java
java(
"""
import java.util.*;
class Test {
List<String> list = Collections.singletonList("ABC");
}
""",
"""
import java.util.List;
class Test {
List<String> list = List.of("ABC");
}
"""
import java.util.*;
class Test {
List<String> list = Collections.singletonList("ABC");
}
""",
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
@Test
void singletonListStaticImport() {
rewriteRun(
//language=java
java(
"""
import java.util.*;
import static java.util.Collections.singletonList;
class Test {
List<String> list = singletonList("ABC");
}
""",
"""
import java.util.List;
class Test {
List<String> list = List.of("ABC");
}
"""
import java.util.List;
class Test {
List<String> list = List.of("ABC");
}
"""
),
9
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
@Test
void singletonListCustomType() {
//language=java
rewriteRun(
version(
java(
"""
import java.util.*;
import java.time.LocalDate;
class Test {
List<LocalDate> list = Collections.singletonList(LocalDate.now());
}
""",
//language=java
java(
"""
import java.util.*;
import java.time.LocalDate;
class Test {
List<LocalDate> list = Collections.singletonList(LocalDate.now());
}
""",
"""
import java.util.List;
import java.time.LocalDate;
class Test {
List<LocalDate> list = List.of(LocalDate.now());
}
"""
import java.util.List;
import java.time.LocalDate;
class Test {
List<LocalDate> list = List.of(LocalDate.now());
}
"""
),
9
)
);
}
Expand All @@ -147,27 +165,24 @@ void lombokAllArgsConstructor() {
spec -> spec.parser(JavaParser.fromJavaVersion().classpath("lombok"))
.typeValidationOptions(TypeValidation.builder().constructorInvocations(false).build()),
//language=java
version(
java(
java(
"""
import lombok.AllArgsConstructor;
import java.util.List;
import static java.util.Collections.singletonList;
enum FooEnum {
FOO, BAR;
@AllArgsConstructor
public enum BarEnum {
foobar(singletonList(FOO));
private final List<FooEnum> expectedStates;
}
}
"""
import lombok.AllArgsConstructor;
import java.util.List;
import static java.util.Collections.singletonList;
enum FooEnum {
FOO, BAR;
@AllArgsConstructor
public enum BarEnum {
foobar(singletonList(FOO));
private final List<FooEnum> expectedStates;
}
}
"""
),
9
)
);
}
Expand Down

0 comments on commit 75000d1

Please sign in to comment.