Skip to content

Commit 49ff83d

Browse files
cushonabashev
authored andcommitted
Leave a comment that opens with "/***" out of javadoc formatting
With javadoc formatting on, a banner such as /************************* * Copyright * * Some Company *************************/ was taken for javadoc because it starts with "/**": the row of asterisks moved onto a line of its own, and its paragraphs became <p> tags. The javadoc formatter has nothing to improve in a comment like that. This ports upstream commit 37171795 by cushon (google-java-format 1.8): a comment whose fourth character is another asterisk is not javadoc. That holds with javadoc formatting off as well, where it decides how blank lines around the comment are treated: a blank line between such a banner and the declaration after it is now kept, as after any other block comment, instead of being removed as it would be after javadoc. The empty() expectation changes accordingly, and a test pins the banner. Of the 15,747 files of the JDK 21 sources, 43 format differently, by 70 blank lines more in all and no other change; every one of them is in the input, after a banner comment. The four of those files that change again on a second run did so on main already.
1 parent 746fb60 commit 49ff83d

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ public boolean isSlashStarComment() {
154154

155155
@Override
156156
public boolean isJavadocComment() {
157-
return text.startsWith("/**") && text.length() > 4;
157+
// comments like `/***` are also javadoc, but their formatting probably won't be improved
158+
// by the javadoc formatter
159+
return text.startsWith("/**") && text.charAt("/**".length()) != '*' && text.length() > 4;
158160
}
159161

160162
@Override

‎open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,28 @@ public void empty() {
5555
"class Test {}",
5656
};
5757
String[] expected = {
58-
"/** */", "class Test {}",
58+
"/***/", "class Test {}",
5959
};
6060
doFormatTest(input, expected);
6161
}
6262

63+
@Test
64+
public void bannerCommentIsNotJavadoc() {
65+
// A comment that opens with three or more asterisks is left to the plain comment handling: reflowing it as
66+
// javadoc would push the asterisks onto a line of their own and turn its paragraphs into <p> tags.
67+
String[] input = {
68+
"/****************************************",
69+
" * Copyright",
70+
" *",
71+
" * Some",
72+
" *",
73+
" * Company",
74+
" ****************************************/",
75+
"class Test {}",
76+
};
77+
doFormatTest(input, input);
78+
}
79+
6380
@Test
6481
public void emptyMultipleLines() {
6582
String[] input = {

0 commit comments

Comments
 (0)