Skip to content

Commit 681df90

Browse files
committed
Generate metadata for records using implicit constructor binding
Closes gh-27216
1 parent a5656e0 commit 681df90

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -197,7 +197,8 @@ static ConfigurationPropertiesTypeElement of(TypeElement type, MetadataGeneratio
197197
}
198198

199199
private static boolean isConstructorBoundType(TypeElement type, MetadataGenerationEnvironment env) {
200-
if (env.hasConstructorBindingAnnotation(type)) {
200+
if (env.hasConstructorBindingAnnotation(type)
201+
|| "java.lang.Record".equals(type.getSuperclass().toString())) {
201202
return true;
202203
}
203204
if (type.getNestingKind() == NestingKind.MEMBER) {

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/AbstractMetadataGenerationTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.util.Arrays;
2122

2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.io.TempDir;
@@ -54,4 +55,11 @@ protected ConfigurationMetadata compile(Class<?>... types) {
5455
return processor.getMetadata();
5556
}
5657

58+
protected ConfigurationMetadata compile(File... sources) {
59+
TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor(
60+
this.compiler.getOutputLocation());
61+
this.compiler.getTask(Arrays.asList(sources)).call(processor);
62+
return processor.getMetadata();
63+
}
64+
5765
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616

1717
package org.springframework.boot.configurationprocessor;
1818

19+
import java.io.File;
20+
import java.io.FileWriter;
21+
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
1924
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.condition.EnabledForJreRange;
26+
import org.junit.jupiter.api.condition.JRE;
27+
import org.junit.jupiter.api.io.TempDir;
2028

2129
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
2230
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
@@ -402,4 +410,54 @@ void recursivePropertiesDoNotCauseAStackOverflow() {
402410
compile(RecursiveProperties.class);
403411
}
404412

413+
@Test
414+
@EnabledForJreRange(min = JRE.JAVA_16)
415+
void explicityBoundRecordProperties(@TempDir File temp) throws IOException {
416+
File exampleRecord = new File(temp, "ExampleRecord.java");
417+
try (PrintWriter writer = new PrintWriter(new FileWriter(exampleRecord))) {
418+
writer.println("@org.springframework.boot.configurationsample.ConstructorBinding");
419+
writer.println("@org.springframework.boot.configurationsample.ConfigurationProperties(\"explicit\")");
420+
writer.println("public record ExampleRecord(String someString, Integer someInteger) {");
421+
writer.println("}");
422+
}
423+
ConfigurationMetadata metadata = compile(exampleRecord);
424+
assertThat(metadata).has(Metadata.withProperty("explicit.some-string"));
425+
assertThat(metadata).has(Metadata.withProperty("explicit.some-integer"));
426+
}
427+
428+
@Test
429+
@EnabledForJreRange(min = JRE.JAVA_16)
430+
void implicitlyBoundRecordProperties(@TempDir File temp) throws IOException {
431+
File exampleRecord = new File(temp, "ExampleRecord.java");
432+
try (PrintWriter writer = new PrintWriter(new FileWriter(exampleRecord))) {
433+
writer.println("@org.springframework.boot.configurationsample.ConfigurationProperties(\"implicit\")");
434+
writer.println("public record ExampleRecord(String someString, Integer someInteger) {");
435+
writer.println("}");
436+
}
437+
ConfigurationMetadata metadata = compile(exampleRecord);
438+
assertThat(metadata).has(Metadata.withProperty("implicit.some-string"));
439+
assertThat(metadata).has(Metadata.withProperty("implicit.some-integer"));
440+
}
441+
442+
@Test
443+
@EnabledForJreRange(min = JRE.JAVA_16)
444+
void multiConstructorRecordProperties(@TempDir File temp) throws IOException {
445+
File exampleRecord = new File(temp, "ExampleRecord.java");
446+
try (PrintWriter writer = new PrintWriter(new FileWriter(exampleRecord))) {
447+
writer.println("@org.springframework.boot.configurationsample.ConfigurationProperties(\"multi\")");
448+
writer.println("public record ExampleRecord(String someString, Integer someInteger) {");
449+
writer.println(" @org.springframework.boot.configurationsample.ConstructorBinding");
450+
writer.println(" public ExampleRecord(String someString) {");
451+
writer.println(" this(someString, 42);");
452+
writer.println(" }");
453+
writer.println(" public ExampleRecord(Integer someInteger) {");
454+
writer.println(" this(\"someString\", someInteger);");
455+
writer.println(" }");
456+
writer.println("}");
457+
}
458+
ConfigurationMetadata metadata = compile(exampleRecord);
459+
assertThat(metadata).has(Metadata.withProperty("multi.some-string"));
460+
assertThat(metadata).doesNotHave(Metadata.withProperty("multi.some-integer"));
461+
}
462+
405463
}

0 commit comments

Comments
 (0)