diff --git a/rewrite-core/build.gradle.kts b/rewrite-core/build.gradle.kts index ef8625e288e..e3a59325a5f 100644 --- a/rewrite-core/build.gradle.kts +++ b/rewrite-core/build.gradle.kts @@ -15,10 +15,6 @@ dependencies { api("org.jspecify:jspecify:latest.release") - // Pinning okhttp while waiting on 5.0.0 - // https://github.com/openrewrite/rewrite/issues/1479 - compileOnly("com.squareup.okhttp3:okhttp:4.9.3") - implementation("org.apache.commons:commons-compress:latest.release") implementation("io.micrometer:micrometer-core:1.9.+") diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddImportTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddImportTest.java index d82981f8863..ac15fd5da84 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/AddImportTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddImportTest.java @@ -184,6 +184,51 @@ class A { ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540") + @Test + void forceImportNoJavaRecord() { + // Add import for a class named `Record`, even within the same package, to avoid conflicts with java.lang.Record + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new AddImport<>("com.acme.bank.Record", null, false))), + //language=java + java( + """ + package com.acme.bank; + + class Foo { + } + """, + """ + package com.acme.bank; + + import com.acme.bank.Record; + + class Foo { + } + """, + spec -> spec.markers(javaVersion(11)) + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540") + @Test + void notForceImportJavaRecord() { + // Do not add import for java.lang.Record by default + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new AddImport<>("java.lang.Record", null, false))), + //language=java + java( + """ + package com.acme.bank; + + class Foo { + } + """, + spec -> spec.markers(javaVersion(11)) + ) + ); + } @Test void dontImportJavaLang() { rewriteRun( diff --git a/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java b/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java index 9aecd66f89f..4299ef1dd08 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java @@ -105,9 +105,14 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String return cu; } - // No need to add imports if the class to import is in java.lang, or if the classes are within the same package - if (("java.lang".equals(packageName) && StringUtils.isBlank(member)) || (cu.getPackageDeclaration() != null && - packageName.equals(cu.getPackageDeclaration().getExpression().printTrimmed(getCursor())))) { + // No need to add imports if the class to import is in java.lang + if ("java.lang".equals(packageName) && StringUtils.isBlank(member)) { + return cu; + } + // Nor if the classes are within the same package + if (!"Record".equals(typeName) && // Record's late addition to `java.lang` might conflict with user class + cu.getPackageDeclaration() != null && + packageName.equals(cu.getPackageDeclaration().getExpression().printTrimmed(getCursor()))) { return cu; } diff --git a/rewrite-maven/build.gradle.kts b/rewrite-maven/build.gradle.kts index d6efe37c616..ac44b44e905 100755 --- a/rewrite-maven/build.gradle.kts +++ b/rewrite-maven/build.gradle.kts @@ -35,6 +35,8 @@ dependencies { implementation("org.apache.commons:commons-text:latest.release") testImplementation(project(":rewrite-test")) + + testImplementation("com.squareup.okhttp3:okhttp:4.+") testImplementation("com.squareup.okhttp3:mockwebserver:4.+") testImplementation("com.squareup.okhttp3:okhttp-tls:4.+") testImplementation("com.squareup.okio:okio-jvm:3.9.1") diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java index b3cbe440c88..cef61f90ef3 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java @@ -31,7 +31,7 @@ import org.openrewrite.Issue; import org.openrewrite.ParseExceptionResult; import org.openrewrite.Parser; -import org.openrewrite.ipc.http.OkHttpSender; +import org.openrewrite.maven.http.OkHttpSender; import org.openrewrite.maven.internal.MavenParsingException; import org.openrewrite.maven.tree.*; import org.openrewrite.test.RewriteTest; diff --git a/rewrite-core/src/main/java/org/openrewrite/ipc/http/OkHttpSender.java b/rewrite-maven/src/test/java/org/openrewrite/maven/http/OkHttpSender.java similarity index 97% rename from rewrite-core/src/main/java/org/openrewrite/ipc/http/OkHttpSender.java rename to rewrite-maven/src/test/java/org/openrewrite/maven/http/OkHttpSender.java index eb42f70c913..e92a9359c1d 100644 --- a/rewrite-core/src/main/java/org/openrewrite/ipc/http/OkHttpSender.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/http/OkHttpSender.java @@ -13,12 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openrewrite.ipc.http; +package org.openrewrite.maven.http; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.RequestBody; import okhttp3.ResponseBody; +import org.openrewrite.ipc.http.HttpSender; import java.io.IOException; import java.io.UncheckedIOException; diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java index 152265c02fd..4c19fc87f33 100755 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java @@ -33,7 +33,7 @@ import org.openrewrite.*; import org.openrewrite.ipc.http.HttpSender; import org.openrewrite.ipc.http.HttpUrlConnectionSender; -import org.openrewrite.ipc.http.OkHttpSender; +import org.openrewrite.maven.http.OkHttpSender; import org.openrewrite.maven.MavenDownloadingException; import org.openrewrite.maven.MavenExecutionContextView; import org.openrewrite.maven.MavenParser;