diff --git a/src/main/java/org/openrewrite/java/migrate/lang/ExplicitRecordImport.java b/src/main/java/org/openrewrite/java/migrate/lang/ExplicitRecordImport.java new file mode 100644 index 000000000..bf4e2b00c --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/lang/ExplicitRecordImport.java @@ -0,0 +1,62 @@ +/* + * Copyright 2024 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.migrate.lang;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Preconditions;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.search.UsesType;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaSourceFile;
+import org.openrewrite.java.tree.JavaType;
+
+public class ExplicitRecordImport extends Recipe {
+ @Override
+ public String getDisplayName() {
+ return "Add explicit import for `Record` classes";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Add explicit import for `Record` classes when upgrading past Java 14+, to avoid conflicts with `java.lang.Record`.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return Preconditions.check(
+ new UsesType<>("*..Record", false),
+ new JavaIsoVisitor
+ * 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.migrate.lang;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.Issue;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class ExplicitRecordImportTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new ExplicitRecordImport())
+ //language=java
+ .parser(JavaParser.fromJavaVersion().dependsOn("""
+ package com.acme.music;
+ public class Record {
+ String name;
+ }
+ """
+ )
+ );
+ }
+
+ @DocumentExample
+ @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540")
+ @Test
+ void addImportFromSamePackage() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ package com.acme.music;
+
+ public class Test {
+ Record record;
+ }
+ """,
+ """
+ package com.acme.music;
+
+ import com.acme.music.Record;
+
+ public class Test {
+ Record record;
+ }
+ """
+ )
+ );
+ }
+
+
+ @Test
+ void noChangeIfAlreadyFullyQualified() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ package com.acme.music;
+
+ public class Test {
+ com.acme.music.Record record;
+ }
+ """
+ )
+ );
+ }
+
+
+ @Test
+ void noChangeIfAlreadyImported() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ package com.acme.music;
+
+ import com.acme.music.Record;
+
+ public class Test {
+ Record record;
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void noImportAddedForJavaLangRecord() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ package foo.bar;
+
+ public class Test {
+ Record record;
+ }
+ """
+ )
+ );
+ }
+}