Skip to content

Commit bc0d2eb

Browse files
fix: UseEnumSetOf should ignore array and varargs parameters (#518)
* fix: UseEnumSetOf should ignore array and varargs parameters EnumSet doesn't have the equivalent of `Set.of(arr[])`. For now, let's just skip those cases and bail out. * Minor polish --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 59524b8 commit bc0d2eb

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6767
maybeAddImport("java.util.EnumSet");
6868

6969
List<Expression> args = m.getArguments();
70+
if (isArrayParameter(args)) {
71+
return m;
72+
}
73+
7074
StringJoiner setOf = new StringJoiner(", ", "EnumSet.of(", ")");
7175
args.forEach(o -> setOf.add("#{any()}"));
7276

@@ -93,6 +97,14 @@ private boolean isAssignmentSetOfEnum(@Nullable JavaType type) {
9397
}
9498
return false;
9599
}
100+
101+
private boolean isArrayParameter(final List<Expression> args) {
102+
if (args.size() != 1) {
103+
return false;
104+
}
105+
JavaType type = args.get(0).getType();
106+
return TypeUtils.asArray(type) != null;
107+
}
96108
});
97109
}
98110
}

src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.migrate.util;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.Issue;
1920
import org.openrewrite.test.RecipeSpec;
2021
import org.openrewrite.test.RewriteTest;
2122

@@ -38,7 +39,7 @@ void changeDeclaration() {
3839
java(
3940
"""
4041
import java.util.Set;
41-
42+
4243
class Test {
4344
public enum Color {
4445
RED, GREEN, BLUE
@@ -51,7 +52,7 @@ public void method() {
5152
"""
5253
import java.util.EnumSet;
5354
import java.util.Set;
54-
55+
5556
class Test {
5657
public enum Color {
5758
RED, GREEN, BLUE
@@ -75,7 +76,7 @@ void changeAssignment() {
7576
java(
7677
"""
7778
import java.util.Set;
78-
79+
7980
class Test {
8081
public enum Color {
8182
RED, GREEN, BLUE
@@ -89,7 +90,7 @@ public void method() {
8990
"""
9091
import java.util.EnumSet;
9192
import java.util.Set;
92-
93+
9394
class Test {
9495
public enum Color {
9596
RED, GREEN, BLUE
@@ -105,4 +106,55 @@ public void method() {
105106
)
106107
);
107108
}
109+
110+
@Test
111+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/516")
112+
void dontChangeVarargs() {
113+
//language=java
114+
rewriteRun(
115+
version(
116+
java(
117+
"""
118+
import java.util.Set;
119+
120+
class Test {
121+
public enum Color {
122+
RED, GREEN, BLUE
123+
}
124+
public void method(final Color... colors) {
125+
Set<Color> s = Set.of(colors);
126+
}
127+
}
128+
"""
129+
),
130+
9
131+
)
132+
);
133+
}
134+
135+
@Test
136+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/516")
137+
void dontChangeArray() {
138+
//language=java
139+
rewriteRun(
140+
version(
141+
java(
142+
"""
143+
import java.util.Set;
144+
145+
class Test {
146+
public enum Color {
147+
RED, GREEN, BLUE
148+
}
149+
public void method() {
150+
Color[] colors = {};
151+
Set<Color> s = Set.of(colors);
152+
}
153+
}
154+
"""
155+
),
156+
9
157+
)
158+
);
159+
}
108160
}

0 commit comments

Comments
 (0)