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.

View File

@ -521,7 +521,7 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
void recordPropertiesWithDescriptions() {
ConfigurationMetadata metadata = compile(ExampleRecord.class);
assertThat(metadata).has(Metadata.withProperty("record.descriptions.some-string", String.class)
.withDescription("very long description that doesn't fit single line"));
.withDescription("very long description that doesn't fit single line and is indented"));
assertThat(metadata).has(Metadata.withProperty("record.descriptions.some-integer", Integer.class)
.withDescription("description with @param and @ pitfalls"));
assertThat(metadata).has(Metadata.withProperty("record.descriptions.some-boolean", Boolean.class)

View File

@ -16,10 +16,13 @@
package org.springframework.boot.configurationsample.record;
// @formatter:off
/**
* Example Record Javadoc sample
*
* @param someString very long description that doesn't fit single line
* @param someString very long description that
* doesn't fit single line and is indented
* @param someInteger description with @param and @ pitfalls
* @param someBoolean description with extra spaces
* @param someLong description without space after asterisk
@ -30,3 +33,5 @@ package org.springframework.boot.configurationsample.record;
@org.springframework.boot.configurationsample.ConfigurationProperties("record.descriptions")
public record ExampleRecord(String someString, Integer someInteger, Boolean someBoolean, Long someLong, Byte someByte) {
}
//@formatter:on

View File

@ -0,0 +1,70 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.properties;
/**
* @author pwebb
*/
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.support.TestPropertySourceUtils;
public class TempTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Test
void testName() {
load(MyConfig.class, "foo.bar.baz=hello");
System.out.println(this.context.getBean(Foo.class));
}
@Test
void testName2() {
load(MyConfig.class);
System.out.println(this.context.getBean(Foo.class));
}
private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
return load(new Class<?>[] { configuration }, inlinedProperties);
}
private AnnotationConfigApplicationContext load(Class<?>[] configuration, String... inlinedProperties) {
this.context.register(configuration);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, inlinedProperties);
this.context.refresh();
return this.context;
}
@Configuration
@EnableConfigurationProperties(Foo.class)
static class MyConfig {
}
@ConfigurationProperties("foo")
record Foo(@DefaultValue Bar bar) {
}
record Bar(@DefaultValue("hello") String baz) {
}
}