From 602e0fa3fcbe97e57c3596cec0c6adabfe829082 Mon Sep 17 00:00:00 2001 From: bramli ahmed khalil Date: Sun, 15 Dec 2024 18:09:52 +0100 Subject: [PATCH 1/3] force Import No Java Record --- .../org/openrewrite/java/AddImportTest.java | 30 +++++++++++++++++++ .../java/org/openrewrite/java/AddImport.java | 15 ++++++---- 2 files changed, 39 insertions(+), 6 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 ac15fd5da84..ae6ac0bbb9f 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 @@ -210,6 +210,36 @@ class Foo { ) ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540") + @Test + void forceImportNoJavaRecord2() { + // 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; + + import com.acme.bank.*; + + class Foo { + } + """, + """ + package com.acme.bank; + + import 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 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 4299ef1dd08..a505c5fd0a1 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java @@ -110,7 +110,7 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String 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 + if (!isRecord() && // Record's late addition to `java.lang` might conflict with user class cu.getPackageDeclaration() != null && packageName.equals(cu.getPackageDeclaration().getExpression().printTrimmed(getCursor()))) { return cu; @@ -123,10 +123,10 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String if (cu.getImports().stream().anyMatch(i -> { String ending = i.getQualid().getSimpleName(); if (member == null) { - return !i.isStatic() && i.getPackageName().equals(packageName) && + return !isRecord() && !i.isStatic() && i.getPackageName().equals(packageName) && (ending.equals(typeName) || "*".equals(ending)); } - return i.isStatic() && i.getTypeName().equals(fullyQualifiedName) && + return !isRecord() && i.isStatic() && i.getTypeName().equals(fullyQualifiedName) && (ending.equals(member) || "*".equals(ending)); })) { return cu; @@ -143,8 +143,8 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String List> imports = new ArrayList<>(cu.getPadding().getImports()); - if (imports.isEmpty() && !cu.getClasses().isEmpty()) { - if (cu.getPackageDeclaration() == null) { + if (imports.isEmpty() && !cu.getClasses().isEmpty() && + cu.getPackageDeclaration() == null) { // leave javadocs on the class and move other comments up to the import // (which could include license headers and the like) Space firstClassPrefix = cu.getClasses().get(0).getPrefix(); @@ -155,7 +155,6 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String cu = cu.withClasses(ListUtils.mapFirst(cu.getClasses(), clazz -> clazz.withComments(ListUtils.map(clazz.getComments(), comment -> comment instanceof Javadoc ? comment : null)) )); - } } ImportLayoutStyle layoutStyle = Optional.ofNullable(((SourceFile) cu).getStyle(ImportLayoutStyle.class)) @@ -183,6 +182,10 @@ 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)); From 1911398320dac2999de97fb4b3d1abced91706fd Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 15 Dec 2024 19:36:26 +0100 Subject: [PATCH 2/3] Apply formatter --- .../org/openrewrite/java/AddImportTest.java | 3 ++- .../java/org/openrewrite/java/AddImport.java | 25 +++++++++---------- 2 files changed, 14 insertions(+), 14 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 ae6ac0bbb9f..d028fabfa09 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 @@ -210,6 +210,7 @@ class Foo { ) ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540") @Test void forceImportNoJavaRecord2() { @@ -232,7 +233,7 @@ class Foo { import com.acme.bank.*; import com.acme.bank.Record; - + class Foo { } """, 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 a505c5fd0a1..e7396ca21e3 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java @@ -126,7 +126,7 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String return !isRecord() && !i.isStatic() && i.getPackageName().equals(packageName) && (ending.equals(typeName) || "*".equals(ending)); } - return !isRecord() && i.isStatic() && i.getTypeName().equals(fullyQualifiedName) && + return !isRecord() && i.isStatic() && i.getTypeName().equals(fullyQualifiedName) && (ending.equals(member) || "*".equals(ending)); })) { return cu; @@ -143,18 +143,17 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String List> imports = new ArrayList<>(cu.getPadding().getImports()); - if (imports.isEmpty() && !cu.getClasses().isEmpty() && - cu.getPackageDeclaration() == null) { - // leave javadocs on the class and move other comments up to the import - // (which could include license headers and the like) - Space firstClassPrefix = cu.getClasses().get(0).getPrefix(); - importToAdd = importToAdd.withPrefix(firstClassPrefix - .withComments(ListUtils.map(firstClassPrefix.getComments(), comment -> comment instanceof Javadoc ? null : comment)) - .withWhitespace("")); - - cu = cu.withClasses(ListUtils.mapFirst(cu.getClasses(), clazz -> - clazz.withComments(ListUtils.map(clazz.getComments(), comment -> comment instanceof Javadoc ? comment : null)) - )); + if (imports.isEmpty() && !cu.getClasses().isEmpty() && cu.getPackageDeclaration() == null) { + // leave javadocs on the class and move other comments up to the import + // (which could include license headers and the like) + Space firstClassPrefix = cu.getClasses().get(0).getPrefix(); + importToAdd = importToAdd.withPrefix(firstClassPrefix + .withComments(ListUtils.map(firstClassPrefix.getComments(), comment -> comment instanceof Javadoc ? null : comment)) + .withWhitespace("")); + + cu = cu.withClasses(ListUtils.mapFirst(cu.getClasses(), clazz -> + clazz.withComments(ListUtils.map(clazz.getComments(), comment -> comment instanceof Javadoc ? comment : null)) + )); } ImportLayoutStyle layoutStyle = Optional.ofNullable(((SourceFile) cu).getStyle(ImportLayoutStyle.class)) From 223f622ad54ebd8c7cf6baad7be3ad51445dfae2 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 15 Dec 2024 19:46:35 +0100 Subject: [PATCH 3/3] 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));