Skip to content

Commit

Permalink
force Import No Java Record
Browse files Browse the repository at this point in the history
  • Loading branch information
BramliAK committed Dec 15, 2024
1 parent 601fec7 commit 602e0fa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions rewrite-java/src/main/java/org/openrewrite/java/AddImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -143,8 +143,8 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String

List<JRightPadded<J.Import>> 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();
Expand All @@ -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))
Expand Down Expand Up @@ -183,6 +182,10 @@ 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 602e0fa

Please sign in to comment.