Skip to content

Commit

Permalink
Apply formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Dec 15, 2024
1 parent 602e0fa commit 1911398
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class Foo {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540")
@Test
void forceImportNoJavaRecord2() {
Expand All @@ -232,7 +233,7 @@ class Foo {
import com.acme.bank.*;
import com.acme.bank.Record;
class Foo {
}
""",
Expand Down
25 changes: 12 additions & 13 deletions rewrite-java/src/main/java/org/openrewrite/java/AddImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -143,18 +143,17 @@ 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() &&
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))
Expand Down

0 comments on commit 1911398

Please sign in to comment.