diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java index 070c7d13241..ab817711a8b 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java @@ -7774,4 +7774,45 @@ public void testBug552012d() { "}"; formatSource(source); } +/** + * https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2127 + */ +public void testIssue2127a() { + setComplianceLevel(CompilerOptions.VERSION_9); + String source = """ + /** This is a header comment */ + + /** This is a module javadoc */ + module test{} + """; + formatSource(source, """ + /** This is a header comment */ + + /** This is a module javadoc */ + module test { + } + """, + CodeFormatter.K_MODULE_INFO | CodeFormatter.F_INCLUDE_COMMENTS); +} +/** + * https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2127 + */ +public void testIssue2127b() { + setComplianceLevel(CompilerOptions.VERSION_9); + this.formatterPrefs.comment_format_header = true; + String source = """ + /** This is a header comment */ + + /** This is a module javadoc */ + module test{} + """; + formatSource(source, """ + /** This is a header comment */ + + /** This is a module javadoc */ + module test { + } + """, + CodeFormatter.K_MODULE_INFO | CodeFormatter.F_INCLUDE_COMMENTS); +} } diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java index e1847e5bba3..bbe84d5bbca 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java @@ -233,10 +233,11 @@ private List prepareFormattedCode(String source, int kind) { } private void findHeader() { - if (this.astRoot instanceof CompilationUnit) { - CompilationUnit unit = (CompilationUnit) this.astRoot; + if (this.astRoot instanceof CompilationUnit unit) { List types = unit.types(); - ASTNode firstElement = types.isEmpty() ? unit.getPackage() : types.get(0); + ASTNode firstElement = !types.isEmpty() ? types.get(0) + : unit.getModule() != null ? unit.getModule() + : unit.getPackage(); if (firstElement != null) { int headerEndIndex = this.tokenManager.firstIndexIn(firstElement, -1); this.tokenManager.setHeaderEndIndex(headerEndIndex);