From 2bbad93bc8c8c686268980ac9da83424fdffe07a Mon Sep 17 00:00:00 2001 From: bramli ahmed khalil Date: Sat, 5 Oct 2024 09:59:48 +0200 Subject: [PATCH 1/8] issue-490: Rewriting with EnumSet.noneOf from empty Set.of() by UseEnumSetOf --- .../java/migrate/util/UseEnumSetOf.java | 58 ++++++++++++++----- .../java/migrate/util/UseEnumSetOfTest.java | 37 ++++++++++++ 2 files changed, 80 insertions(+), 15 deletions(-) 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 61b169b8a1..c20607ffbb 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java @@ -15,6 +15,9 @@ */ package org.openrewrite.java.migrate.util; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; + import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.java.JavaIsoVisitor; @@ -25,6 +28,7 @@ import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.Space; import org.openrewrite.java.tree.TypeUtils; import java.time.Duration; @@ -33,7 +37,7 @@ public class UseEnumSetOf extends Recipe { private static final MethodMatcher SET_OF = new MethodMatcher("java.util.Set of(..)", true); - + private static final String METHOD_TYPE = "java.util.EnumSet"; @Override public String getDisplayName() { return "Prefer `EnumSet of(..)`"; @@ -55,35 +59,59 @@ public TreeVisitor getVisitor() { new UsesMethod<>(SET_OF)), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation m = super.visitMethodInvocation(method, ctx); + J.MethodInvocation methodInvocation = super.visitMethodInvocation(method, ctx); - if (SET_OF.matches(method) && method.getType() instanceof JavaType.Parameterized && - !TypeUtils.isOfClassType(method.getType(), "java.util.EnumSet")) { + if (SET_OF.matches(method) && method.getType() instanceof JavaType.Parameterized + && !TypeUtils.isOfClassType(method.getType(), METHOD_TYPE)) { Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Assignment || is instanceof J.VariableDeclarations || is instanceof J.Block); if (!(parent.getValue() instanceof J.Block)) { JavaType type = parent.getValue() instanceof J.Assignment ? ((J.Assignment) parent.getValue()).getType() : ((J.VariableDeclarations) parent.getValue()).getVariables().get(0).getType(); if (isAssignmentSetOfEnum(type)) { - maybeAddImport("java.util.EnumSet"); + maybeAddImport(METHOD_TYPE); - List args = m.getArguments(); + List args = methodInvocation.getArguments(); if (isArrayParameter(args)) { - return m; + return methodInvocation; } - - StringJoiner setOf = new StringJoiner(", ", "EnumSet.of(", ")"); + StringJoiner setOf = initStringJoiner(args); args.forEach(o -> setOf.add("#{any()}")); - return JavaTemplate.builder(setOf.toString()) - .contextSensitive() - .imports("java.util.EnumSet") - .build() - .apply(updateCursor(m), m.getCoordinates().replace(), args.toArray()); + return createNewMethodInvocation(methodInvocation, (JavaType.Parameterized) type, setOf); } } } - return m; + return methodInvocation; + } + + private StringJoiner initStringJoiner(List args) { + if(args.get(0) instanceof J.Empty) { + return new StringJoiner(", ", "EnumSet.noneOf(", ")"); + } + return new StringJoiner(", ", "EnumSet.of(", ")"); + } + + private J.MethodInvocation createNewMethodInvocation(J.MethodInvocation methodInvocation, + JavaType.Parameterized type, + StringJoiner setOf) { + J.MethodInvocation visitMethodInvocation = JavaTemplate.builder(setOf.toString()) + .contextSensitive() + .imports(METHOD_TYPE) + .build() + .apply(updateCursor(methodInvocation), methodInvocation.getCoordinates().replace(), + methodInvocation.getArguments().toArray()); + + if (methodInvocation.getArguments().get(0) instanceof J.Empty) { + JavaType.Method methodType = methodInvocation.getMethodType().withName("noneOf"); + JavaType.Class parameterType = JavaType.ShallowClass.build( + type.getTypeParameters().get(0).toString()); + return visitMethodInvocation.withMethodType(methodType) + .withArguments(singletonList(new J.Identifier( + Tree.randomId(), Space.EMPTY, methodInvocation.getMarkers(), emptyList(), + parameterType.getClassName() + ".class", parameterType, null))); } + return visitMethodInvocation; + } private boolean isAssignmentSetOfEnum(@Nullable JavaType type) { if (type instanceof JavaType.Parameterized) { 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 38ea2d0594..2a0e5322f9 100644 --- a/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java @@ -157,4 +157,41 @@ public void method() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/490") + void nonof() { + //language=java + rewriteRun( + version( + java( + """ + import java.util.Set; + import java.util.concurrent.TimeUnit; + class Test { + + public void method() { + Set warm = Set.of(); + } + } + """, + """ + import java.util.EnumSet; + import java.util.Set; + import java.util.concurrent.TimeUnit; + + class Test { + + public void method() { + Set warm = EnumSet.noneOf(TimeUnit.class); + } + } + """ + ), + 9 + ) + ); + } + + } From 51507d887dd7852feb539dcb8334e69e9ab2810a Mon Sep 17 00:00:00 2001 From: bramli ahmed khalil Date: Sat, 5 Oct 2024 10:06:35 +0200 Subject: [PATCH 2/8] refactoring code --- .../java/migrate/util/UseEnumSetOf.java | 119 +++++++++--------- 1 file changed, 61 insertions(+), 58 deletions(-) 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 c20607ffbb..8b3778bdc6 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java @@ -20,8 +20,8 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.*; -import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesJavaVersion; import org.openrewrite.java.search.UsesMethod; @@ -56,44 +56,47 @@ public Duration getEstimatedEffortPerOccurrence() { @Override public TreeVisitor getVisitor() { return Preconditions.check(Preconditions.and(new UsesJavaVersion<>(9), - new UsesMethod<>(SET_OF)), new JavaIsoVisitor() { - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation methodInvocation = super.visitMethodInvocation(method, ctx); - - if (SET_OF.matches(method) && method.getType() instanceof JavaType.Parameterized - && !TypeUtils.isOfClassType(method.getType(), METHOD_TYPE)) { - Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Assignment || is instanceof J.VariableDeclarations || is instanceof J.Block); - if (!(parent.getValue() instanceof J.Block)) { - JavaType type = parent.getValue() instanceof J.Assignment ? - ((J.Assignment) parent.getValue()).getType() : ((J.VariableDeclarations) parent.getValue()).getVariables().get(0).getType(); - if (isAssignmentSetOfEnum(type)) { - maybeAddImport(METHOD_TYPE); - - List args = methodInvocation.getArguments(); - if (isArrayParameter(args)) { - return methodInvocation; - } - StringJoiner setOf = initStringJoiner(args); - args.forEach(o -> setOf.add("#{any()}")); - - return createNewMethodInvocation(methodInvocation, (JavaType.Parameterized) type, setOf); + new UsesMethod<>(SET_OF)), new UseEnumSetOfVisitor()); + } + + private static class UseEnumSetOfVisitor extends JavaVisitor { + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation methodInvocation = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); + + if (SET_OF.matches(method) && method.getType() instanceof JavaType.Parameterized + && !TypeUtils.isOfClassType(method.getType(), METHOD_TYPE)) { + Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Assignment || is instanceof J.VariableDeclarations || is instanceof J.Block); + if (!(parent.getValue() instanceof J.Block)) { + JavaType type = parent.getValue() instanceof J.Assignment ? + ((J.Assignment) parent.getValue()).getType() : ((J.VariableDeclarations) parent.getValue()).getVariables().get(0).getType(); + if (isAssignmentSetOfEnum(type)) { + maybeAddImport(METHOD_TYPE); + + List args = methodInvocation.getArguments(); + if (isArrayParameter(args)) { + return methodInvocation; } + StringJoiner setOf = initStringJoiner(args); + args.forEach(o -> setOf.add("#{any()}")); + + return createNewMethodInvocation(methodInvocation, (JavaType.Parameterized) type, setOf); } } - return methodInvocation; } + return methodInvocation; + } - private StringJoiner initStringJoiner(List args) { - if(args.get(0) instanceof J.Empty) { - return new StringJoiner(", ", "EnumSet.noneOf(", ")"); - } - return new StringJoiner(", ", "EnumSet.of(", ")"); + private StringJoiner initStringJoiner(List args) { + if(args.get(0) instanceof J.Empty) { + return new StringJoiner(", ", "EnumSet.noneOf(", ")"); } + return new StringJoiner(", ", "EnumSet.of(", ")"); + } - private J.MethodInvocation createNewMethodInvocation(J.MethodInvocation methodInvocation, - JavaType.Parameterized type, - StringJoiner setOf) { + private J.MethodInvocation createNewMethodInvocation(J.MethodInvocation methodInvocation, + JavaType.Parameterized type, + StringJoiner setOf) { J.MethodInvocation visitMethodInvocation = JavaTemplate.builder(setOf.toString()) .contextSensitive() .imports(METHOD_TYPE) @@ -102,37 +105,37 @@ private J.MethodInvocation createNewMethodInvocation(J.MethodInvocation methodIn methodInvocation.getArguments().toArray()); if (methodInvocation.getArguments().get(0) instanceof J.Empty) { - JavaType.Method methodType = methodInvocation.getMethodType().withName("noneOf"); - JavaType.Class parameterType = JavaType.ShallowClass.build( - type.getTypeParameters().get(0).toString()); - return visitMethodInvocation.withMethodType(methodType) - .withArguments(singletonList(new J.Identifier( - Tree.randomId(), Space.EMPTY, methodInvocation.getMarkers(), emptyList(), - parameterType.getClassName() + ".class", parameterType, null))); + JavaType.Method methodType = methodInvocation.getMethodType().withName("noneOf"); + JavaType.Class parameterType = JavaType.ShallowClass.build( + type.getTypeParameters().get(0).toString()); + return visitMethodInvocation.withMethodType(methodType) + .withArguments(singletonList(new J.Identifier( + Tree.randomId(), Space.EMPTY, methodInvocation.getMarkers(), emptyList(), + parameterType.getClassName() + ".class", parameterType, null))); } return visitMethodInvocation; - } - - private boolean isAssignmentSetOfEnum(@Nullable JavaType type) { - if (type instanceof JavaType.Parameterized) { - JavaType.Parameterized parameterized = (JavaType.Parameterized) type; - if (TypeUtils.isOfClassType(parameterized.getType(), "java.util.Set")) { - return ((JavaType.Parameterized) type).getTypeParameters().stream() - .filter(org.openrewrite.java.tree.JavaType.Class.class::isInstance) - .map(org.openrewrite.java.tree.JavaType.Class.class::cast) - .anyMatch(o -> o.getKind() == JavaType.FullyQualified.Kind.Enum); - } + } + + private boolean isAssignmentSetOfEnum(@Nullable JavaType type) { + if (type instanceof JavaType.Parameterized) { + JavaType.Parameterized parameterized = (JavaType.Parameterized) type; + if (TypeUtils.isOfClassType(parameterized.getType(), "java.util.Set")) { + return ((JavaType.Parameterized) type).getTypeParameters().stream() + .filter(org.openrewrite.java.tree.JavaType.Class.class::isInstance) + .map(org.openrewrite.java.tree.JavaType.Class.class::cast) + .anyMatch(o -> o.getKind() == JavaType.FullyQualified.Kind.Enum); } - return false; } + return false; + } - private boolean isArrayParameter(final List args) { - if (args.size() != 1) { - return false; - } - JavaType type = args.get(0).getType(); - return TypeUtils.asArray(type) != null; + private boolean isArrayParameter(final List args) { + if (args.size() != 1) { + return false; } - }); + JavaType type = args.get(0).getType(); + return TypeUtils.asArray(type) != null; + } } + } From e7243ec4205482b09d15a090a02578d0062e39b0 Mon Sep 17 00:00:00 2001 From: bramli ahmed khalil Date: Sat, 5 Oct 2024 10:24:31 +0200 Subject: [PATCH 3/8] format code --- .../java/migrate/util/UseEnumSetOf.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) 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 8b3778bdc6..e899a0d353 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java @@ -18,8 +18,16 @@ import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; +import java.time.Duration; +import java.util.List; +import java.util.StringJoiner; import org.jspecify.annotations.Nullable; -import org.openrewrite.*; +import org.openrewrite.Cursor; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.Tree; +import org.openrewrite.TreeVisitor; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; @@ -31,10 +39,6 @@ import org.openrewrite.java.tree.Space; import org.openrewrite.java.tree.TypeUtils; -import java.time.Duration; -import java.util.List; -import java.util.StringJoiner; - public class UseEnumSetOf extends Recipe { private static final MethodMatcher SET_OF = new MethodMatcher("java.util.Set of(..)", true); private static final String METHOD_TYPE = "java.util.EnumSet"; @@ -64,8 +68,9 @@ private static class UseEnumSetOfVisitor extends JavaVisitor { public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation methodInvocation = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); - if (SET_OF.matches(method) && method.getType() instanceof JavaType.Parameterized - && !TypeUtils.isOfClassType(method.getType(), METHOD_TYPE)) { + if (SET_OF.matches(method) && + method.getType() instanceof JavaType.Parameterized && + !TypeUtils.isOfClassType(method.getType(), METHOD_TYPE)) { Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Assignment || is instanceof J.VariableDeclarations || is instanceof J.Block); if (!(parent.getValue() instanceof J.Block)) { JavaType type = parent.getValue() instanceof J.Assignment ? From cfab8a5ba445b4ab8cef8d43bd89fe221126122b Mon Sep 17 00:00:00 2001 From: bramli ahmed khalil Date: Sat, 5 Oct 2024 19:26:49 +0200 Subject: [PATCH 4/8] change test name --- .../org/openrewrite/java/migrate/util/UseEnumSetOfTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 2a0e5322f9..161cfb96c7 100644 --- a/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java @@ -160,8 +160,7 @@ public void method() { @Test @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/490") - void nonof() { - //language=java + void dontHaveArgs() { rewriteRun( version( java( From f34e93633a73c02533a929906724fbda5f97fcc8 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 6 Oct 2024 00:28:13 +0200 Subject: [PATCH 5/8] Apply formatter --- .../java/migrate/util/UseEnumSetOf.java | 57 ++++++++----------- .../java/migrate/util/UseEnumSetOfTest.java | 14 ++--- 2 files changed, 32 insertions(+), 39 deletions(-) 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 e899a0d353..c9f66b248c 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java @@ -15,33 +15,26 @@ */ package org.openrewrite.java.migrate.util; -import static java.util.Collections.emptyList; -import static java.util.Collections.singletonList; - -import java.time.Duration; -import java.util.List; -import java.util.StringJoiner; import org.jspecify.annotations.Nullable; -import org.openrewrite.Cursor; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.Tree; -import org.openrewrite.TreeVisitor; +import org.openrewrite.*; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesJavaVersion; import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.Expression; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JavaType; -import org.openrewrite.java.tree.Space; -import org.openrewrite.java.tree.TypeUtils; +import org.openrewrite.java.tree.*; + +import java.time.Duration; +import java.util.List; +import java.util.StringJoiner; + +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; public class UseEnumSetOf extends Recipe { private static final MethodMatcher SET_OF = new MethodMatcher("java.util.Set of(..)", true); private static final String METHOD_TYPE = "java.util.EnumSet"; + @Override public String getDisplayName() { return "Prefer `EnumSet of(..)`"; @@ -66,7 +59,7 @@ public TreeVisitor getVisitor() { private static class UseEnumSetOfVisitor extends JavaVisitor { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation methodInvocation = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); + J.MethodInvocation methodInvocation = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); if (SET_OF.matches(method) && method.getType() instanceof JavaType.Parameterized && @@ -74,7 +67,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Assignment || is instanceof J.VariableDeclarations || is instanceof J.Block); if (!(parent.getValue() instanceof J.Block)) { JavaType type = parent.getValue() instanceof J.Assignment ? - ((J.Assignment) parent.getValue()).getType() : ((J.VariableDeclarations) parent.getValue()).getVariables().get(0).getType(); + ((J.Assignment) parent.getValue()).getType() : ((J.VariableDeclarations) parent.getValue()).getVariables().get(0).getType(); if (isAssignmentSetOfEnum(type)) { maybeAddImport(METHOD_TYPE); @@ -93,7 +86,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu } private StringJoiner initStringJoiner(List args) { - if(args.get(0) instanceof J.Empty) { + if (args.get(0) instanceof J.Empty) { return new StringJoiner(", ", "EnumSet.noneOf(", ")"); } return new StringJoiner(", ", "EnumSet.of(", ")"); @@ -103,20 +96,20 @@ private J.MethodInvocation createNewMethodInvocation(J.MethodInvocation methodIn JavaType.Parameterized type, StringJoiner setOf) { J.MethodInvocation visitMethodInvocation = JavaTemplate.builder(setOf.toString()) - .contextSensitive() - .imports(METHOD_TYPE) - .build() - .apply(updateCursor(methodInvocation), methodInvocation.getCoordinates().replace(), - methodInvocation.getArguments().toArray()); + .contextSensitive() + .imports(METHOD_TYPE) + .build() + .apply(updateCursor(methodInvocation), methodInvocation.getCoordinates().replace(), + methodInvocation.getArguments().toArray()); if (methodInvocation.getArguments().get(0) instanceof J.Empty) { JavaType.Method methodType = methodInvocation.getMethodType().withName("noneOf"); JavaType.Class parameterType = JavaType.ShallowClass.build( - type.getTypeParameters().get(0).toString()); + type.getTypeParameters().get(0).toString()); return visitMethodInvocation.withMethodType(methodType) - .withArguments(singletonList(new J.Identifier( - Tree.randomId(), Space.EMPTY, methodInvocation.getMarkers(), emptyList(), - parameterType.getClassName() + ".class", parameterType, null))); + .withArguments(singletonList(new J.Identifier( + Tree.randomId(), Space.EMPTY, methodInvocation.getMarkers(), emptyList(), + parameterType.getClassName() + ".class", parameterType, null))); } return visitMethodInvocation; } @@ -126,9 +119,9 @@ private boolean isAssignmentSetOfEnum(@Nullable JavaType type) { JavaType.Parameterized parameterized = (JavaType.Parameterized) type; if (TypeUtils.isOfClassType(parameterized.getType(), "java.util.Set")) { return ((JavaType.Parameterized) type).getTypeParameters().stream() - .filter(org.openrewrite.java.tree.JavaType.Class.class::isInstance) - .map(org.openrewrite.java.tree.JavaType.Class.class::cast) - .anyMatch(o -> o.getKind() == JavaType.FullyQualified.Kind.Enum); + .filter(org.openrewrite.java.tree.JavaType.Class.class::isInstance) + .map(org.openrewrite.java.tree.JavaType.Class.class::cast) + .anyMatch(o -> o.getKind() == JavaType.FullyQualified.Kind.Enum); } } 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 161cfb96c7..40e65c62cf 100644 --- a/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java @@ -39,7 +39,7 @@ void changeDeclaration() { java( """ import java.util.Set; - + class Test { public enum Color { RED, GREEN, BLUE @@ -52,7 +52,7 @@ public void method() { """ import java.util.EnumSet; import java.util.Set; - + class Test { public enum Color { RED, GREEN, BLUE @@ -76,7 +76,7 @@ void changeAssignment() { java( """ import java.util.Set; - + class Test { public enum Color { RED, GREEN, BLUE @@ -90,7 +90,7 @@ public void method() { """ import java.util.EnumSet; import java.util.Set; - + class Test { public enum Color { RED, GREEN, BLUE @@ -116,7 +116,7 @@ void dontChangeVarargs() { java( """ import java.util.Set; - + class Test { public enum Color { RED, GREEN, BLUE @@ -141,7 +141,7 @@ void dontChangeArray() { java( """ import java.util.Set; - + class Test { public enum Color { RED, GREEN, BLUE @@ -180,7 +180,7 @@ public void method() { import java.util.concurrent.TimeUnit; class Test { - + public void method() { Set warm = EnumSet.noneOf(TimeUnit.class); } From 9d13d74226cc5fb066f38aeafefb9d2ffc189b32 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 6 Oct 2024 00:50:36 +0200 Subject: [PATCH 6/8] Adopt conventions for local variable naming --- .../java/migrate/util/UseEnumSetOf.java | 69 ++++++++----------- .../java/migrate/util/UseEnumSetOfTest.java | 3 +- 2 files changed, 31 insertions(+), 41 deletions(-) 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 c9f66b248c..75285def26 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java @@ -58,12 +58,12 @@ public TreeVisitor getVisitor() { private static class UseEnumSetOfVisitor extends JavaVisitor { @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation methodInvocation = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext ctx) { + J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(methodInvocation, ctx); - if (SET_OF.matches(method) && - method.getType() instanceof JavaType.Parameterized && - !TypeUtils.isOfClassType(method.getType(), METHOD_TYPE)) { + if (SET_OF.matches(mi) && + mi.getType() instanceof JavaType.Parameterized && + !TypeUtils.isOfClassType(mi.getType(), METHOD_TYPE)) { Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Assignment || is instanceof J.VariableDeclarations || is instanceof J.Block); if (!(parent.getValue() instanceof J.Block)) { JavaType type = parent.getValue() instanceof J.Assignment ? @@ -71,47 +71,38 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu if (isAssignmentSetOfEnum(type)) { maybeAddImport(METHOD_TYPE); - List args = methodInvocation.getArguments(); + List args = mi.getArguments(); if (isArrayParameter(args)) { - return methodInvocation; + return mi; + } + StringJoiner setOf; + if (args.get(0) instanceof J.Empty) { + setOf = new StringJoiner(", ", "EnumSet.noneOf(", ")"); + } else { + setOf = new StringJoiner(", ", "EnumSet.of(", ")"); } - StringJoiner setOf = initStringJoiner(args); args.forEach(o -> setOf.add("#{any()}")); - return createNewMethodInvocation(methodInvocation, (JavaType.Parameterized) type, setOf); + J.MethodInvocation visitMethodInvocation = JavaTemplate.builder(setOf.toString()) + .contextSensitive() + .imports(METHOD_TYPE) + .build() + .apply(updateCursor(mi), mi.getCoordinates().replace(), args.toArray()); + + if (args.get(0) instanceof J.Empty) { + JavaType.Method methodType = mi.getMethodType().withName("noneOf"); + JavaType.Class parameterType = JavaType.ShallowClass.build( + ((JavaType.Parameterized) type).getTypeParameters().get(0).toString()); + return visitMethodInvocation.withMethodType(methodType) + .withArguments(singletonList(new J.Identifier( + Tree.randomId(), Space.EMPTY, mi.getMarkers(), emptyList(), + parameterType.getClassName() + ".class", parameterType, null))); + } + return visitMethodInvocation; } } } - return methodInvocation; - } - - private StringJoiner initStringJoiner(List args) { - if (args.get(0) instanceof J.Empty) { - return new StringJoiner(", ", "EnumSet.noneOf(", ")"); - } - return new StringJoiner(", ", "EnumSet.of(", ")"); - } - - private J.MethodInvocation createNewMethodInvocation(J.MethodInvocation methodInvocation, - JavaType.Parameterized type, - StringJoiner setOf) { - J.MethodInvocation visitMethodInvocation = JavaTemplate.builder(setOf.toString()) - .contextSensitive() - .imports(METHOD_TYPE) - .build() - .apply(updateCursor(methodInvocation), methodInvocation.getCoordinates().replace(), - methodInvocation.getArguments().toArray()); - - if (methodInvocation.getArguments().get(0) instanceof J.Empty) { - JavaType.Method methodType = methodInvocation.getMethodType().withName("noneOf"); - JavaType.Class parameterType = JavaType.ShallowClass.build( - type.getTypeParameters().get(0).toString()); - return visitMethodInvocation.withMethodType(methodType) - .withArguments(singletonList(new J.Identifier( - Tree.randomId(), Space.EMPTY, methodInvocation.getMarkers(), emptyList(), - parameterType.getClassName() + ".class", parameterType, null))); - } - return visitMethodInvocation; + return mi; } private boolean isAssignmentSetOfEnum(@Nullable JavaType type) { 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 40e65c62cf..b37b217ebd 100644 --- a/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/util/UseEnumSetOfTest.java @@ -161,6 +161,7 @@ public void method() { @Test @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/490") void dontHaveArgs() { + //language=java rewriteRun( version( java( @@ -191,6 +192,4 @@ public void method() { ) ); } - - } From 9babc27710c71c21089be1913409cb96ba597b20 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 6 Oct 2024 01:01:33 +0200 Subject: [PATCH 7/8] Use two separate templates for replacement --- .../java/migrate/util/UseEnumSetOf.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) 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 75285def26..0a133f1364 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java @@ -75,30 +75,24 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat if (isArrayParameter(args)) { return mi; } - StringJoiner setOf; + if (args.get(0) instanceof J.Empty) { - setOf = new StringJoiner(", ", "EnumSet.noneOf(", ")"); - } else { - setOf = new StringJoiner(", ", "EnumSet.of(", ")"); + JavaType firstTypeParameter = ((JavaType.Parameterized) type).getTypeParameters().get(0); + JavaType.ShallowClass shallowClass = JavaType.ShallowClass.build(firstTypeParameter.toString()); + return JavaTemplate.builder("EnumSet.noneOf(" + shallowClass.getClassName() + ".class)") + .contextSensitive() + .imports(METHOD_TYPE) + .build() + .apply(updateCursor(mi), mi.getCoordinates().replace()); } - args.forEach(o -> setOf.add("#{any()}")); - J.MethodInvocation visitMethodInvocation = JavaTemplate.builder(setOf.toString()) + StringJoiner setOf = new StringJoiner(", ", "EnumSet.of(", ")"); + args.forEach(o -> setOf.add("#{any()}")); + return JavaTemplate.builder(setOf.toString()) .contextSensitive() .imports(METHOD_TYPE) .build() .apply(updateCursor(mi), mi.getCoordinates().replace(), args.toArray()); - - if (args.get(0) instanceof J.Empty) { - JavaType.Method methodType = mi.getMethodType().withName("noneOf"); - JavaType.Class parameterType = JavaType.ShallowClass.build( - ((JavaType.Parameterized) type).getTypeParameters().get(0).toString()); - return visitMethodInvocation.withMethodType(methodType) - .withArguments(singletonList(new J.Identifier( - Tree.randomId(), Space.EMPTY, mi.getMarkers(), emptyList(), - parameterType.getClassName() + ".class", parameterType, null))); - } - return visitMethodInvocation; } } } From d080b88fb4a03df71893869efe4e5b67318d4d3a Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 6 Oct 2024 01:25:25 +0200 Subject: [PATCH 8/8] Organize imports --- .../openrewrite/java/migrate/util/UseEnumSetOf.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 0a133f1364..d67df418cb 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java @@ -22,15 +22,15 @@ import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesJavaVersion; import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.*; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.TypeUtils; import java.time.Duration; import java.util.List; import java.util.StringJoiner; -import static java.util.Collections.emptyList; -import static java.util.Collections.singletonList; - public class UseEnumSetOf extends Recipe { private static final MethodMatcher SET_OF = new MethodMatcher("java.util.Set of(..)", true); private static final String METHOD_TYPE = "java.util.EnumSet"; @@ -86,7 +86,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat .apply(updateCursor(mi), mi.getCoordinates().replace()); } - StringJoiner setOf = new StringJoiner(", ", "EnumSet.of(", ")"); + StringJoiner setOf = new StringJoiner(", ", "EnumSet.of(", ")"); args.forEach(o -> setOf.add("#{any()}")); return JavaTemplate.builder(setOf.toString()) .contextSensitive()