Skip to content

Commit

Permalink
Minor polish
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Dec 15, 2024
1 parent 1911398 commit 223f622
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand All @@ -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(
"""
Expand Down
13 changes: 4 additions & 9 deletions rewrite-java/src/main/java/org/openrewrite/java/AddImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -181,10 +180,6 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String
return j;
}

private boolean isRecord() {
return "Record".equals(typeName);
}

private List<JRightPadded<J.Import>> checkCRLF(JavaSourceFile cu, List<JRightPadded<J.Import>> newImports) {
GeneralFormatStyle generalFormatStyle = Optional.ofNullable(((SourceFile) cu).getStyle(GeneralFormatStyle.class))
.orElse(autodetectGeneralFormatStyle(cu));
Expand Down

0 comments on commit 223f622

Please sign in to comment.