Improve javadoc cleanup to remove duplicate spaces

Improve `TypeUtils` so that repeated space chars are removed.

Fixes gh-40593
This commit is contained in:
Phillip Webb
2024-05-01 22:35:28 -07:00
parent 72925e7a17
commit ff5c2a2351
4 changed files with 94 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -67,8 +67,6 @@ class TypeUtils {
private static final Map<String, TypeKind> WRAPPER_TO_PRIMITIVE;
private static final Pattern NEW_LINE_PATTERN = Pattern.compile("[\r\n]+");
static {
Map<String, TypeKind> primitives = new HashMap<>();
PRIMITIVE_WRAPPERS.forEach((kind, wrapperClass) -> primitives.put(wrapperClass.getName(), kind));
@@ -183,9 +181,7 @@ class TypeUtils {
return getJavaDoc((RecordComponentElement) element);
}
String javadoc = (element != null) ? this.env.getElementUtils().getDocComment(element) : null;
if (javadoc != null) {
javadoc = NEW_LINE_PATTERN.matcher(javadoc).replaceAll("").trim();
}
javadoc = (javadoc != null) ? cleanupJavaDoc(javadoc) : null;
return (javadoc == null || javadoc.isEmpty()) ? null : javadoc;
}
@@ -259,7 +255,7 @@ class TypeUtils {
Pattern paramJavadocPattern = paramJavadocPattern(recordComponent.getSimpleName().toString());
Matcher paramJavadocMatcher = paramJavadocPattern.matcher(recordJavadoc);
if (paramJavadocMatcher.find()) {
String paramJavadoc = NEW_LINE_PATTERN.matcher(paramJavadocMatcher.group()).replaceAll("").trim();
String paramJavadoc = cleanupJavaDoc(paramJavadocMatcher.group());
return paramJavadoc.isEmpty() ? null : paramJavadoc;
}
}
@@ -271,6 +267,20 @@ class TypeUtils {
return Pattern.compile(pattern, Pattern.DOTALL);
}
private String cleanupJavaDoc(String javadoc) {
StringBuilder result = new StringBuilder(javadoc.length());
char lastChar = '.';
for (int i = 0; i < javadoc.length(); i++) {
char ch = javadoc.charAt(i);
boolean repeatedSpace = ch == ' ' && lastChar == ' ';
if (ch != '\r' && ch != '\n' && !repeatedSpace) {
result.append(ch);
lastChar = ch;
}
}
return result.toString().trim();
}
/**
* A visitor that extracts the fully qualified name of a type, including generic
* information.