diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java index b824ef20ae0..8ee56e1d780 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java @@ -191,7 +191,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor private void processTypeElement(String prefix, TypeElement element, ExecutableElement source) { - TypeElementMembers members = new TypeElementMembers(this.processingEnv, this.fieldValuesParser, element); + TypeElementMembers members = new TypeElementMembers(this.processingEnv, + this.fieldValuesParser, element); Map fieldValues = members.getFieldValues(); processSimpleTypes(prefix, element, source, members, fieldValues); processSimpleLombokTypes(prefix, element, source, members, fieldValues); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java index 466d356ca7a..7d6c662f106 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java @@ -31,7 +31,6 @@ import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.ElementFilter; -import javax.tools.Diagnostic; import org.springframework.boot.configurationprocessor.fieldvalues.FieldValuesParser; @@ -65,7 +64,6 @@ class TypeElementMembers { this.typeUtils = new TypeUtils(this.env); this.fieldValuesParser = fieldValuesParser; process(element); - processFieldValues(element); } private void process(TypeElement element) { @@ -77,6 +75,19 @@ class TypeElementMembers { .fieldsIn(element.getEnclosedElements())) { processField(field); } + try { + Map fieldValues = this.fieldValuesParser + .getFieldValues(element); + for (Map.Entry entry : fieldValues.entrySet()) { + if (!this.fieldValues.containsKey(entry.getKey())) { + this.fieldValues.put(entry.getKey(), entry.getValue()); + } + } + } + catch (Exception ex) { + // continue + } + Element superType = this.env.getTypeUtils().asElement(element.getSuperclass()); if (superType != null && superType instanceof TypeElement && !OBJECT_CLASS_NAME.equals(superType.toString())) { @@ -174,24 +185,6 @@ class TypeElementMembers { return null; } - private void processFieldValues(TypeElement element) { - try { - this.fieldValues.putAll(this.fieldValuesParser.getFieldValues(element)); - } - catch (Exception ex) { - logWarning("Could not get values for type :" + element.getSimpleName().toString()); - } - - Element superType = this.env.getTypeUtils().asElement(element.getSuperclass()); - if (superType != null && superType instanceof TypeElement && !Object.class.getName().equals(superType.toString())) { - processFieldValues((TypeElement) superType); - } - } - - private void logWarning(String message) { - this.env.getMessager().printMessage(Diagnostic.Kind.WARNING, message); - } - public Map getFieldValues() { return Collections.unmodifiableMap(this.fieldValues); } diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java index eef11d02832..bf3ffba5895 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java @@ -352,6 +352,17 @@ public class ConfigurationMetadataAnnotationProcessorTests { assertThat(metadata).isNotEqualTo(Metadata.withProperty("specific.foo")); } + @Test + public void nestedClassChildProperties() throws Exception { + ConfigurationMetadata metadata = compile(ClassWithNestedProperties.class); + assertThat(metadata).has(Metadata.withGroup("nestedChildProps") + .fromSource(ClassWithNestedProperties.NestedChildClass.class)); + assertThat(metadata).has(Metadata.withProperty("nestedChildProps.child-class-property", Integer.class) + .fromSource(ClassWithNestedProperties.NestedChildClass.class).withDefaultValue(20)); + assertThat(metadata).has(Metadata.withProperty("nestedChildProps.parent-class-property", Integer.class) + .fromSource(ClassWithNestedProperties.NestedChildClass.class).withDefaultValue(10)); + } + @Test public void builderPojo() throws IOException { ConfigurationMetadata metadata = compile(BuilderPojo.class); @@ -768,17 +779,6 @@ public class ConfigurationMetadataAnnotationProcessorTests { } } - @Test - public void nestedClassChildProperties() throws Exception { - ConfigurationMetadata metadata = compile(ClassWithNestedProperties.class); - assertThat(metadata).has(Metadata.withGroup("nestedChildProps") - .fromSource(ClassWithNestedProperties.NestedChildClass.class)); - assertThat(metadata).has(Metadata.withProperty("nestedChildProps.child-class-property", Integer.class) - .fromSource(ClassWithNestedProperties.NestedChildClass.class).withDefaultValue(20)); - assertThat(metadata).has(Metadata.withProperty("nestedChildProps.parent-class-property", Integer.class) - .fromSource(ClassWithNestedProperties.NestedChildClass.class).withDefaultValue(10)); - } - private static class AdditionalMetadata { }