Skip to content

Commit 7d361de

Browse files
Laurens Westerlakentimtebeek
andauthored
Fix Java 25 javadoc parsing issue (#6370)
* Fix Java 25 javadoc parsing issue * Add testcase * Reduce test * Polish test * Apply suggestions from code review --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 52f563b commit 7d361de

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

rewrite-java-25/src/main/java/org/openrewrite/java/isolated/ReloadableJava25JavadocVisitor.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.HashMap;
4545
import java.util.List;
4646
import java.util.Map;
47+
import java.util.function.Predicate;
4748

4849
import static java.util.Collections.*;
4950
import static java.util.stream.Collectors.toList;
@@ -981,7 +982,7 @@ public List<Javadoc> visitText(String node) {
981982
// The AST contained unnecessary whitespace for Javadoc, and they got rid of this with Java 25.
982983
// So now have to manually account for this.
983984
if (i+1 <= node.length() -1 && node.charAt(i+1) != source.charAt(cursor) && Character.isWhitespace(source.charAt(cursor))) {
984-
text.append(whitespaceBeforeAsString());
985+
text.append(whitespaceBeforeAsString(Character::isSpaceChar));
985986
}
986987
}
987988

@@ -1085,13 +1086,17 @@ private List<Javadoc> sourceBefore(String delim) {
10851086
}
10861087

10871088
private String whitespaceBeforeAsString() {
1089+
return whitespaceBeforeAsString(Character::isWhitespace);
1090+
}
1091+
1092+
private String whitespaceBeforeAsString(Predicate<Character> whitespace) {
10881093
if (cursor >= source.length()) {
10891094
return "";
10901095
}
10911096

10921097
int i = cursor;
10931098
for (; i < source.length(); i++) {
1094-
if (!Character.isWhitespace(source.charAt(i))) {
1099+
if (!whitespace.test(source.charAt(i))) {
10951100
break;
10961101
}
10971102
}

rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,22 @@ public void methodC(String aString) {
22002200
);
22012201
}
22022202

2203+
@Test
2204+
void redundantSpacing() {
2205+
rewriteRun(
2206+
java(
2207+
"""
2208+
/**
2209+
* <p>Some text.
2210+
*\s
2211+
* <p>More text.
2212+
*/
2213+
class SomeClass {}
2214+
"""
2215+
)
2216+
);
2217+
}
2218+
22032219
@Issue("https://github.com/openrewrite/rewrite/issues/5855")
22042220
@Nested
22052221
class GenericWildcard {

0 commit comments

Comments
 (0)