From 0890b40c95ec07d5e9c86695d9223db221bf8887 Mon Sep 17 00:00:00 2001 From: bramli ahmed khalil Date: Wed, 13 Nov 2024 14:05:24 +0100 Subject: [PATCH 1/5] [HotFix] NoGuavaJava21 causing invalid code since v2.28.0 --- .../guava/AbstractNoGuavaImmutableOf.java | 306 +++---- .../guava/NoGuavaImmutableListOfTest.java | 835 +++++++++--------- .../guava/NoGuavaImmutableMapOfTest.java | 781 ++++++++-------- .../guava/NoGuavaImmutableSetOfTest.java | 84 +- .../java/migrate/guava/NoGuavaJava21Test.java | 27 +- 5 files changed, 932 insertions(+), 1101 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java index 1a3984d32..be252ea6a 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java @@ -17,199 +17,145 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.*; -import org.openrewrite.internal.ListUtils; 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.UsesType; import org.openrewrite.java.tree.*; -import org.openrewrite.marker.Markers; import java.time.Duration; -import java.util.ArrayList; import java.util.List; - -import static java.util.Collections.emptyList; - abstract class AbstractNoGuavaImmutableOf extends Recipe { - private final String guavaType; - private final String javaType; - - AbstractNoGuavaImmutableOf(String guavaType, String javaType) { - this.guavaType = guavaType; - this.javaType = javaType; - } - - private String getShortType(String fullyQualifiedType) { - return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1); - } - - @Override - public String getDisplayName() { - return "Prefer `" + getShortType(javaType) + ".of(..)` in Java 9 or higher"; - } - - @Override - public String getDescription() { - return "Replaces `" + getShortType(guavaType) + ".of(..)` if the returned type is immediately down-cast."; - } - - @Override - public Duration getEstimatedEffortPerOccurrence() { - return Duration.ofMinutes(10); - } - - @Override - public TreeVisitor getVisitor() { - TreeVisitor check = Preconditions.and(new UsesJavaVersion<>(9), - new UsesType<>(guavaType, false)); - final MethodMatcher IMMUTABLE_MATCHER = new MethodMatcher(guavaType + " of(..)"); - return Preconditions.check(check, new JavaVisitor() { - @Override - public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); - if (!IMMUTABLE_MATCHER.matches(mi) || !isParentTypeDownCast(mi)) { - return mi; - } - maybeRemoveImport(guavaType); - maybeAddImport(javaType); - - String template; - Object[] templateArguments; - List methodArguments = mi.getArguments(); - if (methodArguments.isEmpty() || methodArguments.get(0) instanceof J.Empty) { - template = getShortType(javaType) + ".of()"; - templateArguments = new Object[]{}; - } else if ("com.google.common.collect.ImmutableMap".equals(guavaType)) { - template = getShortType(javaType) + ".of(#{any()}, #{any()})"; - templateArguments = new Object[]{methodArguments.get(0), methodArguments.get(1)}; - } else { - template = getShortType(javaType) + ".of(#{any()})"; - templateArguments = new Object[]{methodArguments.get(0)}; - } - - J.MethodInvocation m = JavaTemplate.builder(template) - .imports(javaType) - .build() - .apply(getCursor(), mi.getCoordinates().replace(), templateArguments); - m = m.getPadding().withArguments(mi.getPadding().getArguments()); - JavaType.Method newType = (JavaType.Method) visitType(mi.getMethodType(), ctx); - m = m.withMethodType(newType).withName(m.getName().withType(newType)); - return super.visitMethodInvocation(m, ctx); + private final String guavaType; + private final String javaType; + + AbstractNoGuavaImmutableOf(String guavaType, String javaType) { + this.guavaType = guavaType; + this.javaType = javaType; + } + + private String getShortType(String fullyQualifiedType) { + return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1); + } + + @Override + public String getDisplayName() { + return "Prefer `" + getShortType(javaType) + ".of(..)` in Java 9 or higher"; + } + + @Override + public String getDescription() { + return "Replaces `" + getShortType(guavaType) + ".of(..)` if the returned type is immediately down-cast."; + } + + @Override + public Duration getEstimatedEffortPerOccurrence() { + return Duration.ofMinutes(10); + } + + @Override + public TreeVisitor getVisitor() { + TreeVisitor check = Preconditions.and(new UsesJavaVersion<>(9), + new UsesType<>(guavaType, false)); + final MethodMatcher IMMUTABLE_MATCHER = new MethodMatcher(guavaType + " of(..)"); + return Preconditions.check(check, new JavaVisitor() { + @Override + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); + if (!IMMUTABLE_MATCHER.matches(mi) || !isParentTypeDownCast(mi)) { + return mi; + } + maybeRemoveImport(guavaType); + maybeAddImport(javaType); + + String template; + Object[] templateArguments; + List methodArguments = mi.getArguments(); + if (methodArguments.isEmpty() || methodArguments.get(0) instanceof J.Empty) { + template = getShortType(javaType) + ".of()"; + templateArguments = new Object[]{}; + } else if ("com.google.common.collect.ImmutableMap".equals(guavaType)) { + template = getShortType(javaType) + ".of(#{any()}, #{any()})"; + templateArguments = new Object[]{methodArguments.get(0), methodArguments.get(1)}; + } else { + template = getShortType(javaType) + ".of(#{any()})"; + templateArguments = new Object[]{methodArguments.get(0)}; + } + + J.MethodInvocation m = JavaTemplate.builder(template) + .imports(javaType) + .build() + .apply(getCursor(), mi.getCoordinates().replace(), templateArguments); + m = m.getPadding().withArguments(mi.getPadding().getArguments()); + JavaType.Method newType = (JavaType.Method) visitType(mi.getMethodType(), ctx); + m = m.withMethodType(newType).withName(m.getName().withType(newType)); + return super.visitMethodInvocation(m, ctx); + } + + private boolean isParentTypeDownCast(MethodCall immutableMethod) { + J parent = getCursor().dropParentUntil(J.class::isInstance).getValue(); + boolean isParentTypeDownCast = false; + if (parent instanceof J.VariableDeclarations.NamedVariable) { + isParentTypeDownCast = isParentTypeMatched(((J.VariableDeclarations.NamedVariable) parent).getType()); + } else if (parent instanceof J.Assignment) { + J.Assignment a = (J.Assignment) parent; + if (a.getVariable() instanceof J.Identifier && ((J.Identifier) a.getVariable()).getFieldType() != null) { + isParentTypeDownCast = isParentTypeMatched(((J.Identifier) a.getVariable()).getFieldType().getType()); + } else if (a.getVariable() instanceof J.FieldAccess) { + isParentTypeDownCast = isParentTypeMatched(a.getVariable().getType()); + } + } else if (parent instanceof J.Return) { + // Does not currently support returns in lambda expressions. + J j = getCursor().dropParentUntil(is -> is instanceof J.MethodDeclaration || is instanceof J.CompilationUnit).getValue(); + if (j instanceof J.MethodDeclaration) { + TypeTree returnType = ((J.MethodDeclaration) j).getReturnTypeExpression(); + if (returnType != null) { + isParentTypeDownCast = isParentTypeMatched(returnType.getType()); } - - @Override - public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { - J.VariableDeclarations mv = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx); - - if (multiVariable != mv && TypeUtils.isOfClassType(mv.getType(), guavaType)) { - JavaType newType = JavaType.buildType(javaType); - mv = mv.withTypeExpression(mv.getTypeExpression() == null ? - null : createNewTypeExpression(mv.getTypeExpression(), newType)); - - mv = mv.withVariables(ListUtils.map(mv.getVariables(), variable -> { - JavaType.FullyQualified varType = TypeUtils.asFullyQualified(variable.getType()); - if (varType != null && !varType.equals(newType)) { - return variable.withType(newType).withName(variable.getName().withType(newType)); - } - return variable; - })); - } - - return mv; - } - - private TypeTree createNewTypeExpression(TypeTree typeTree, JavaType newType) { - if (typeTree instanceof J.ParameterizedType) { - J.ParameterizedType parameterizedType = (J.ParameterizedType) typeTree; - List> jRightPaddedList = new ArrayList<>(); - parameterizedType.getTypeParameters().forEach( - expression -> { - if (expression instanceof J.ParameterizedType && TypeUtils.isOfClassType(expression.getType(), guavaType)) { - jRightPaddedList.add(JRightPadded.build(((J.ParameterizedType) createNewTypeExpression((TypeTree) expression, newType)))); - } else { - jRightPaddedList.add(JRightPadded.build(expression)); - } - }); - NameTree clazz = new J.Identifier( - Tree.randomId(), Space.EMPTY, Markers.EMPTY, emptyList(), getShortType(javaType), null, null); - return parameterizedType.withClazz(clazz).withType(newType).getPadding().withTypeParameters(JContainer.build(jRightPaddedList)); - } - return new J.Identifier( - typeTree.getId(), - typeTree.getPrefix(), - Markers.EMPTY, - emptyList(), - getShortType(javaType), - newType, - null - ); + } + } else if (parent instanceof J.MethodInvocation) { + J.MethodInvocation m = (J.MethodInvocation) parent; + int index = m.getArguments().indexOf(immutableMethod); + if (m.getMethodType() != null) { + if(index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) { + isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index)); + } else { + isParentTypeDownCast = !TypeUtils.isOfClassType(m.getMethodType().getReturnType() , guavaType); } - - - private boolean isParentTypeDownCast(MethodCall immutableMethod) { - J parent = getCursor().dropParentUntil(J.class::isInstance).getValue(); - boolean isParentTypeDownCast = false; - if (parent instanceof J.VariableDeclarations.NamedVariable) { - isParentTypeDownCast = isParentTypeMatched(((J.VariableDeclarations.NamedVariable) parent).getType()); - } else if (parent instanceof J.Assignment) { - J.Assignment a = (J.Assignment) parent; - if (a.getVariable() instanceof J.Identifier && ((J.Identifier) a.getVariable()).getFieldType() != null) { - isParentTypeDownCast = isParentTypeMatched(((J.Identifier) a.getVariable()).getFieldType().getType()); - } else if (a.getVariable() instanceof J.FieldAccess) { - isParentTypeDownCast = isParentTypeMatched(a.getVariable().getType()); - } - } else if (parent instanceof J.Return) { - // Does not currently support returns in lambda expressions. - J j = getCursor().dropParentUntil(is -> is instanceof J.MethodDeclaration || is instanceof J.CompilationUnit).getValue(); - if (j instanceof J.MethodDeclaration) { - TypeTree returnType = ((J.MethodDeclaration) j).getReturnTypeExpression(); - if (returnType != null) { - isParentTypeDownCast = isParentTypeMatched(returnType.getType()); - } - } - } else if (parent instanceof J.MethodInvocation) { - J.MethodInvocation m = (J.MethodInvocation) parent; - int index = m.getArguments().indexOf(immutableMethod); - if (m.getMethodType() != null && index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) { - isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index)); - } else { - isParentTypeDownCast = true; - } - } else if (parent instanceof J.NewClass) { - J.NewClass c = (J.NewClass) parent; - int index = 0; - if (c.getConstructorType() != null) { - for (Expression argument : c.getArguments()) { - if (IMMUTABLE_MATCHER.matches(argument)) { - break; - } - index++; - } - if (c.getConstructorType() != null) { - isParentTypeDownCast = isParentTypeMatched(c.getConstructorType().getParameterTypes().get(index)); - } - } - } else if (parent instanceof J.NewArray) { - J.NewArray a = (J.NewArray) parent; - JavaType arrayType = a.getType(); - while (arrayType instanceof JavaType.Array) { - arrayType = ((JavaType.Array) arrayType).getElemType(); - } - - isParentTypeDownCast = isParentTypeMatched(arrayType); - } - return isParentTypeDownCast; + } + } else if (parent instanceof J.NewClass) { + J.NewClass c = (J.NewClass) parent; + int index = 0; + if (c.getConstructorType() != null) { + for (Expression argument : c.getArguments()) { + if (IMMUTABLE_MATCHER.matches(argument)) { + break; + } + index++; } - - private boolean isParentTypeMatched(@Nullable JavaType type) { - JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type); - return TypeUtils.isOfClassType(fq, javaType) || - TypeUtils.isOfClassType(fq, "java.lang.Object") || - TypeUtils.isOfClassType(fq, guavaType); + if (c.getConstructorType() != null) { + isParentTypeDownCast = isParentTypeMatched(c.getConstructorType().getParameterTypes().get(index)); } - }); - } + } + } else if (parent instanceof J.NewArray) { + J.NewArray a = (J.NewArray) parent; + JavaType arrayType = a.getType(); + while (arrayType instanceof JavaType.Array) { + arrayType = ((JavaType.Array) arrayType).getElemType(); + } + + isParentTypeDownCast = isParentTypeMatched(arrayType); + } + return isParentTypeDownCast; + } + + private boolean isParentTypeMatched(@Nullable JavaType type) { + JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type); + return TypeUtils.isOfClassType(fq, javaType) || + TypeUtils.isOfClassType(fq, "java.lang.Object"); + } + }); + } } diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java index 98b286fbb..bcd78c042 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java @@ -28,25 +28,27 @@ class NoGuavaImmutableListOfTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec - .recipe(new NoGuavaImmutableListOf()) - .parser(JavaParser.fromJavaVersion().classpath("guava")); + .recipe(new NoGuavaImmutableListOf()) + .parser(JavaParser.fromJavaVersion().classpath("guava")); } @Test void doNotChangeReturnsImmutableList() { rewriteRun( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - ImmutableList getList() { - return ImmutableList.of(); - } - } - """ - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + ImmutableList getList() { + return ImmutableList.of(); + } + } + """ + ) + ,9) ); } @@ -54,40 +56,44 @@ ImmutableList getList() { @Test void doNotChangeFieldAssignmentToImmutableList() { rewriteRun( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - ImmutableList m; - - { - this.m = ImmutableList.of(); - } - } - """ - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + ImmutableList m; + + { + this.m = ImmutableList.of(); + } + } + """ + ) + ,9) ); } @Test void doNotChangeAssignsToImmutableList() { rewriteRun( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - ImmutableList m; - - void init() { - m = ImmutableList.of(); - } - } - """ - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + ImmutableList m; + + void init() { + m = ImmutableList.of(); + } + } + """ + ) + ,9) ); } @@ -95,181 +101,185 @@ void init() { void doNotChangeNewClass() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableList; - - public class A { - ImmutableList immutableList; - public A(ImmutableList immutableList) { - this.immutableList = immutableList; + java( + """ + import com.google.common.collect.ImmutableList; + + public class A { + ImmutableList immutableList; + public A(ImmutableList immutableList) { + this.immutableList = immutableList; + } } - } - """ - ), - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - A a = new A(ImmutableList.of()); - } - """) + """ + ), + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + A a = new A(ImmutableList.of()); + } + """) + ,9) ); } @Test void doNotChangeMethodInvocation() { rewriteRun( - spec -> spec.parser( - JavaParser.fromJavaVersion() - .classpath("guava") - .dependsOn( + spec -> spec.parser( + JavaParser.fromJavaVersion() + .classpath("guava") + .dependsOn( + //language=java + """ + import com.google.common.collect.ImmutableList; + + public class A { + ImmutableList immutableList; + public void method(ImmutableList immutableList) { + this.immutableList = immutableList; + } + } + """ + ) + ), + version( //language=java - """ - import com.google.common.collect.ImmutableList; - - public class A { - ImmutableList immutableList; - public void method(ImmutableList immutableList) { - this.immutableList = immutableList; + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + void method() { + A a = new A(); + a.method(ImmutableList.of()); + } } - } - """ - ) - ), - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableList.of()); - } - } - """ - ) + """ + ) + ,9) ); } @Test void replaceArguments() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; - - class Test { - List m = ImmutableList.of("A", "B", "C", "D"); - } - """, - """ - import java.util.List; - - class Test { - List m = List.of("A", "B", "C", "D"); - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; + + class Test { + List m = ImmutableList.of("A", "B", "C", "D"); + } + """, + """ + import java.util.List; + + class Test { + List m = List.of("A", "B", "C", "D"); + } + """ + ), + 9 + ) ); } @Test void fieldAssignmentToList() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; - - class Test { - List m; - { - this.m = ImmutableList.of(); - } - } - """, - """ - import java.util.List; - - class Test { - List m; - { - this.m = List.of(); - } - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; + + class Test { + List m; + { + this.m = ImmutableList.of(); + } + } + """, + """ + import java.util.List; + + class Test { + List m; + { + this.m = List.of(); + } + } + """ + ), + 9 + ) ); } @Test void assignmentToList() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; - - class Test { - List m = ImmutableList.of(); - } - """, - """ - import java.util.List; - - class Test { - List m = List.of(); - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; + + class Test { + List m = ImmutableList.of(); + } + """, + """ + import java.util.List; + + class Test { + List m = List.of(); + } + """ + ), + 9 + ) ); } @Test void returnsList() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; - - class Test { - List list() { - return ImmutableList.of(); - } - } - """, - """ - import java.util.List; - - class Test { - List list() { - return List.of(); - } - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; + + class Test { + List list() { + return ImmutableList.of(); + } + } + """, + """ + import java.util.List; + + class Test { + List list() { + return List.of(); + } + } + """ + ), + 9 + ) ); } @@ -277,38 +287,38 @@ List list() { void newClassWithListArgument() { //language=java rewriteRun( - java( - """ - import java.util.List; - - public class A { - List list; - public A(List list) { - this.list = list; - } - } - """ - ), - version( - //language=java java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - A a = new A(ImmutableList.of()); - } - """, - """ + """ import java.util.List; - - class Test { - A a = new A(List.of()); + + public class A { + List list; + public A(List list) { + this.list = list; + } } - """ + """ ), - 11 - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + A a = new A(ImmutableList.of()); + } + """, + """ + import java.util.List; + + class Test { + A a = new A(List.of()); + } + """ + ), + 11 + ) ); } @@ -316,44 +326,44 @@ class Test { void doChangeMethodInvocationWithSelect() { //language=java rewriteRun( - java( - """ - import java.util.List; - - public class A { - Object[] list; - public void method(Object[] list ) { - this.list = list; - } - } - """ - ), - version( - //language=java java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableList.of().toArray()); - } - } - """, - """ - import java.util.List; - - class Test { - void method() { - A a = new A(); - a.method(List.of().toArray()); - } - } """ + import java.util.List; + + public class A { + Object[] list; + public void method(Object[] list ) { + this.list = list; + } + } + """ ), - 11 - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + void method() { + A a = new A(); + a.method(ImmutableList.of().toArray()); + } + } + """, + """ + import java.util.List; + + class Test { + void method() { + A a = new A(); + a.method(List.of().toArray()); + } + } + """ + ), + 11 + ) ); } @@ -361,44 +371,44 @@ void method() { void methodInvocationWithListArgument() { //language=java rewriteRun( - java( - """ - import java.util.List; - - public class A { - List list; - public void method(List list) { - this.list = list; - } - } - """ - ), - version( - //language=java java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableList.of()); - } - } - """, - """ - import java.util.List; - - class Test { - void method() { - A a = new A(); - a.method(List.of()); - } - } """ + import java.util.List; + + public class A { + List list; + public void method(List list) { + this.list = list; + } + } + """ ), - 11 - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + void method() { + A a = new A(); + a.method(ImmutableList.of()); + } + } + """, + """ + import java.util.List; + + class Test { + void method() { + A a = new A(); + a.method(List.of()); + } + } + """ + ), + 11 + ) ); } @@ -406,31 +416,31 @@ void method() { @Test void listOfInts() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; - - class Test { - List list() { - return ImmutableList.of(1, 2, 3); - } - } - """, - """ - import java.util.List; - - class Test { - List list() { - return List.of(1, 2, 3); - } - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; + + class Test { + List list() { + return ImmutableList.of(1, 2, 3); + } + } + """, + """ + import java.util.List; + + class Test { + List list() { + return List.of(1, 2, 3); + } + } + """ + ), + 9 + ) ); } @@ -438,30 +448,30 @@ List list() { @Test void insideAnonymousArrayInitializer() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class A { - Object[] o = new Object[] { - ImmutableList.of(1, 2, 3) - }; - } - """, - """ - import java.util.List; - - class A { - Object[] o = new Object[] { - List.of(1, 2, 3) - }; - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class A { + Object[] o = new Object[] { + ImmutableList.of(1, 2, 3) + }; + } + """, + """ + import java.util.List; + + class A { + Object[] o = new Object[] { + List.of(1, 2, 3) + }; + } + """ + ), + 9 + ) ); } @@ -469,26 +479,26 @@ class A { @Test void assignToMoreGeneralType() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class A { - Object o = ImmutableList.of(1, 2, 3); - } - """, - """ - import java.util.List; - - class A { - Object o = List.of(1, 2, 3); - } - """ - ), - 11 - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class A { + Object o = ImmutableList.of(1, 2, 3); + } + """, + """ + import java.util.List; + + class A { + Object o = List.of(1, 2, 3); + } + """ + ), + 11 + ) ); } @@ -497,107 +507,66 @@ class A { void doChangeAssignToImmutableMap() { //language=java rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableList; - import java.util.List; - - class A { - Object o = List.of(ImmutableList.of(1, 2), ImmutableList.of(2, 3)); - } - """, - """ - import java.util.List; - - class A { - Object o = List.of(List.of(1, 2), List.of(2, 3)); - } - """ - ), - 9 - ) - ); - } + version( + java( + """ + import com.google.common.collect.ImmutableList; + import java.util.List; - @Test - void doChangeNestedListsWithVariableType() { - //language=java - rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableList; - - class A { - ImmutableList> o = ImmutableList.of(ImmutableList.of(1, 2), ImmutableList.of(2, 3)); - } - """, - """ - import java.util.List; - - class A { - List> o = List.of(List.of(1, 2), List.of(2, 3)); - } - """ - ), - 9 - ) + class A { + Object o = List.of(ImmutableList.of(1, 2), ImmutableList.of(2, 3)); + } + """ + ), + 9 + ) ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/489") @Test - void doChangeAssignFromImmutableListToList() { + void doChangeWithStreamOperation() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class A { - void test() { - ImmutableList o = ImmutableList.of(1, 2); - } - } - """, - """ - import java.util.List; - - class A { - void test() { - List o = List.of(1, 2); - } - } - """ - ), - 11 - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class A { + void test() { + final List list = ImmutableList.of(1, 2).stream().toList(); + } + } + """, + """ + import java.util.List; + + class A { + void test() { + final List list = List.of(1, 2).stream().toList(); + } + } + """ + ), + 11 + ) ); } @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/489") @Test - void doChangeWithStreamOperation() { + void doNotChangeWithImmutableListMethod() { rewriteRun( version( //language=java java( """ import com.google.common.collect.ImmutableList; - - class A { - void test() { - final List list = ImmutableList.of(1, 2).stream().toList(); - } - } - """, - """ - import java.util.List; - + class A { void test() { - final List list = List.of(1, 2).stream().toList(); + ImmutableList.of(1, 2).asList(); } } """ diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java index 0c3af4c26..b975f951e 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java @@ -28,45 +28,45 @@ class NoGuavaImmutableMapOfTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec - .recipe(new NoGuavaImmutableMapOf()) - .parser(JavaParser.fromJavaVersion().classpath("guava")); + .recipe(new NoGuavaImmutableMapOf()) + .parser(JavaParser.fromJavaVersion().classpath("guava")); } @Test void doNotChangeReturnsImmutableMap() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - ImmutableMap getMap() { - return ImmutableMap.of(); + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + ImmutableMap getMap() { + return ImmutableMap.of(); + } } - } - """ - ) + """ + ) ); } @Test void doNotChangeFieldAssignmentToImmutableMap() { rewriteRun( - //language=java - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - ImmutableMap m; - - { - this.m = ImmutableMap.of(); + //language=java + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + ImmutableMap m; + + { + this.m = ImmutableMap.of(); + } } - } - """ - ) + """ + ) ); } @@ -74,19 +74,19 @@ class Test { void doNotChangeAssignsToImmutableMap() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - ImmutableMap m; - - void init() { - m = ImmutableMap.of(); + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + ImmutableMap m; + + void init() { + m = ImmutableMap.of(); + } } - } - """ - ) + """ + ) ); } @@ -94,27 +94,27 @@ void init() { void doNotChangeNewClass() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableMap; - - public class A { - ImmutableMap immutableMap; - public A(ImmutableMap immutableMap) { - this.immutableMap = immutableMap; + java( + """ + import com.google.common.collect.ImmutableMap; + + public class A { + ImmutableMap immutableMap; + public A(ImmutableMap immutableMap) { + this.immutableMap = immutableMap; + } + } + """ + ), + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + A a = new A(ImmutableMap.of()); } - } - """ - ), - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - A a = new A(ImmutableMap.of()); - } - """ - ) + """ + ) ); } @@ -122,30 +122,30 @@ class Test { void doNotChangeMethodInvocation() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableMap; - - public class A { - ImmutableMap immutableMap; - public void method(ImmutableMap immutableMap) { - this.immutableMap = immutableMap; + java( + """ + import com.google.common.collect.ImmutableMap; + + public class A { + ImmutableMap immutableMap; + public void method(ImmutableMap immutableMap) { + this.immutableMap = immutableMap; + } } - } - """ - ), - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableMap.of()); + """ + ), + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + void method() { + A a = new A(); + a.method(ImmutableMap.of()); + } } - } - """ - ) + """ + ) ); } @@ -153,26 +153,26 @@ void method() { void replaceArguments() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map m = ImmutableMap.of("A", "B", "C", "D"); - } - """, - """ - import java.util.Map; - - class Test { - Map m = Map.of("A", "B", "C", "D"); - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map m = ImmutableMap.of("A", "B", "C", "D"); + } + """, + """ + import java.util.Map; + + class Test { + Map m = Map.of("A", "B", "C", "D"); + } + """ + ), + 9 + ) ); } @@ -180,32 +180,32 @@ class Test { void fieldAssignmentToMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map m; - { - this.m = ImmutableMap.of(); - } - } - """, - """ - import java.util.Map; - - class Test { - Map m; - { - this.m = Map.of(); - } - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map m; + { + this.m = ImmutableMap.of(); + } + } + """, + """ + import java.util.Map; + + class Test { + Map m; + { + this.m = Map.of(); + } + } + """ + ), + 9 + ) ); } @@ -213,26 +213,26 @@ class Test { void assignmentToMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map m = ImmutableMap.of(); - } - """, - """ - import java.util.Map; - - class Test { - Map m = Map.of(); - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map m = ImmutableMap.of(); + } + """, + """ + import java.util.Map; + + class Test { + Map m = Map.of(); + } + """ + ), + 9 + ) ); } @@ -240,30 +240,30 @@ class Test { void returnsMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map map() { - return ImmutableMap.of(); - } - } - """, - """ - import java.util.Map; - - class Test { - Map map() { - return Map.of(); - } - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map map() { + return ImmutableMap.of(); + } + } + """, + """ + import java.util.Map; + + class Test { + Map map() { + return Map.of(); + } + } + """ + ), + 9 + ) ); } @@ -272,30 +272,30 @@ Map map() { void mapOfInts() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map map() { - return ImmutableMap.of(1, 1, 2, 2, 3, 3); - } - } - """, - """ - import java.util.Map; - - class Test { - Map map() { - return Map.of(1, 1, 2, 2, 3, 3); - } - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map map() { + return ImmutableMap.of(1, 1, 2, 2, 3, 3); + } + } + """, + """ + import java.util.Map; + + class Test { + Map map() { + return Map.of(1, 1, 2, 2, 3, 3); + } + } + """ + ), + 9 + ) ); } @@ -303,37 +303,37 @@ Map map() { void newClassWithMapArgument() { //language=java rewriteRun( - java( - """ - import java.util.Map; - - public class A { - Map map; - public A(Map map) { - this.map = map; - } - } - """ - ), - version( java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - A a = new A(ImmutableMap.of()); - } - """, - """ - import java.util.Map; - - class Test { - A a = new A(Map.of()); - } """ + import java.util.Map; + + public class A { + Map map; + public A(Map map) { + this.map = map; + } + } + """ ), - 9 - ) + version( + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + A a = new A(ImmutableMap.of()); + } + """, + """ + import java.util.Map; + + class Test { + A a = new A(Map.of()); + } + """ + ), + 9 + ) ); } @@ -341,43 +341,43 @@ class Test { void methodInvocationWithMapArgument() { //language=java rewriteRun( - java( - """ - import java.util.Map; - - public class A { - Map map; - public void method(Map map) { - this.map = map; - } - } - """ - ), - version( java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableMap.of()); - } - } - """, - """ - import java.util.Map; - - class Test { - void method() { - A a = new A(); - a.method(Map.of()); - } - } """ + import java.util.Map; + + public class A { + Map map; + public void method(Map map) { + this.map = map; + } + } + """ ), - 9 - ) + version( + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + void method() { + A a = new A(); + a.method(ImmutableMap.of()); + } + } + """, + """ + import java.util.Map; + + class Test { + void method() { + A a = new A(); + a.method(Map.of()); + } + } + """ + ), + 9 + ) ); } @@ -385,38 +385,38 @@ void method() { void variableIsMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.HashMap; - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map> map = new HashMap<>(); - void setMap(String value) { - for (int i = 0; i < 10; i++) { - map.getOrDefault(i, ImmutableMap.of()); - } - } - } - """, - """ - import java.util.HashMap; - import java.util.Map; - - class Test { - Map> map = new HashMap<>(); - void setMap(String value) { - for (int i = 0; i < 10; i++) { - map.getOrDefault(i, Map.of()); - } - } - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.HashMap; + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map> map = new HashMap<>(); + void setMap(String value) { + for (int i = 0; i < 10; i++) { + map.getOrDefault(i, ImmutableMap.of()); + } + } + } + """, + """ + import java.util.HashMap; + import java.util.Map; + + class Test { + Map> map = new HashMap<>(); + void setMap(String value) { + for (int i = 0; i < 10; i++) { + map.getOrDefault(i, Map.of()); + } + } + } + """ + ), + 9 + ) ); } @@ -425,29 +425,29 @@ void setMap(String value) { void insideAnonymousArrayInitializer() { //language=java rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableMap; - - class A { - Object[] o = new Object[] { - ImmutableMap.of(1, 1, 2, 2, 3, 3) - }; - } - """, - """ - import java.util.Map; - - class A { - Object[] o = new Object[] { - Map.of(1, 1, 2, 2, 3, 3) - }; - } - """ - ), - 11 - ) + version( + java( + """ + import com.google.common.collect.ImmutableMap; + + class A { + Object[] o = new Object[] { + ImmutableMap.of(1, 1, 2, 2, 3, 3) + }; + } + """, + """ + import java.util.Map; + + class A { + Object[] o = new Object[] { + Map.of(1, 1, 2, 2, 3, 3) + }; + } + """ + ), + 11 + ) ); } @@ -456,138 +456,67 @@ class A { void assignToMoreGeneralType() { //language=java rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableMap; - - class A { - Object o = ImmutableMap.of(1, 1, 2, 2, 3, 3); - } - """, - """ - import java.util.Map; - - class A { - Object o = Map.of(1, 1, 2, 2, 3, 3); - } - """ - ), - 11 - ) + version( + java( + """ + import com.google.common.collect.ImmutableMap; + + class A { + Object o = ImmutableMap.of(1, 1, 2, 2, 3, 3); + } + """, + """ + import java.util.Map; + + class A { + Object o = Map.of(1, 1, 2, 2, 3, 3); + } + """ + ), + 11 + ) ); } @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/256") @Test - void doChangeNestedMaps() { + void doNotChangeNestedMaps() { //language=java rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableMap; - import java.util.Map; - - class A { - Object o = Map.of(1, ImmutableMap.of(2, 3)); - } - """, - """ - import java.util.Map; - - class A { - Object o = Map.of(1, Map.of(2, 3)); - } - """ - ), - 11 - ) - ); - } + version( + java( + """ + import com.google.common.collect.ImmutableMap; + import java.util.Map; - @Test - void doChangeNestedMaps2() { - //language=java - rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableMap; - - class A { - Object o = ImmutableMap.of(1, ImmutableMap.of(2, 3)); - } - """, - """ - import java.util.Map; - - class A { - Object o = Map.of(1, Map.of(2, 3)); - } - """ - ), - 11 - ) + class A { + Object o = Map.of(1, ImmutableMap.of(2, 3)); + } + """ + ), + 11 + ) ); } @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/256") @Test - void doChangeAssignToImmutableMap() { + void doNotChangeAssignToImmutableMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - ImmutableMap m = ImmutableMap.of(); - } - """, - """ - import java.util.Map; - - class Test { - Map m = Map.of(); - } - """ - ), - 9 - ) - ); - } + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; - @Test - void doChangeNestedAssignReturnType() { - rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableMap; - - class A { - public void getMap() { - ImmutableMap> s = ImmutableMap.of("key", ImmutableMap.of("value1", "value2")); - } - } - """, - """ - import java.util.Map; - - class A { - public void getMap() { - Map> s = Map.of("key", Map.of("value1", "value2")); - } - } - """ - ), - 9 - ) + class Test { + ImmutableMap m = ImmutableMap.of(); + } + """ + ), + 9 + ) ); } - } diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOfTest.java index d86874126..ad1acec03 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOfTest.java @@ -39,7 +39,7 @@ void doNotChangeReturnsImmutableSet() { java( """ import com.google.common.collect.ImmutableSet; - + class Test { ImmutableSet getSet() { return ImmutableSet.of(); @@ -57,10 +57,10 @@ void doNotChangeFieldAssignmentToImmutableSet() { java( """ import com.google.common.collect.ImmutableSet; - + class Test { ImmutableSet m; - + { this.m = ImmutableSet.of(); } @@ -77,10 +77,10 @@ void doNotChangeAssignsToImmutableSet() { java( """ import com.google.common.collect.ImmutableSet; - + class Test { ImmutableSet m; - + void init() { m = ImmutableSet.of(); } @@ -100,7 +100,7 @@ void doNotChangeNewClass() { //language=java """ import com.google.common.collect.ImmutableSet; - + public class A { ImmutableSet immutableSet; public A(ImmutableSet immutableSet) { @@ -113,7 +113,7 @@ public A(ImmutableSet immutableSet) { java( """ import com.google.common.collect.ImmutableSet; - + class Test { A a = new A(ImmutableSet.of()); } @@ -132,7 +132,7 @@ void doNotChangeMethodInvocation() { //language=java """ import com.google.common.collect.ImmutableSet; - + public class A { ImmutableSet immutableSet; public void method(ImmutableSet immutableSet) { @@ -145,7 +145,7 @@ public void method(ImmutableSet immutableSet) { java( """ import com.google.common.collect.ImmutableSet; - + class Test { void method() { A a = new A(); @@ -167,14 +167,14 @@ void replaceArguments() { """ import java.util.Set; import com.google.common.collect.ImmutableSet; - + class Test { Set m = ImmutableSet.of("A", "B", "C", "D"); } """, """ import java.util.Set; - + class Test { Set m = Set.of("A", "B", "C", "D"); } @@ -194,7 +194,7 @@ void fieldAssignmentToSet() { """ import java.util.Set; import com.google.common.collect.ImmutableSet; - + class Test { Set m; { @@ -204,7 +204,7 @@ class Test { """, """ import java.util.Set; - + class Test { Set m; { @@ -227,14 +227,14 @@ void assignmentToSet() { """ import java.util.Set; import com.google.common.collect.ImmutableSet; - + class Test { Set m = ImmutableSet.of(); } """, """ import java.util.Set; - + class Test { Set m = Set.of(); } @@ -254,7 +254,7 @@ void returnsSet() { """ import java.util.Set; import com.google.common.collect.ImmutableSet; - + class Test { Set set() { return ImmutableSet.of(); @@ -263,7 +263,7 @@ Set set() { """, """ import java.util.Set; - + class Test { Set set() { return Set.of(); @@ -286,7 +286,7 @@ void setOfInts() { """ import java.util.Set; import com.google.common.collect.ImmutableSet; - + class Test { Set set() { return ImmutableSet.of(1, 2, 3); @@ -295,7 +295,7 @@ Set set() { """, """ import java.util.Set; - + class Test { Set set() { return Set.of(1, 2, 3); @@ -315,7 +315,7 @@ void newClassWithSetArgument() { java( """ import java.util.Set; - + public class A { Set set; public A(Set set) { @@ -329,14 +329,14 @@ public A(Set set) { java( """ import com.google.common.collect.ImmutableSet; - + class Test { A a = new A(ImmutableSet.of()); } """, """ import java.util.Set; - + class Test { A a = new A(Set.of()); } @@ -354,7 +354,7 @@ void methodInvocationWithSetArgument() { java( """ import java.util.Set; - + public class A { Set set; public void method(Set set) { @@ -367,7 +367,7 @@ public void method(Set set) { java( """ import com.google.common.collect.ImmutableSet; - + class Test { void method() { A a = new A(); @@ -377,7 +377,7 @@ void method() { """, """ import java.util.Set; - + class Test { void method() { A a = new A(); @@ -400,7 +400,7 @@ void insideAnonymousArrayInitializer() { java( """ import com.google.common.collect.ImmutableSet; - + class A { Object[] o = new Object[] { ImmutableSet.of(1, 2, 3) @@ -409,7 +409,7 @@ class A { """, """ import java.util.Set; - + class A { Object[] o = new Object[] { Set.of(1, 2, 3) @@ -431,14 +431,14 @@ void assignToMoreGeneralType() { java( """ import com.google.common.collect.ImmutableSet; - + class A { Object o = ImmutableSet.of(1, 2, 3); } """, """ import java.util.Set; - + class A { Object o = Set.of(1, 2, 3); } @@ -451,7 +451,7 @@ class A { @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/256") @Test - void doChangeNestedSets() { + void doNotChangeNestedSets() { //language=java rewriteRun( version( @@ -459,17 +459,10 @@ void doChangeNestedSets() { """ import com.google.common.collect.ImmutableSet; import java.util.Set; - + class A { Object o = Set.of(ImmutableSet.of(1, 2)); } - """, - """ - import java.util.Set; - - class A { - Object o = Set.of(Set.of(1, 2)); - } """ ), 9 @@ -479,24 +472,17 @@ class A { @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/256") @Test - void doChangeAssignToImmutableSet() { + void doNotChangeAssignToImmutableSet() { //language=java rewriteRun( spec -> spec.allSources(all -> all.markers(javaVersion(9))), java( """ import com.google.common.collect.ImmutableSet; - + class Test { ImmutableSet m = ImmutableSet.of(); } - """, - """ - import java.util.Set; - - class Test { - Set m = Set.of(); - } """ ) ); @@ -511,7 +497,7 @@ void multiLine() { """ import com.google.common.collect.ImmutableSet; import java.util.Set; - + class Test { Set m = ImmutableSet.of( "foo", @@ -521,7 +507,7 @@ class Test { """, """ import java.util.Set; - + class Test { Set m = Set.of( "foo", diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaJava21Test.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaJava21Test.java index b49fac08f..a2a7359b7 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaJava21Test.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaJava21Test.java @@ -43,16 +43,16 @@ void preferMathClampForDouble() { rewriteRun( version( java( - """ + """ import com.google.common.primitives.Doubles; - + class Test { public double testMethod() { return Doubles.constrainToRange(20D, 10D, 100D); } } """, - """ + """ class Test { public double testMethod() { return Math.clamp(20D, 10D, 100D); @@ -70,16 +70,16 @@ void preferMathClampForLongs() { rewriteRun( version( java( - """ + """ import com.google.common.primitives.Longs; - + class Test { public long testMethod() { return Longs.constrainToRange(20L, 10L, 100L); } } """, - """ + """ class Test { public long testMethod() { return Math.clamp(20L, 10L, 100L); @@ -97,16 +97,16 @@ void preferMathClampForFloats() { rewriteRun( version( java( - """ + """ import com.google.common.primitives.Floats; - + class Test { public float testMethod() { return Floats.constrainToRange(20F, 10F, 100F); } } """, - """ + """ class Test { public float testMethod() { return Math.clamp(20F, 10F, 100F); @@ -128,7 +128,7 @@ void noGuavaImmutableOfException() { """ import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableMap; - + class A { public Object getMap() { return ImmutableMap.of("key", ImmutableSet.of("value1", "value2")); @@ -136,12 +136,13 @@ public Object getMap() { } """, """ + import com.google.common.collect.ImmutableSet; + import java.util.Map; - import java.util.Set; - + class A { public Object getMap() { - return Map.of("key", Set.of("value1", "value2")); + return Map.of("key", ImmutableSet.of("value1", "value2")); } } """ From 85ecc14587cc46e3a83de487e89e97f5842a1f7b Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 13 Nov 2024 14:27:29 +0100 Subject: [PATCH 2/5] Apply formatter to minimize diff --- .../guava/AbstractNoGuavaImmutableOf.java | 244 +++--- .../guava/NoGuavaImmutableListOfTest.java | 770 +++++++++--------- .../guava/NoGuavaImmutableMapOfTest.java | 702 ++++++++-------- 3 files changed, 860 insertions(+), 856 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java index be252ea6a..2e5f16b86 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java @@ -16,7 +16,10 @@ package org.openrewrite.java.migrate.guava; import org.jspecify.annotations.Nullable; -import org.openrewrite.*; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; @@ -26,136 +29,137 @@ import java.time.Duration; import java.util.List; -abstract class AbstractNoGuavaImmutableOf extends Recipe { - private final String guavaType; - private final String javaType; +abstract class AbstractNoGuavaImmutableOf extends Recipe { - AbstractNoGuavaImmutableOf(String guavaType, String javaType) { - this.guavaType = guavaType; - this.javaType = javaType; - } + private final String guavaType; + private final String javaType; - private String getShortType(String fullyQualifiedType) { - return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1); - } + AbstractNoGuavaImmutableOf(String guavaType, String javaType) { + this.guavaType = guavaType; + this.javaType = javaType; + } - @Override - public String getDisplayName() { - return "Prefer `" + getShortType(javaType) + ".of(..)` in Java 9 or higher"; - } + private String getShortType(String fullyQualifiedType) { + return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1); + } - @Override - public String getDescription() { - return "Replaces `" + getShortType(guavaType) + ".of(..)` if the returned type is immediately down-cast."; - } + @Override + public String getDisplayName() { + return "Prefer `" + getShortType(javaType) + ".of(..)` in Java 9 or higher"; + } - @Override - public Duration getEstimatedEffortPerOccurrence() { - return Duration.ofMinutes(10); - } + @Override + public String getDescription() { + return "Replaces `" + getShortType(guavaType) + ".of(..)` if the returned type is immediately down-cast."; + } - @Override - public TreeVisitor getVisitor() { - TreeVisitor check = Preconditions.and(new UsesJavaVersion<>(9), - new UsesType<>(guavaType, false)); - final MethodMatcher IMMUTABLE_MATCHER = new MethodMatcher(guavaType + " of(..)"); - return Preconditions.check(check, new JavaVisitor() { - @Override - public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); - if (!IMMUTABLE_MATCHER.matches(mi) || !isParentTypeDownCast(mi)) { - return mi; - } - maybeRemoveImport(guavaType); - maybeAddImport(javaType); + @Override + public Duration getEstimatedEffortPerOccurrence() { + return Duration.ofMinutes(10); + } - String template; - Object[] templateArguments; - List methodArguments = mi.getArguments(); - if (methodArguments.isEmpty() || methodArguments.get(0) instanceof J.Empty) { - template = getShortType(javaType) + ".of()"; - templateArguments = new Object[]{}; - } else if ("com.google.common.collect.ImmutableMap".equals(guavaType)) { - template = getShortType(javaType) + ".of(#{any()}, #{any()})"; - templateArguments = new Object[]{methodArguments.get(0), methodArguments.get(1)}; - } else { - template = getShortType(javaType) + ".of(#{any()})"; - templateArguments = new Object[]{methodArguments.get(0)}; - } + @Override + public TreeVisitor getVisitor() { + TreeVisitor check = Preconditions.and(new UsesJavaVersion<>(9), + new UsesType<>(guavaType, false)); + final MethodMatcher IMMUTABLE_MATCHER = new MethodMatcher(guavaType + " of(..)"); + return Preconditions.check(check, new JavaVisitor() { + @Override + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); + if (!IMMUTABLE_MATCHER.matches(mi) || !isParentTypeDownCast(mi)) { + return mi; + } + maybeRemoveImport(guavaType); + maybeAddImport(javaType); - J.MethodInvocation m = JavaTemplate.builder(template) - .imports(javaType) - .build() - .apply(getCursor(), mi.getCoordinates().replace(), templateArguments); - m = m.getPadding().withArguments(mi.getPadding().getArguments()); - JavaType.Method newType = (JavaType.Method) visitType(mi.getMethodType(), ctx); - m = m.withMethodType(newType).withName(m.getName().withType(newType)); - return super.visitMethodInvocation(m, ctx); - } + String template; + Object[] templateArguments; + List methodArguments = mi.getArguments(); + if (methodArguments.isEmpty() || methodArguments.get(0) instanceof J.Empty) { + template = getShortType(javaType) + ".of()"; + templateArguments = new Object[]{}; + } else if ("com.google.common.collect.ImmutableMap".equals(guavaType)) { + template = getShortType(javaType) + ".of(#{any()}, #{any()})"; + templateArguments = new Object[]{methodArguments.get(0), methodArguments.get(1)}; + } else { + template = getShortType(javaType) + ".of(#{any()})"; + templateArguments = new Object[]{methodArguments.get(0)}; + } - private boolean isParentTypeDownCast(MethodCall immutableMethod) { - J parent = getCursor().dropParentUntil(J.class::isInstance).getValue(); - boolean isParentTypeDownCast = false; - if (parent instanceof J.VariableDeclarations.NamedVariable) { - isParentTypeDownCast = isParentTypeMatched(((J.VariableDeclarations.NamedVariable) parent).getType()); - } else if (parent instanceof J.Assignment) { - J.Assignment a = (J.Assignment) parent; - if (a.getVariable() instanceof J.Identifier && ((J.Identifier) a.getVariable()).getFieldType() != null) { - isParentTypeDownCast = isParentTypeMatched(((J.Identifier) a.getVariable()).getFieldType().getType()); - } else if (a.getVariable() instanceof J.FieldAccess) { - isParentTypeDownCast = isParentTypeMatched(a.getVariable().getType()); - } - } else if (parent instanceof J.Return) { - // Does not currently support returns in lambda expressions. - J j = getCursor().dropParentUntil(is -> is instanceof J.MethodDeclaration || is instanceof J.CompilationUnit).getValue(); - if (j instanceof J.MethodDeclaration) { - TypeTree returnType = ((J.MethodDeclaration) j).getReturnTypeExpression(); - if (returnType != null) { - isParentTypeDownCast = isParentTypeMatched(returnType.getType()); - } - } - } else if (parent instanceof J.MethodInvocation) { - J.MethodInvocation m = (J.MethodInvocation) parent; - int index = m.getArguments().indexOf(immutableMethod); - if (m.getMethodType() != null) { - if(index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) { - isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index)); - } else { - isParentTypeDownCast = !TypeUtils.isOfClassType(m.getMethodType().getReturnType() , guavaType); - } - } - } else if (parent instanceof J.NewClass) { - J.NewClass c = (J.NewClass) parent; - int index = 0; - if (c.getConstructorType() != null) { - for (Expression argument : c.getArguments()) { - if (IMMUTABLE_MATCHER.matches(argument)) { - break; - } - index++; + J.MethodInvocation m = JavaTemplate.builder(template) + .imports(javaType) + .build() + .apply(getCursor(), mi.getCoordinates().replace(), templateArguments); + m = m.getPadding().withArguments(mi.getPadding().getArguments()); + JavaType.Method newType = (JavaType.Method) visitType(mi.getMethodType(), ctx); + m = m.withMethodType(newType).withName(m.getName().withType(newType)); + return super.visitMethodInvocation(m, ctx); } - if (c.getConstructorType() != null) { - isParentTypeDownCast = isParentTypeMatched(c.getConstructorType().getParameterTypes().get(index)); - } - } - } else if (parent instanceof J.NewArray) { - J.NewArray a = (J.NewArray) parent; - JavaType arrayType = a.getType(); - while (arrayType instanceof JavaType.Array) { - arrayType = ((JavaType.Array) arrayType).getElemType(); - } - isParentTypeDownCast = isParentTypeMatched(arrayType); - } - return isParentTypeDownCast; - } + private boolean isParentTypeDownCast(MethodCall immutableMethod) { + J parent = getCursor().dropParentUntil(J.class::isInstance).getValue(); + boolean isParentTypeDownCast = false; + if (parent instanceof J.VariableDeclarations.NamedVariable) { + isParentTypeDownCast = isParentTypeMatched(((J.VariableDeclarations.NamedVariable) parent).getType()); + } else if (parent instanceof J.Assignment) { + J.Assignment a = (J.Assignment) parent; + if (a.getVariable() instanceof J.Identifier && ((J.Identifier) a.getVariable()).getFieldType() != null) { + isParentTypeDownCast = isParentTypeMatched(((J.Identifier) a.getVariable()).getFieldType().getType()); + } else if (a.getVariable() instanceof J.FieldAccess) { + isParentTypeDownCast = isParentTypeMatched(a.getVariable().getType()); + } + } else if (parent instanceof J.Return) { + // Does not currently support returns in lambda expressions. + J j = getCursor().dropParentUntil(is -> is instanceof J.MethodDeclaration || is instanceof J.CompilationUnit).getValue(); + if (j instanceof J.MethodDeclaration) { + TypeTree returnType = ((J.MethodDeclaration) j).getReturnTypeExpression(); + if (returnType != null) { + isParentTypeDownCast = isParentTypeMatched(returnType.getType()); + } + } + } else if (parent instanceof J.MethodInvocation) { + J.MethodInvocation m = (J.MethodInvocation) parent; + int index = m.getArguments().indexOf(immutableMethod); + if (m.getMethodType() != null) { + if (index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) { + isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index)); + } else { + isParentTypeDownCast = !TypeUtils.isOfClassType(m.getMethodType().getReturnType(), guavaType); + } + } + } else if (parent instanceof J.NewClass) { + J.NewClass c = (J.NewClass) parent; + int index = 0; + if (c.getConstructorType() != null) { + for (Expression argument : c.getArguments()) { + if (IMMUTABLE_MATCHER.matches(argument)) { + break; + } + index++; + } + if (c.getConstructorType() != null) { + isParentTypeDownCast = isParentTypeMatched(c.getConstructorType().getParameterTypes().get(index)); + } + } + } else if (parent instanceof J.NewArray) { + J.NewArray a = (J.NewArray) parent; + JavaType arrayType = a.getType(); + while (arrayType instanceof JavaType.Array) { + arrayType = ((JavaType.Array) arrayType).getElemType(); + } + + isParentTypeDownCast = isParentTypeMatched(arrayType); + } + return isParentTypeDownCast; + } - private boolean isParentTypeMatched(@Nullable JavaType type) { - JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type); - return TypeUtils.isOfClassType(fq, javaType) || - TypeUtils.isOfClassType(fq, "java.lang.Object"); - } - }); - } + private boolean isParentTypeMatched(@Nullable JavaType type) { + JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type); + return TypeUtils.isOfClassType(fq, javaType) || + TypeUtils.isOfClassType(fq, "java.lang.Object"); + } + }); + } } diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java index bcd78c042..70bcbeb0c 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java @@ -28,27 +28,27 @@ class NoGuavaImmutableListOfTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec - .recipe(new NoGuavaImmutableListOf()) - .parser(JavaParser.fromJavaVersion().classpath("guava")); + .recipe(new NoGuavaImmutableListOf()) + .parser(JavaParser.fromJavaVersion().classpath("guava")); } @Test void doNotChangeReturnsImmutableList() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - ImmutableList getList() { - return ImmutableList.of(); - } - } - """ - ) - ,9) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + ImmutableList getList() { + return ImmutableList.of(); + } + } + """ + ) + , 9) ); } @@ -56,44 +56,44 @@ ImmutableList getList() { @Test void doNotChangeFieldAssignmentToImmutableList() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; - class Test { - ImmutableList m; + class Test { + ImmutableList m; - { - this.m = ImmutableList.of(); - } - } - """ - ) - ,9) + { + this.m = ImmutableList.of(); + } + } + """ + ) + , 9) ); } @Test void doNotChangeAssignsToImmutableList() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; - class Test { - ImmutableList m; + class Test { + ImmutableList m; - void init() { - m = ImmutableList.of(); - } - } - """ - ) - ,9) + void init() { + m = ImmutableList.of(); + } + } + """ + ) + , 9) ); } @@ -101,185 +101,185 @@ void init() { void doNotChangeNewClass() { //language=java rewriteRun( + java( + """ + import com.google.common.collect.ImmutableList; + + public class A { + ImmutableList immutableList; + public A(ImmutableList immutableList) { + this.immutableList = immutableList; + } + } + """ + ), + version( + //language=java java( + """ + import com.google.common.collect.ImmutableList; + + class Test { + A a = new A(ImmutableList.of()); + } + """) + , 9) + ); + } + + @Test + void doNotChangeMethodInvocation() { + rewriteRun( + spec -> spec.parser( + JavaParser.fromJavaVersion() + .classpath("guava") + .dependsOn( + //language=java """ import com.google.common.collect.ImmutableList; public class A { ImmutableList immutableList; - public A(ImmutableList immutableList) { + public void method(ImmutableList immutableList) { this.immutableList = immutableList; } } """ - ), - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - A a = new A(ImmutableList.of()); - } - """) - ,9) - ); - } + ) + ), + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; - @Test - void doNotChangeMethodInvocation() { - rewriteRun( - spec -> spec.parser( - JavaParser.fromJavaVersion() - .classpath("guava") - .dependsOn( - //language=java - """ - import com.google.common.collect.ImmutableList; - - public class A { - ImmutableList immutableList; - public void method(ImmutableList immutableList) { - this.immutableList = immutableList; - } - } - """ - ) - ), - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableList.of()); - } - } - """ - ) - ,9) + class Test { + void method() { + A a = new A(); + a.method(ImmutableList.of()); + } + } + """ + ) + , 9) ); } @Test void replaceArguments() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; - class Test { - List m = ImmutableList.of("A", "B", "C", "D"); - } - """, - """ - import java.util.List; + class Test { + List m = ImmutableList.of("A", "B", "C", "D"); + } + """, + """ + import java.util.List; - class Test { - List m = List.of("A", "B", "C", "D"); - } - """ - ), - 9 - ) + class Test { + List m = List.of("A", "B", "C", "D"); + } + """ + ), + 9 + ) ); } @Test void fieldAssignmentToList() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; - - class Test { - List m; - { - this.m = ImmutableList.of(); - } - } - """, - """ - import java.util.List; - - class Test { - List m; - { - this.m = List.of(); - } - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; + + class Test { + List m; + { + this.m = ImmutableList.of(); + } + } + """, + """ + import java.util.List; + + class Test { + List m; + { + this.m = List.of(); + } + } + """ + ), + 9 + ) ); } @Test void assignmentToList() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; - class Test { - List m = ImmutableList.of(); - } - """, - """ - import java.util.List; + class Test { + List m = ImmutableList.of(); + } + """, + """ + import java.util.List; - class Test { - List m = List.of(); - } - """ - ), - 9 - ) + class Test { + List m = List.of(); + } + """ + ), + 9 + ) ); } @Test void returnsList() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; - - class Test { - List list() { - return ImmutableList.of(); - } - } - """, - """ - import java.util.List; - - class Test { - List list() { - return List.of(); - } - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; + + class Test { + List list() { + return ImmutableList.of(); + } + } + """, + """ + import java.util.List; + + class Test { + List list() { + return List.of(); + } + } + """ + ), + 9 + ) ); } @@ -287,38 +287,38 @@ List list() { void newClassWithListArgument() { //language=java rewriteRun( + java( + """ + import java.util.List; + + public class A { + List list; + public A(List list) { + this.list = list; + } + } + """ + ), + version( + //language=java java( - """ + """ + import com.google.common.collect.ImmutableList; + + class Test { + A a = new A(ImmutableList.of()); + } + """, + """ import java.util.List; - public class A { - List list; - public A(List list) { - this.list = list; - } + class Test { + A a = new A(List.of()); } - """ + """ ), - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - A a = new A(ImmutableList.of()); - } - """, - """ - import java.util.List; - - class Test { - A a = new A(List.of()); - } - """ - ), - 11 - ) + 11 + ) ); } @@ -326,44 +326,44 @@ class Test { void doChangeMethodInvocationWithSelect() { //language=java rewriteRun( + java( + """ + import java.util.List; + + public class A { + Object[] list; + public void method(Object[] list ) { + this.list = list; + } + } + """ + ), + version( + //language=java java( - """ - import java.util.List; + """ + import com.google.common.collect.ImmutableList; - public class A { - Object[] list; - public void method(Object[] list ) { - this.list = list; - } - } - """ + class Test { + void method() { + A a = new A(); + a.method(ImmutableList.of().toArray()); + } + } + """, + """ + import java.util.List; + + class Test { + void method() { + A a = new A(); + a.method(List.of().toArray()); + } + } + """ ), - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableList.of().toArray()); - } - } - """, - """ - import java.util.List; - - class Test { - void method() { - A a = new A(); - a.method(List.of().toArray()); - } - } - """ - ), - 11 - ) + 11 + ) ); } @@ -371,44 +371,44 @@ void method() { void methodInvocationWithListArgument() { //language=java rewriteRun( + java( + """ + import java.util.List; + + public class A { + List list; + public void method(List list) { + this.list = list; + } + } + """ + ), + version( + //language=java java( - """ - import java.util.List; + """ + import com.google.common.collect.ImmutableList; - public class A { - List list; - public void method(List list) { - this.list = list; - } - } - """ + class Test { + void method() { + A a = new A(); + a.method(ImmutableList.of()); + } + } + """, + """ + import java.util.List; + + class Test { + void method() { + A a = new A(); + a.method(List.of()); + } + } + """ ), - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableList.of()); - } - } - """, - """ - import java.util.List; - - class Test { - void method() { - A a = new A(); - a.method(List.of()); - } - } - """ - ), - 11 - ) + 11 + ) ); } @@ -416,31 +416,31 @@ void method() { @Test void listOfInts() { rewriteRun( - version( - //language=java - java( - """ - import java.util.List; - import com.google.common.collect.ImmutableList; - - class Test { - List list() { - return ImmutableList.of(1, 2, 3); - } - } - """, - """ - import java.util.List; - - class Test { - List list() { - return List.of(1, 2, 3); - } - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import java.util.List; + import com.google.common.collect.ImmutableList; + + class Test { + List list() { + return ImmutableList.of(1, 2, 3); + } + } + """, + """ + import java.util.List; + + class Test { + List list() { + return List.of(1, 2, 3); + } + } + """ + ), + 9 + ) ); } @@ -448,30 +448,30 @@ List list() { @Test void insideAnonymousArrayInitializer() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class A { - Object[] o = new Object[] { - ImmutableList.of(1, 2, 3) - }; - } - """, - """ - import java.util.List; - - class A { - Object[] o = new Object[] { - List.of(1, 2, 3) - }; - } - """ - ), - 9 - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class A { + Object[] o = new Object[] { + ImmutableList.of(1, 2, 3) + }; + } + """, + """ + import java.util.List; + + class A { + Object[] o = new Object[] { + List.of(1, 2, 3) + }; + } + """ + ), + 9 + ) ); } @@ -479,26 +479,26 @@ class A { @Test void assignToMoreGeneralType() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; - class A { - Object o = ImmutableList.of(1, 2, 3); - } - """, - """ - import java.util.List; + class A { + Object o = ImmutableList.of(1, 2, 3); + } + """, + """ + import java.util.List; - class A { - Object o = List.of(1, 2, 3); - } - """ - ), - 11 - ) + class A { + Object o = List.of(1, 2, 3); + } + """ + ), + 11 + ) ); } @@ -507,19 +507,19 @@ class A { void doChangeAssignToImmutableMap() { //language=java rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableList; - import java.util.List; - - class A { - Object o = List.of(ImmutableList.of(1, 2), ImmutableList.of(2, 3)); - } - """ - ), - 9 - ) + version( + java( + """ + import com.google.common.collect.ImmutableList; + import java.util.List; + + class A { + Object o = List.of(ImmutableList.of(1, 2), ImmutableList.of(2, 3)); + } + """ + ), + 9 + ) ); } @@ -527,30 +527,30 @@ class A { @Test void doChangeWithStreamOperation() { rewriteRun( - version( - //language=java - java( - """ - import com.google.common.collect.ImmutableList; - - class A { - void test() { - final List list = ImmutableList.of(1, 2).stream().toList(); - } - } - """, - """ - import java.util.List; - - class A { - void test() { - final List list = List.of(1, 2).stream().toList(); - } - } - """ - ), - 11 - ) + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class A { + void test() { + final List list = ImmutableList.of(1, 2).stream().toList(); + } + } + """, + """ + import java.util.List; + + class A { + void test() { + final List list = List.of(1, 2).stream().toList(); + } + } + """ + ), + 11 + ) ); } diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java index b975f951e..6a76da8a8 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java @@ -1,4 +1,4 @@ -/* + /* * Copyright 2021 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,45 +28,45 @@ class NoGuavaImmutableMapOfTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec - .recipe(new NoGuavaImmutableMapOf()) - .parser(JavaParser.fromJavaVersion().classpath("guava")); + .recipe(new NoGuavaImmutableMapOf()) + .parser(JavaParser.fromJavaVersion().classpath("guava")); } @Test void doNotChangeReturnsImmutableMap() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableMap; + java( + """ + import com.google.common.collect.ImmutableMap; - class Test { - ImmutableMap getMap() { - return ImmutableMap.of(); - } + class Test { + ImmutableMap getMap() { + return ImmutableMap.of(); } - """ - ) + } + """ + ) ); } @Test void doNotChangeFieldAssignmentToImmutableMap() { rewriteRun( - //language=java - java( - """ - import com.google.common.collect.ImmutableMap; + //language=java + java( + """ + import com.google.common.collect.ImmutableMap; - class Test { - ImmutableMap m; + class Test { + ImmutableMap m; - { - this.m = ImmutableMap.of(); - } + { + this.m = ImmutableMap.of(); } - """ - ) + } + """ + ) ); } @@ -74,19 +74,19 @@ class Test { void doNotChangeAssignsToImmutableMap() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableMap; + java( + """ + import com.google.common.collect.ImmutableMap; - class Test { - ImmutableMap m; + class Test { + ImmutableMap m; - void init() { - m = ImmutableMap.of(); - } + void init() { + m = ImmutableMap.of(); } - """ - ) + } + """ + ) ); } @@ -94,27 +94,27 @@ void init() { void doNotChangeNewClass() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableMap; - - public class A { - ImmutableMap immutableMap; - public A(ImmutableMap immutableMap) { - this.immutableMap = immutableMap; - } + java( + """ + import com.google.common.collect.ImmutableMap; + + public class A { + ImmutableMap immutableMap; + public A(ImmutableMap immutableMap) { + this.immutableMap = immutableMap; } - """ - ), - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - A a = new A(ImmutableMap.of()); - } - """ - ) + } + """ + ), + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + A a = new A(ImmutableMap.of()); + } + """ + ) ); } @@ -122,30 +122,30 @@ class Test { void doNotChangeMethodInvocation() { //language=java rewriteRun( - java( - """ - import com.google.common.collect.ImmutableMap; - - public class A { - ImmutableMap immutableMap; - public void method(ImmutableMap immutableMap) { - this.immutableMap = immutableMap; - } + java( + """ + import com.google.common.collect.ImmutableMap; + + public class A { + ImmutableMap immutableMap; + public void method(ImmutableMap immutableMap) { + this.immutableMap = immutableMap; } - """ - ), - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableMap.of()); - } + } + """ + ), + java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + void method() { + A a = new A(); + a.method(ImmutableMap.of()); } - """ - ) + } + """ + ) ); } @@ -153,26 +153,26 @@ void method() { void replaceArguments() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map m = ImmutableMap.of("A", "B", "C", "D"); - } - """, - """ - import java.util.Map; - - class Test { - Map m = Map.of("A", "B", "C", "D"); - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map m = ImmutableMap.of("A", "B", "C", "D"); + } + """, + """ + import java.util.Map; + + class Test { + Map m = Map.of("A", "B", "C", "D"); + } + """ + ), + 9 + ) ); } @@ -180,32 +180,32 @@ class Test { void fieldAssignmentToMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map m; - { - this.m = ImmutableMap.of(); - } - } - """, - """ - import java.util.Map; - - class Test { - Map m; - { - this.m = Map.of(); - } - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map m; + { + this.m = ImmutableMap.of(); + } + } + """, + """ + import java.util.Map; + + class Test { + Map m; + { + this.m = Map.of(); + } + } + """ + ), + 9 + ) ); } @@ -213,26 +213,26 @@ class Test { void assignmentToMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map m = ImmutableMap.of(); - } - """, - """ - import java.util.Map; - - class Test { - Map m = Map.of(); - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map m = ImmutableMap.of(); + } + """, + """ + import java.util.Map; + + class Test { + Map m = Map.of(); + } + """ + ), + 9 + ) ); } @@ -240,30 +240,30 @@ class Test { void returnsMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map map() { - return ImmutableMap.of(); - } - } - """, - """ - import java.util.Map; - - class Test { - Map map() { - return Map.of(); - } - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map map() { + return ImmutableMap.of(); + } + } + """, + """ + import java.util.Map; + + class Test { + Map map() { + return Map.of(); + } + } + """ + ), + 9 + ) ); } @@ -272,30 +272,30 @@ Map map() { void mapOfInts() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map map() { - return ImmutableMap.of(1, 1, 2, 2, 3, 3); - } - } - """, - """ - import java.util.Map; - - class Test { - Map map() { - return Map.of(1, 1, 2, 2, 3, 3); - } - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map map() { + return ImmutableMap.of(1, 1, 2, 2, 3, 3); + } + } + """, + """ + import java.util.Map; + + class Test { + Map map() { + return Map.of(1, 1, 2, 2, 3, 3); + } + } + """ + ), + 9 + ) ); } @@ -303,37 +303,37 @@ Map map() { void newClassWithMapArgument() { //language=java rewriteRun( + java( + """ + import java.util.Map; + + public class A { + Map map; + public A(Map map) { + this.map = map; + } + } + """ + ), + version( java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + A a = new A(ImmutableMap.of()); + } + """, + """ + import java.util.Map; + + class Test { + A a = new A(Map.of()); + } """ - import java.util.Map; - - public class A { - Map map; - public A(Map map) { - this.map = map; - } - } - """ ), - version( - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - A a = new A(ImmutableMap.of()); - } - """, - """ - import java.util.Map; - - class Test { - A a = new A(Map.of()); - } - """ - ), - 9 - ) + 9 + ) ); } @@ -341,43 +341,43 @@ class Test { void methodInvocationWithMapArgument() { //language=java rewriteRun( + java( + """ + import java.util.Map; + + public class A { + Map map; + public void method(Map map) { + this.map = map; + } + } + """ + ), + version( java( + """ + import com.google.common.collect.ImmutableMap; + + class Test { + void method() { + A a = new A(); + a.method(ImmutableMap.of()); + } + } + """, + """ + import java.util.Map; + + class Test { + void method() { + A a = new A(); + a.method(Map.of()); + } + } """ - import java.util.Map; - - public class A { - Map map; - public void method(Map map) { - this.map = map; - } - } - """ ), - version( - java( - """ - import com.google.common.collect.ImmutableMap; - - class Test { - void method() { - A a = new A(); - a.method(ImmutableMap.of()); - } - } - """, - """ - import java.util.Map; - - class Test { - void method() { - A a = new A(); - a.method(Map.of()); - } - } - """ - ), - 9 - ) + 9 + ) ); } @@ -385,38 +385,38 @@ void method() { void variableIsMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.HashMap; - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - Map> map = new HashMap<>(); - void setMap(String value) { - for (int i = 0; i < 10; i++) { - map.getOrDefault(i, ImmutableMap.of()); - } - } - } - """, - """ - import java.util.HashMap; - import java.util.Map; - - class Test { - Map> map = new HashMap<>(); - void setMap(String value) { - for (int i = 0; i < 10; i++) { - map.getOrDefault(i, Map.of()); - } - } - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.HashMap; + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + Map> map = new HashMap<>(); + void setMap(String value) { + for (int i = 0; i < 10; i++) { + map.getOrDefault(i, ImmutableMap.of()); + } + } + } + """, + """ + import java.util.HashMap; + import java.util.Map; + + class Test { + Map> map = new HashMap<>(); + void setMap(String value) { + for (int i = 0; i < 10; i++) { + map.getOrDefault(i, Map.of()); + } + } + } + """ + ), + 9 + ) ); } @@ -425,29 +425,29 @@ void setMap(String value) { void insideAnonymousArrayInitializer() { //language=java rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableMap; - - class A { - Object[] o = new Object[] { - ImmutableMap.of(1, 1, 2, 2, 3, 3) - }; - } - """, - """ - import java.util.Map; - - class A { - Object[] o = new Object[] { - Map.of(1, 1, 2, 2, 3, 3) - }; - } - """ - ), - 11 - ) + version( + java( + """ + import com.google.common.collect.ImmutableMap; + + class A { + Object[] o = new Object[] { + ImmutableMap.of(1, 1, 2, 2, 3, 3) + }; + } + """, + """ + import java.util.Map; + + class A { + Object[] o = new Object[] { + Map.of(1, 1, 2, 2, 3, 3) + }; + } + """ + ), + 11 + ) ); } @@ -456,25 +456,25 @@ class A { void assignToMoreGeneralType() { //language=java rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableMap; - - class A { - Object o = ImmutableMap.of(1, 1, 2, 2, 3, 3); - } - """, - """ - import java.util.Map; - - class A { - Object o = Map.of(1, 1, 2, 2, 3, 3); - } - """ - ), - 11 - ) + version( + java( + """ + import com.google.common.collect.ImmutableMap; + + class A { + Object o = ImmutableMap.of(1, 1, 2, 2, 3, 3); + } + """, + """ + import java.util.Map; + + class A { + Object o = Map.of(1, 1, 2, 2, 3, 3); + } + """ + ), + 11 + ) ); } @@ -483,19 +483,19 @@ class A { void doNotChangeNestedMaps() { //language=java rewriteRun( - version( - java( - """ - import com.google.common.collect.ImmutableMap; - import java.util.Map; - - class A { - Object o = Map.of(1, ImmutableMap.of(2, 3)); - } - """ - ), - 11 - ) + version( + java( + """ + import com.google.common.collect.ImmutableMap; + import java.util.Map; + + class A { + Object o = Map.of(1, ImmutableMap.of(2, 3)); + } + """ + ), + 11 + ) ); } @@ -504,19 +504,19 @@ class A { void doNotChangeAssignToImmutableMap() { //language=java rewriteRun( - version( - java( - """ - import java.util.Map; - import com.google.common.collect.ImmutableMap; - - class Test { - ImmutableMap m = ImmutableMap.of(); - } - """ - ), - 9 - ) + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + ImmutableMap m = ImmutableMap.of(); + } + """ + ), + 9 + ) ); } } From 20ce9da50e176fec5b36daf8e9198046a8033fe3 Mon Sep 17 00:00:00 2001 From: bramli ahmed khalil Date: Wed, 13 Nov 2024 18:33:29 +0100 Subject: [PATCH 3/5] add Ability To Convert Return Type as an option --- .../guava/AbstractNoGuavaImmutableOf.java | 70 +++++++++++ .../migrate/guava/NoGuavaImmutableListOf.java | 4 + .../migrate/guava/NoGuavaImmutableMapOf.java | 3 + .../migrate/guava/NoGuavaImmutableSetOf.java | 3 + .../guava/NoGuavaImmutableListOfTest.java | 84 +++++++++++++ .../guava/NoGuavaImmutableMapOfTest.java | 114 ++++++++++++++++++ .../guava/NoGuavaImmutableSetOfTest.java | 55 +++++++++ 7 files changed, 333 insertions(+) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java index 2e5f16b86..4f26bc0bd 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java @@ -15,11 +15,17 @@ */ package org.openrewrite.java.migrate.guava; +import static java.util.Collections.emptyList; + +import java.util.ArrayList; import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; +import org.openrewrite.Option; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; +import org.openrewrite.Tree; import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.ListUtils; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; @@ -29,16 +35,28 @@ import java.time.Duration; import java.util.List; +import org.openrewrite.marker.Markers; abstract class AbstractNoGuavaImmutableOf extends Recipe { private final String guavaType; private final String javaType; + @Option(displayName = "Whether to convert return type (the default value is false).", + description = "converting the return type from Guava Type to Java Type " + + "The default value is false.", + example = "true", + required = false) + boolean isAbleToConvertReturnType; AbstractNoGuavaImmutableOf(String guavaType, String javaType) { this.guavaType = guavaType; this.javaType = javaType; } + AbstractNoGuavaImmutableOf(String guavaType, String javaType, boolean isAbleToConvertReturnType) { + this.guavaType = guavaType; + this.javaType = javaType; + this.isAbleToConvertReturnType = isAbleToConvertReturnType; + } private String getShortType(String fullyQualifiedType) { return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1); @@ -155,9 +173,61 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) { return isParentTypeDownCast; } + @Override + public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { + J.VariableDeclarations mv = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx); + if(!isAbleToConvertReturnType) { + return mv; + } + if (multiVariable != mv && TypeUtils.isOfClassType(mv.getType(), guavaType)) { + JavaType newType = JavaType.buildType(javaType); + mv = mv.withTypeExpression(mv.getTypeExpression() == null ? + null : createNewTypeExpression(mv.getTypeExpression(), newType)); + + mv = mv.withVariables(ListUtils.map(mv.getVariables(), variable -> { + JavaType.FullyQualified varType = TypeUtils.asFullyQualified(variable.getType()); + if (varType != null && !varType.equals(newType)) { + return variable.withType(newType).withName(variable.getName().withType(newType)); + } + return variable; + })); + } + + return mv; + } + + private TypeTree createNewTypeExpression(TypeTree typeTree, JavaType newType) { + if (typeTree instanceof J.ParameterizedType) { + J.ParameterizedType parameterizedType = (J.ParameterizedType) typeTree; + List> jRightPaddedList = new ArrayList<>(); + parameterizedType.getTypeParameters().forEach( + expression -> { + if (expression instanceof J.ParameterizedType && TypeUtils.isOfClassType(expression.getType(), guavaType)) { + jRightPaddedList.add(JRightPadded.build(((J.ParameterizedType) createNewTypeExpression((TypeTree) expression, newType)))); + } else { + jRightPaddedList.add(JRightPadded.build(expression)); + } + }); + NameTree clazz = new J.Identifier( + Tree.randomId(), Space.EMPTY, Markers.EMPTY, emptyList(), getShortType(javaType), null, null); + return parameterizedType.withClazz(clazz).withType(newType).getPadding().withTypeParameters(JContainer.build(jRightPaddedList)); + } + return new J.Identifier( + typeTree.getId(), + typeTree.getPrefix(), + Markers.EMPTY, + emptyList(), + getShortType(javaType), + newType, + null + ); + } + + private boolean isParentTypeMatched(@Nullable JavaType type) { JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type); return TypeUtils.isOfClassType(fq, javaType) || + (isAbleToConvertReturnType && TypeUtils.isOfClassType(fq, guavaType)) || TypeUtils.isOfClassType(fq, "java.lang.Object"); } }); diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOf.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOf.java index 69e71f1b1..952cec6d8 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOf.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOf.java @@ -19,4 +19,8 @@ public class NoGuavaImmutableListOf extends AbstractNoGuavaImmutableOf { public NoGuavaImmutableListOf() { super("com.google.common.collect.ImmutableList", "java.util.List"); } + + public NoGuavaImmutableListOf(boolean isAbleToConvertReturnType) { + super("com.google.common.collect.ImmutableList", "java.util.List", isAbleToConvertReturnType); + } } diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOf.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOf.java index 55866046a..393c87c39 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOf.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOf.java @@ -19,4 +19,7 @@ public class NoGuavaImmutableMapOf extends AbstractNoGuavaImmutableOf { public NoGuavaImmutableMapOf() { super("com.google.common.collect.ImmutableMap", "java.util.Map"); } + public NoGuavaImmutableMapOf(boolean isAbleToConvertReturnType) { + super("com.google.common.collect.ImmutableMap", "java.util.Map", isAbleToConvertReturnType); + } } diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOf.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOf.java index 17fc5649e..a805f9040 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOf.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOf.java @@ -19,4 +19,7 @@ public class NoGuavaImmutableSetOf extends AbstractNoGuavaImmutableOf { public NoGuavaImmutableSetOf() { super("com.google.common.collect.ImmutableSet", "java.util.Set"); } + public NoGuavaImmutableSetOf(boolean isAbleToConvertReturnType) { + super("com.google.common.collect.ImmutableSet", "java.util.Set", isAbleToConvertReturnType); + } } diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java index 70bcbeb0c..44945e87e 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOfTest.java @@ -576,4 +576,88 @@ void test() { ); } + @Test + void doChangeAllAssignToImmutableMap() { + //language=java + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableListOf(true)), + version( + java( + """ + import com.google.common.collect.ImmutableList; + import java.util.List; + + class A { + Object o = List.of(ImmutableList.of(1, 2), ImmutableList.of(2, 3)); + } + """, + """ + import java.util.List; + + class A { + Object o = List.of(List.of(1, 2), List.of(2, 3)); + } + """ + ), + 9 + ) + ); + } + @Test + void doChangeNestedListsWithVariableType() { + //language=java + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableListOf(true)), + version( + java( + """ + import com.google.common.collect.ImmutableList; + + class A { + ImmutableList> o = ImmutableList.of(ImmutableList.of(1, 2), ImmutableList.of(2, 3)); + } + """, + """ + import java.util.List; + + class A { + List> o = List.of(List.of(1, 2), List.of(2, 3)); + } + """ + ), + 9 + ) + ); + } + + @Test + void doChangeAssignFromImmutableListToList() { + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableListOf(true)), + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableList; + + class A { + void test() { + ImmutableList o = ImmutableList.of(1, 2); + } + } + """, + """ + import java.util.List; + + class A { + void test() { + List o = List.of(1, 2); + } + } + """ + ), + 11 + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java index 6a76da8a8..b88e6dcd0 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOfTest.java @@ -519,4 +519,118 @@ class Test { ) ); } + @Test + void doChangeNestedMaps() { + //language=java + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableMapOf(true)), + version( + java( + """ + import com.google.common.collect.ImmutableMap; + import java.util.Map; + + class A { + Object o = Map.of(1, ImmutableMap.of(2, 3)); + } + """, + """ + import java.util.Map; + + class A { + Object o = Map.of(1, Map.of(2, 3)); + } + """ + ), + 11 + ) + ); + } + + @Test + void doChangeNestedMaps2() { + //language=java + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableMapOf(true)), + version( + java( + """ + import com.google.common.collect.ImmutableMap; + + class A { + Object o = ImmutableMap.of(1, ImmutableMap.of(2, 3)); + } + """, + """ + import java.util.Map; + + class A { + Object o = Map.of(1, Map.of(2, 3)); + } + """ + ), + 11 + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/256") + @Test + void doChangeAssignToImmutableMap() { + //language=java + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableMapOf(true)), + version( + java( + """ + import java.util.Map; + import com.google.common.collect.ImmutableMap; + + class Test { + ImmutableMap m = ImmutableMap.of(); + } + """, + """ + import java.util.Map; + + class Test { + Map m = Map.of(); + } + """ + ), + 9 + ) + ); + } + + @Test + void doChangeNestedAssignReturnType() { + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableMapOf(true)), + version( + //language=java + java( + """ + import com.google.common.collect.ImmutableMap; + + class A { + public void getMap() { + ImmutableMap> s = ImmutableMap.of("key", ImmutableMap.of("value1", "value2")); + } + } + """, + """ + import java.util.Map; + + class A { + public void getMap() { + Map> s = Map.of("key", Map.of("value1", "value2")); + } + } + """ + ), + 9 + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOfTest.java index ad1acec03..9514c7db3 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOfTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOfTest.java @@ -518,4 +518,59 @@ class Test { ) ); } + + @Test + void doChangeNestedSets() { + //language=java + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableSetOf(true)), + version( + java( + """ + import com.google.common.collect.ImmutableSet; + import java.util.Set; + + class A { + Object o = Set.of(ImmutableSet.of(1, 2)); + } + """, + """ + import java.util.Set; + + class A { + Object o = Set.of(Set.of(1, 2)); + } + """ + ), + 9 + ) + ); + } + + @Test + void doChangeAssignToImmutableSet() { + //language=java + rewriteRun( + spec -> spec.recipe(new NoGuavaImmutableSetOf(true)), + version( + java( + """ + import com.google.common.collect.ImmutableSet; + + class Test { + ImmutableSet m = ImmutableSet.of(); + } + """, + """ + import java.util.Set; + + class Test { + Set m = Set.of(); + } + """ + ), + 11) + ); + } + } From e49def9b66b3dcc1ef54b1bf73c4923d38af60b5 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 14 Nov 2024 10:23:55 +0100 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/migrate/guava/AbstractNoGuavaImmutableOf.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java index 4f26bc0bd..8590637c3 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java @@ -15,9 +15,6 @@ */ package org.openrewrite.java.migrate.guava; -import static java.util.Collections.emptyList; - -import java.util.ArrayList; import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Option; @@ -32,8 +29,10 @@ import org.openrewrite.java.search.UsesJavaVersion; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.*; +import org.openrewrite.marker.Markers; +import java.util.ArrayList; -import java.time.Duration; +import static java.util.Collections.emptyList; import java.util.List; import org.openrewrite.marker.Markers; From 25464eb4a328a7f0f038cf3e2954e9e4f3e1d24d Mon Sep 17 00:00:00 2001 From: bramli ahmed khalil Date: Thu, 14 Nov 2024 18:42:09 +0100 Subject: [PATCH 5/5] fix conflicts --- .../java/migrate/guava/AbstractNoGuavaImmutableOf.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java index 8590637c3..a9594533c 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java @@ -15,6 +15,7 @@ */ package org.openrewrite.java.migrate.guava; +import java.time.Duration; import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Option; @@ -34,7 +35,6 @@ import static java.util.Collections.emptyList; import java.util.List; -import org.openrewrite.marker.Markers; abstract class AbstractNoGuavaImmutableOf extends Recipe {