diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeMethodAccessLevelTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeMethodAccessLevelTest.java index 3cb2e3c65a..abc7f028cc 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeMethodAccessLevelTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeMethodAccessLevelTest.java @@ -427,4 +427,32 @@ static void aMethod(Double d) { ) ); } + + @Test + void publicToPackagePrivateMovesLineComment() { + rewriteRun( + spec -> spec.recipe(new ChangeMethodAccessLevel("com.abc.DatabaseConfiguration dataSource(..)", "package", null)), + java( + """ + package com.abc; + + class DatabaseConfiguration { + @SuppressWarnings("ALL") + // comments + public static void dataSource() { + } + } + """, + """ + package com.abc; + + class DatabaseConfiguration { + @SuppressWarnings("ALL") // comments + static void dataSource() { + } + } + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodAccessLevelVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodAccessLevelVisitor.java index 63b00787e8..7d432db063 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodAccessLevelVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodAccessLevelVisitor.java @@ -30,6 +30,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import static java.util.Collections.emptyList; @@ -104,16 +105,32 @@ else if (currentMethodAccessLevel == null) { } // If target access level is package-private (no modifier), remove the current access level modifier - // and copy any associated comments + // and copy any associated comments and space else if (newAccessLevel == null) { final List modifierComments = new ArrayList<>(); + final AtomicReference<@Nullable Space> removedModifierPrefix = new AtomicReference<>(null); List modifiers = ListUtils.map(m.getModifiers(), mod -> { if (mod.getType() == currentMethodAccessLevel) { modifierComments.addAll(mod.getComments()); + removedModifierPrefix.set(mod.getPrefix()); return null; } - // copy access level modifier comment to next modifier if it exists + Space removedSpace = removedModifierPrefix.getAndSet(null); + if (removedSpace != null) { + J.Modifier nextModifier = mod.withPrefix(mod.getPrefix().withComments( + ListUtils.concatAll(removedSpace.getComments(), mod.getComments()))); + + // Also handle modifier's own comments if any + if (!modifierComments.isEmpty()) { + nextModifier = nextModifier.withComments(ListUtils.concatAll(new ArrayList<>(modifierComments), mod.getComments())); + modifierComments.clear(); + } + + return nextModifier; + } + + // copy access level modifier comment to next modifier if it exists (fallback) if (!modifierComments.isEmpty()) { J.Modifier nextModifier = mod.withComments(ListUtils.concatAll(new ArrayList<>(modifierComments), mod.getComments())); modifierComments.clear();