diff --git a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java index d2155cd4f3..07bf1a8b70 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java @@ -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)") @@ -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 args) { if (args.size() != 1) { return false; diff --git a/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java b/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java index ba78e21246..0dd3f47357 100644 --- a/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java @@ -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 SUBSCRIBE_TO_ALL = Set.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() {