Commit 53493fdd authored by Madhura Bhave's avatar Madhura Bhave

Make detection of nested config more lenient

Closes gh-3454
parent 2a4ba89f
......@@ -337,10 +337,25 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
if (hasAnnotation(field, nestedConfigurationPropertyAnnotation())) {
return true;
}
return this.typeUtils.isEnclosedIn(returnType, element)
return (isParentTheSame(returnType, element))
&& returnType.getKind() != ElementKind.ENUM;
}
private boolean isParentTheSame(Element returnType, TypeElement element) {
if (returnType == null || element == null) {
return false;
}
return getTopLevelType(returnType).equals(getTopLevelType(element));
}
private Element getTopLevelType(Element element) {
if ((element.getEnclosingElement() == null) ||
!(element.getEnclosingElement() instanceof TypeElement)) {
return element;
}
return getTopLevelType(element.getEnclosingElement());
}
private boolean isDeprecated(Element element) {
if (isElementDeprecated(element)) {
return true;
......
......@@ -65,6 +65,7 @@ import org.springframework.boot.configurationsample.specific.DoubleRegistrationP
import org.springframework.boot.configurationsample.specific.ExcludedTypesPojo;
import org.springframework.boot.configurationsample.specific.GenericConfig;
import org.springframework.boot.configurationsample.specific.InnerClassAnnotatedGetterConfig;
import org.springframework.boot.configurationsample.specific.InnerClassHierachicalProperties;
import org.springframework.boot.configurationsample.specific.InnerClassProperties;
import org.springframework.boot.configurationsample.specific.InnerClassRootConfig;
import org.springframework.boot.configurationsample.specific.InvalidAccessorProperties;
......@@ -348,6 +349,19 @@ public class ConfigurationMetadataAnnotationProcessorTests {
assertThat(metadata).isNotEqualTo(Metadata.withGroup("config.fourth"));
}
@Test
public void innerClassPropertiesHierachical() throws Exception {
ConfigurationMetadata metadata = compile(InnerClassHierachicalProperties.class);
assertThat(metadata)
.has(Metadata.withGroup("config.foo").ofType(InnerClassHierachicalProperties.Foo.class));
assertThat(metadata).has(
Metadata.withGroup("config.foo.bar").ofType(InnerClassHierachicalProperties.Bar.class));
assertThat(metadata).has(
Metadata.withGroup("config.foo.bar.baz").ofType(InnerClassHierachicalProperties.Foo.Baz.class));
assertThat(metadata).has(Metadata.withProperty("config.foo.bar.baz.blah"));
assertThat(metadata).has(Metadata.withProperty("config.foo.bar.bling"));
}
@Test
public void innerClassAnnotatedGetterConfig() throws Exception {
ConfigurationMetadata metadata = compile(InnerClassAnnotatedGetterConfig.class);
......
/*
* Copyright 2012-2017 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
*
* http://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.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Demonstrate inner classes end up in metadata regardless of
* position in hierarchy and without the use of
* {@link org.springframework.boot.configurationsample.NestedConfigurationProperty}.
*
* @author Madhura Bhave
*/
@ConfigurationProperties(prefix = "config")
public class InnerClassHierachicalProperties {
private Foo foo;
public Foo getFoo() {
return this.foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
public static class Foo {
private Bar bar;
public Bar getBar() {
return this.bar;
}
public void setBar(Bar bar) {
this.bar = bar;
}
public static class Baz {
private String blah;
public String getBlah() {
return this.blah;
}
public void setBlah(String blah) {
this.blah = blah;
}
}
}
public static class Bar {
private String bling;
private Foo.Baz baz;
public String getBling() {
return this.bling;
}
public void setBling(String foo) {
this.bling = foo;
}
public Foo.Baz getBaz() {
return this.baz;
}
public void setBaz(Foo.Baz baz) {
this.baz = baz;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment