Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: UseEnumSetOf should ignore array and varargs parameters #518

Merged
merged 2 commits into from
Jul 26, 2024
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
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
)
);
}
}