Skip to content

Commit

Permalink
fix: UseEnumSetOf should ignore array and varargs parameters (#518)
Browse files Browse the repository at this point in the history
* 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 <tim@moderne.io>
  • Loading branch information
mbruggmann and timtebeek committed Jul 26, 2024
1 parent 59524b8 commit bc0d2eb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
maybeAddImport("java.util.EnumSet");

List<Expression> args = m.getArguments();
if (isArrayParameter(args)) {
return m;
}

StringJoiner setOf = new StringJoiner(", ", "EnumSet.of(", ")");
args.forEach(o -> setOf.add("#{any()}"));

Expand All @@ -93,6 +97,14 @@ private boolean isAssignmentSetOfEnum(@Nullable JavaType type) {
}
return false;
}

private boolean isArrayParameter(final List<Expression> args) {
if (args.size() != 1) {
return false;
}
JavaType type = args.get(0).getType();
return TypeUtils.asArray(type) != 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.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

Expand All @@ -38,7 +39,7 @@ void changeDeclaration() {
java(
"""
import java.util.Set;
class Test {
public enum Color {
RED, GREEN, BLUE
Expand All @@ -51,7 +52,7 @@ public void method() {
"""
import java.util.EnumSet;
import java.util.Set;
class Test {
public enum Color {
RED, GREEN, BLUE
Expand All @@ -75,7 +76,7 @@ void changeAssignment() {
java(
"""
import java.util.Set;
class Test {
public enum Color {
RED, GREEN, BLUE
Expand All @@ -89,7 +90,7 @@ public void method() {
"""
import java.util.EnumSet;
import java.util.Set;
class Test {
public enum Color {
RED, GREEN, BLUE
Expand All @@ -105,4 +106,55 @@ public void method() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/516")
void dontChangeVarargs() {
//language=java
rewriteRun(
version(
java(
"""
import java.util.Set;
class Test {
public enum Color {
RED, GREEN, BLUE
}
public void method(final Color... colors) {
Set<Color> s = Set.of(colors);
}
}
"""
),
9
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/516")
void dontChangeArray() {
//language=java
rewriteRun(
version(
java(
"""
import java.util.Set;
class Test {
public enum Color {
RED, GREEN, BLUE
}
public void method() {
Color[] colors = {};
Set<Color> s = Set.of(colors);
}
}
"""
),
9
)
);
}
}

0 comments on commit bc0d2eb

Please sign in to comment.