Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
}

if (args.get(0) instanceof J.Empty) {
if (isStaticField(parent)) {
return mi;
}
JavaType firstTypeParameter = ((JavaType.Parameterized) type).getTypeParameters().get(0);
JavaType.ShallowClass shallowClass = JavaType.ShallowClass.build(firstTypeParameter.toString());
return JavaTemplate.builder("EnumSet.noneOf(" + shallowClass.getClassName() + ".class)")
Expand Down Expand Up @@ -120,6 +123,11 @@ private boolean isAssignmentSetOfEnum(@Nullable JavaType type) {
return false;
}

private boolean isStaticField(Cursor parent) {
return parent.getValue() instanceof J.VariableDeclarations &&
((J.VariableDeclarations) parent.getValue()).hasModifier(J.Modifier.Type.Static);
}

private boolean isArrayParameter(final List<Expression> args) {
if (args.size() != 1) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,45 @@ public void method() {
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/1157")
@Test
void dontConvertEmptySetOfInStaticField() {
//language=java
rewriteRun(
java(
"""
package com.helloworld;

import java.util.Set;

public enum ConfigType {
ENUM,
BOOLEAN,
INTEGER;

public static final Set<ConfigProperty> SUBSCRIBE_TO_ALL = Set.<ConfigProperty>of();
}
""",
spec -> spec.markers(javaVersion(25))),
java(
"""
package com.helloworld;

import static com.helloworld.ConfigType.BOOLEAN;
import static com.helloworld.ConfigType.ENUM;
import static com.helloworld.ConfigType.INTEGER;

public enum ConfigProperty {
TRIAL_TYPE(ENUM),
IS_TRIAL_ENABLED(BOOLEAN),
MIN_ACCOUNT_AGE_MINUTES(INTEGER);

ConfigProperty(final ConfigType type) {}
}
""",
spec -> spec.markers(javaVersion(25))));
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/958")
@Test
void retainEmptySetOf() {
Expand Down
Loading