diff --git a/src/main/java/org/openrewrite/java/testing/testcontainers/ConvertToRawType.java b/src/main/java/org/openrewrite/java/testing/testcontainers/ConvertToRawType.java new file mode 100644 index 000000000..65eba5fed --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/testcontainers/ConvertToRawType.java @@ -0,0 +1,71 @@ +/* + * Copyright 2025 the original author or authors. + *
+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://docs.moderne.io/licensing/moderne-source-available-license + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.testcontainers;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.jspecify.annotations.Nullable;
+import org.openrewrite.*;
+import org.openrewrite.java.JavaVisitor;
+import org.openrewrite.java.search.UsesType;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaType;
+import org.openrewrite.java.tree.TypeUtils;
+
+@Value
+@EqualsAndHashCode(callSuper = false)
+public class ConvertToRawType extends Recipe {
+
+ @Option(displayName = "Fully qualified type name",
+ description = "The fully qualified name of the Java class to convert to its raw type.",
+ example = "org.testcontainers.containers.PostgreSQLContainer")
+ String fullyQualifiedTypeName;
+
+ @Override
+ public String getDisplayName() {
+ return "Remove parameterized type arguments from a Java class";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Convert parameterized types of a specified Java class to their raw types.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return Preconditions.check(new UsesType<>(fullyQualifiedTypeName, false), new JavaVisitor
+ * Licensed under the Moderne Source Available License (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://docs.moderne.io/licensing/moderne-source-available-license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.testcontainers;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class ConvertToRawTypeTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .recipeFromResources("org.openrewrite.java.testing.testcontainers.Testcontainers2ContainerClasses")
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "testcontainers-1", "nginx"));
+ }
+
+ @DocumentExample
+ @Test
+ void variableDeclaration() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.testcontainers.containers.NginxContainer;
+
+ class Foo {
+ NginxContainer> container = new NginxContainer<>();
+ }
+ """,
+ """
+ import org.testcontainers.nginx.NginxContainer;
+
+ class Foo {
+ NginxContainer container = new NginxContainer();
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void variableDeclarationWithModifier() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.testcontainers.containers.NginxContainer;
+
+ class Foo {
+ private static final NginxContainer> container = new NginxContainer<>();
+ }
+ """,
+ """
+ import org.testcontainers.nginx.NginxContainer;
+
+ class Foo {
+ private static final NginxContainer container = new NginxContainer();
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void methodReturnType() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.testcontainers.containers.NginxContainer;
+
+ class Foo {
+ NginxContainer> createContainer() {
+ return new NginxContainer<>();
+ }
+ }
+ """,
+ """
+ import org.testcontainers.nginx.NginxContainer;
+
+ class Foo {
+ NginxContainer createContainer() {
+ return new NginxContainer();
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void newClassExpression() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.testcontainers.containers.NginxContainer;
+
+ class Foo {
+ void test() {
+ var container = new NginxContainer<>();
+ }
+ }
+ """,
+ """
+ import org.testcontainers.nginx.NginxContainer;
+
+ class Foo {
+ void test() {
+ var container = new NginxContainer();
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void onlyChangeImportWhenAlreadyRaw() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.testcontainers.containers.NginxContainer;
+
+ class Foo {
+ NginxContainer container = new NginxContainer();
+ }
+ """,
+ """
+ import org.testcontainers.nginx.NginxContainer;
+
+ class Foo {
+ NginxContainer container = new NginxContainer();
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void noChangeWhenDifferentType() {
+ rewriteRun(
+ spec -> spec.recipe(new ConvertToRawType("org.testcontainers.containers.MySQLContainer")),
+ //language=java
+ java(
+ """
+ import org.testcontainers.containers.NginxContainer;
+ class Foo {
+ NginxContainer> container = new NginxContainer<>();
+ }
+ """
+ )
+ );
+ }
+}