From 85ecc14587cc46e3a83de487e89e97f5842a1f7b Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 13 Nov 2024 14:27:29 +0100 Subject: [PATCH] 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 + ) ); } }