From 43f834da04698f7a668f25e037f072229582ef93 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 8 Oct 2026 21:29:57 +0300 Subject: [PATCH] Strip trailing asterisks before `*/` in Javadoc comments. Decorative closing delimiters such as `/** ... **/` previously had the trailing asterisk retained as comment body content, resulting in `/** ... * */`. Ported from google/google-java-format#1474 (commit 2957b192). In this fork stripJavadocBeginAndEnd lives in JavadocLexer; the tests were written first and failed with `* */` before the change. --- .../javaformat/java/javadoc/JavadocLexer.java | 6 +++- .../java/JavadocFormattingTest.java | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocLexer.java b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocLexer.java index 78116960a..a4e61b40f 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocLexer.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocLexer.java @@ -92,7 +92,11 @@ private static String stripJavadocBeginAndEnd(String input) { */ checkArgument(input.startsWith("/**"), "Missing /**: %s", input); checkArgument(input.endsWith("*/") && input.length() > 4, "Missing */: %s", input); - return input.substring("/**".length(), input.length() - "*/".length()); + int end = input.length() - "*/".length(); + while (end > "/**".length() && input.charAt(end - 1) == '*') { + end--; + } + return input.substring("/**".length(), end); } private final CharStream input; diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java index da51929fe..bdc14d98f 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java @@ -570,6 +570,35 @@ void significantAsterisks() { doFormatTest(input, expected); } + @Test + void trailingAsterisksBeforeClose() { + String[] input = { + "/** Version information for all the QuotaKeys that we have data for **/", // + "class Test {}", + }; + String[] expected = { + "/** Version information for all the QuotaKeys that we have data for */", // + "class Test {}", + }; + doFormatTest(input, expected); + } + + @Test + void trailingAsterisksOnTheirOwnLine() { + String[] input = { + "/**", // + " * Summary line", + " * continues here.", + " ***/", + "class Test {}", + }; + String[] expected = { + "/** Summary line continues here. */", // + "class Test {}", + }; + doFormatTest(input, expected); + } + @Test void links() { String[] input = {