Allow @DefaultValue to be used on record components

See gh-29010
This commit is contained in:
Pavel Anisimov
2021-12-10 19:48:05 +03:00
committed by Andy Wilkinson
parent 1793cee00f
commit 976ed90cd7
4 changed files with 62 additions and 2 deletions

View File

@@ -74,6 +74,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
* @author Andy Wilkinson
* @author Kris De Volder
* @author Jonas Keßler
* @author Pavel Anisimov
*/
class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGenerationTests {
@@ -445,4 +446,24 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
assertThat(metadata).doesNotHave(Metadata.withProperty("multi.some-integer"));
}
@Test
@EnabledForJreRange(min = JRE.JAVA_16)
void recordPropertiesWithDefaultValues(@TempDir File temp) throws IOException {
File exampleRecord = new File(temp, "ExampleRecord.java");
try (PrintWriter writer = new PrintWriter(new FileWriter(exampleRecord))) {
writer.println(
"@org.springframework.boot.configurationsample.ConfigurationProperties(\"record.defaults\")");
writer.println("public record ExampleRecord(");
writer.println("@org.springframework.boot.configurationsample.DefaultValue(\"An1s9n\") String someString,");
writer.println("@org.springframework.boot.configurationsample.DefaultValue(\"594\") Integer someInteger");
writer.println(") {");
writer.println("}");
}
ConfigurationMetadata metadata = compile(exampleRecord);
assertThat(metadata)
.has(Metadata.withProperty("record.defaults.some-string", String.class).withDefaultValue("An1s9n"));
assertThat(metadata)
.has(Metadata.withProperty("record.defaults.some-integer", Integer.class).withDefaultValue(594));
}
}

View File

@@ -27,8 +27,9 @@ import java.lang.annotation.Target;
* dependency on the real annotation).
*
* @author Stephane Nicoll
* @author Pavel Anisimov
*/
@Target({ ElementType.PARAMETER })
@Target({ ElementType.PARAMETER, ElementType.RECORD_COMPONENT })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DefaultValue {