From 223f622ad54ebd8c7cf6baad7be3ad51445dfae2 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 15 Dec 2024 19:46:35 +0100 Subject: [PATCH] Minor polish --- .../java/org/openrewrite/java/AddImportTest.java | 10 ++++++---- .../main/java/org/openrewrite/java/AddImport.java | 13 ++++--------- 2 files changed, 10 insertions(+), 13 deletions(-) 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 d028fabfa09..96a4a160793 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 @@ -186,10 +186,11 @@ class A { @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540") @Test - void forceImportNoJavaRecord() { + void forceImportNonJavaLangRecord() { // 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))), + spec -> spec.recipe(toRecipe(() -> new AddImport<>("com.acme.bank.Record", null, false))) + .parser(JavaParser.fromJavaVersion().dependsOn("package com.acme.bank; public class Record {}")), //language=java java( """ @@ -213,10 +214,11 @@ class Foo { @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540") @Test - void forceImportNoJavaRecord2() { + void forceImportNonJavaLangRecordFromWildcardImport() { // 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))), + spec -> spec.recipe(toRecipe(() -> new AddImport<>("com.acme.bank.Record", null, false))) + .parser(JavaParser.fromJavaVersion().dependsOn("package com.acme.bank; public class Record {}")), //language=java java( """ 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 e7396ca21e3..2e23df27adb 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java @@ -110,8 +110,7 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String return cu; } // Nor if the classes are within the same package - if (!isRecord() && // Record's late addition to `java.lang` might conflict with user class - cu.getPackageDeclaration() != null && + if (!"Record".equals(typeName) && cu.getPackageDeclaration() != null && packageName.equals(cu.getPackageDeclaration().getExpression().printTrimmed(getCursor()))) { return cu; } @@ -120,13 +119,13 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String return cu; } - if (cu.getImports().stream().anyMatch(i -> { + if (!"Record".equals(typeName) && cu.getImports().stream().anyMatch(i -> { String ending = i.getQualid().getSimpleName(); if (member == null) { - return !isRecord() && !i.isStatic() && i.getPackageName().equals(packageName) && + return !i.isStatic() && i.getPackageName().equals(packageName) && (ending.equals(typeName) || "*".equals(ending)); } - return !isRecord() && i.isStatic() && i.getTypeName().equals(fullyQualifiedName) && + return i.isStatic() && i.getTypeName().equals(fullyQualifiedName) && (ending.equals(member) || "*".equals(ending)); })) { return cu; @@ -181,10 +180,6 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String return j; } - private boolean isRecord() { - return "Record".equals(typeName); - } - private List> checkCRLF(JavaSourceFile cu, List> newImports) { GeneralFormatStyle generalFormatStyle = Optional.ofNullable(((SourceFile) cu).getStyle(GeneralFormatStyle.class)) .orElse(autodetectGeneralFormatStyle(cu));