Skip to content

Commit

Permalink
Improve javadoc cleanup to remove duplicate spaces
Browse files Browse the repository at this point in the history
Improve `TypeUtils` so that repeated space chars are removed.

Fixes gh-40593
  • Loading branch information
philwebb committed May 2, 2024
1 parent 72925e7 commit ff5c2a2
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -183,9 +181,7 @@ String getJavaDoc(Element element) {
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;
}

Expand Down Expand Up @@ -259,7 +255,7 @@ private String getJavaDoc(RecordComponentElement recordComponent) {
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;
}
}
Expand All @@ -271,6 +267,20 @@ private Pattern paramJavadocPattern(String paramName) {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ void shouldNotMarkDbcp2UsernameOrPasswordAsDeprecated() {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,3 +33,5 @@
@org.springframework.boot.configurationsample.ConfigurationProperties("record.descriptions")
public record ExampleRecord(String someString, Integer someInteger, Boolean someBoolean, Long someLong, Byte someByte) {
}

//@formatter:on
Original file line number Diff line number Diff line change
@@ -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) {
}

}

0 comments on commit ff5c2a2

Please sign in to comment.