Skip to content

Commit

Permalink
Merge branch 'main' into fix-groovy-multivariable-declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Dec 15, 2024
2 parents 7cd916b + 601fec7 commit b371832
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
4 changes: 0 additions & 4 deletions rewrite-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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.+")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 8 additions & 3 deletions rewrite-java/src/main/java/org/openrewrite/java/AddImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions rewrite-maven/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b371832

Please sign in to comment.