From afac8dd8af1ed06e48bb79c5eb334a4d797ba1ae Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 30 Sep 2022 11:58:14 -0700 Subject: [PATCH] Refine SourceFile record to class conversion Update conversion logic to deal with annotations. See gh-29236 --- .../core/test/tools/SourceFile.java | 27 ++++++++++++++++++- .../core/test/tools/SourceFileTests.java | 13 +++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/spring-core-test/src/main/java/org/springframework/core/test/tools/SourceFile.java b/spring-core-test/src/main/java/org/springframework/core/test/tools/SourceFile.java index edf7191df3..91f7da6f8b 100644 --- a/spring-core-test/src/main/java/org/springframework/core/test/tools/SourceFile.java +++ b/spring-core-test/src/main/java/org/springframework/core/test/tools/SourceFile.java @@ -22,6 +22,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.StringReader; import java.nio.charset.StandardCharsets; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.thoughtworks.qdox.JavaProjectBuilder; import com.thoughtworks.qdox.model.JavaClass; @@ -189,7 +191,30 @@ public final class SourceFile extends DynamicFile implements AssertProvider 0) { + if (ch == '(') { + parenthesesCount++; + } + else if (ch == ')') { + parenthesesCount--; + } + } + else { + result.append(ch); + } + } + return makeRecordsLookLikeClasses(result.toString()); + } + return content; } /** diff --git a/spring-core-test/src/test/java/org/springframework/core/test/tools/SourceFileTests.java b/spring-core-test/src/test/java/org/springframework/core/test/tools/SourceFileTests.java index a9be26b1d7..639d9ecf24 100644 --- a/spring-core-test/src/test/java/org/springframework/core/test/tools/SourceFileTests.java +++ b/spring-core-test/src/test/java/org/springframework/core/test/tools/SourceFileTests.java @@ -146,6 +146,19 @@ class SourceFileTests { assertThat(sourceFile.getClassName()).isEqualTo("com.example.helloworld.HelloWorld"); } + @Test + void getClassNameFromAnnotatedRecord() { + SourceFile sourceFile = SourceFile.of(""" + package com.example; + + public record RecordProperties( + @org.springframework.boot.context.properties.bind.DefaultValue("default-value-1") String property1, + @org.springframework.boot.context.properties.bind.DefaultValue("default-value-2") String property2) { + } + """); + assertThat(sourceFile.getClassName()).isEqualTo("com.example.RecordProperties"); + } + /** * JavaPoet style API with a {@code writeTo} method. */