Skip to content

Commit

Permalink
Do not covert literal nulls in singletonList/Set
Browse files Browse the repository at this point in the history
Fixes #571
  • Loading branch information
timtebeek committed Oct 6, 2024
1 parent fe412ee commit e0d2127
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package org.openrewrite.java.migrate.util;

import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.NoMissingTypes;
import org.openrewrite.java.search.UsesJavaVersion;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.JavaType.ShallowClass;
Expand Down Expand Up @@ -49,7 +51,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (SINGLETON_LIST.matches(method)) {
if (SINGLETON_LIST.matches(m) && isNotLiteralNull(m)) {
maybeRemoveImport("java.util.Collections");
maybeAddImport("java.util.List");

Expand All @@ -65,6 +67,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}
return m;
}

private boolean isNotLiteralNull(J.MethodInvocation m) {
return !(m.getArguments().get(0) instanceof J.Literal &&
((J.Literal) m.getArguments().get(0)).getValue() == null);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (SINGLETON_SET.matches(method)) {
if (SINGLETON_SET.matches(m) && isNotLiteralNull(m)) {
maybeRemoveImport("java.util.Collections");
maybeAddImport("java.util.Set");
return JavaTemplate.builder("Set.of(#{any()})")
Expand All @@ -58,6 +58,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
}
return m;
}

private boolean isNotLiteralNull(J.MethodInvocation m) {
return !(m.getArguments().get(0) instanceof J.Literal &&
((J.Literal) m.getArguments().get(0)).getValue() == null);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java.migrate.util;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
Expand All @@ -41,15 +42,15 @@ void templateError() {
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
Expand All @@ -61,15 +62,15 @@ 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
Expand All @@ -83,6 +84,7 @@ public void onCreate() {
);
}

@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
@Test
void singletonList() {
Expand All @@ -91,14 +93,14 @@ void singletonList() {
java(
"""
import java.util.*;
class Test {
List<String> list = Collections.singletonList("ABC");
}
""",
"""
import java.util.List;
class Test {
List<String> list = List.of("ABC");
}
Expand All @@ -116,14 +118,14 @@ void singletonListStaticImport() {
"""
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");
}
Expand All @@ -141,15 +143,15 @@ void singletonListCustomType() {
"""
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());
}
Expand All @@ -169,21 +171,38 @@ void lombokAllArgsConstructor() {
"""
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;
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/571")
@Test
void shouldNotConvertLiteralNull() {
rewriteRun(
//language=java
java(
"""
import java.util.*;
class Test {
List<String> list = Collections.singletonList(null);
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java.migrate.util;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -30,6 +31,7 @@ public void defaults(RecipeSpec spec) {
spec.recipe(new MigrateCollectionsSingletonSet());
}

@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
@Test
void singleTonSet() {
Expand Down Expand Up @@ -85,4 +87,24 @@ class Test {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/571")
@Test
void shouldNotConvertLiteralNull() {
//language=java
rewriteRun(
version(
java(
"""
import java.util.*;
class Test {
Set<String> set = Collections.singleton(null);
}
"""
),
9
)
);
}
}

0 comments on commit e0d2127

Please sign in to comment.