Commit e56a1ba5 authored by Phillip Webb's avatar Phillip Webb

Refine inner class detection algorithm

Update the ConfigurationMetadataAnnotationProcessor nested class
algorithm to prevent inner classes being added as both groups and
properties.

Fixes gh-1975
parent 2a9a7493
...@@ -175,8 +175,9 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor ...@@ -175,8 +175,9 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
ExecutableElement getter = entry.getValue(); ExecutableElement getter = entry.getValue();
ExecutableElement setter = members.getPublicSetters().get(name); ExecutableElement setter = members.getPublicSetters().get(name);
VariableElement field = members.getFields().get(name); VariableElement field = members.getFields().get(name);
boolean isNested = getAnnotation(field, Element returnType = this.processingEnv.getTypeUtils().asElement(
nestedConfigurationPropertyAnnotation()) != null; getter.getReturnType());
boolean isNested = isNested(returnType, field, element);
boolean isCollection = this.typeUtils.isCollectionOrMap(getter boolean isCollection = this.typeUtils.isCollectionOrMap(getter
.getReturnType()); .getReturnType());
if (!isNested && (setter != null || isCollection)) { if (!isNested && (setter != null || isCollection)) {
...@@ -203,24 +204,27 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor ...@@ -203,24 +204,27 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
getter.getReturnType()); getter.getReturnType());
AnnotationMirror annotation = getAnnotation(getter, AnnotationMirror annotation = getAnnotation(getter,
configurationPropertiesAnnotation()); configurationPropertiesAnnotation());
boolean isNested = getAnnotation(field, boolean isNested = isNested(returnType, field, element);
nestedConfigurationPropertyAnnotation()) != null;
if (returnType != null && returnType instanceof TypeElement if (returnType != null && returnType instanceof TypeElement
&& annotation == null) { && annotation == null && isNested) {
TypeElement returns = (TypeElement) returnType; String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, name);
if ((this.typeUtils.isEnclosedIn(returnType, element) && returnType this.metadata.add(ItemMetadata.newGroup(nestedPrefix,
.getKind() != ElementKind.ENUM) || isNested) { this.typeUtils.getType(returnType),
String nestedPrefix = ConfigurationMetadata this.typeUtils.getType(element), getter.toString()));
.nestedPrefix(prefix, name); processTypeElement(nestedPrefix, (TypeElement) returnType);
this.metadata.add(ItemMetadata.newGroup(nestedPrefix,
this.typeUtils.getType(returns),
this.typeUtils.getType(element), getter.toString()));
processTypeElement(nestedPrefix, returns);
}
} }
} }
} }
private boolean isNested(Element returnType, VariableElement field,
TypeElement element) {
if (getAnnotation(field, nestedConfigurationPropertyAnnotation()) != null) {
return true;
}
return this.typeUtils.isEnclosedIn(returnType, element)
&& returnType.getKind() != ElementKind.ENUM;
}
private boolean hasDeprecateAnnotation(Element element) { private boolean hasDeprecateAnnotation(Element element) {
return getAnnotation(element, "java.lang.Deprecated") != null; return getAnnotation(element, "java.lang.Deprecated") != null;
} }
......
...@@ -236,17 +236,14 @@ public class ConfigurationMetadataAnnotationProcessorTests { ...@@ -236,17 +236,14 @@ public class ConfigurationMetadataAnnotationProcessorTests {
assertThat(metadata, containsProperty("config.first.name")); assertThat(metadata, containsProperty("config.first.name"));
assertThat(metadata, containsProperty("config.first.bar.name")); assertThat(metadata, containsProperty("config.first.bar.name"));
assertThat(metadata, assertThat(metadata,
containsProperty("config.the-second", InnerClassProperties.Foo.class) containsGroup("config.the-second", InnerClassProperties.Foo.class)
.fromSource(InnerClassProperties.class)); .fromSource(InnerClassProperties.class));
assertThat(metadata, containsProperty("config.the-second.name")); assertThat(metadata, containsProperty("config.the-second.name"));
assertThat(metadata, containsProperty("config.the-second.bar.name")); assertThat(metadata, containsProperty("config.the-second.bar.name"));
assertThat(metadata, containsGroup("config.third").ofType(SimplePojo.class) assertThat(metadata, containsGroup("config.third").ofType(SimplePojo.class)
.fromSource(InnerClassProperties.class)); .fromSource(InnerClassProperties.class));
assertThat(metadata, containsProperty("config.third.value")); assertThat(metadata, containsProperty("config.third.value"));
assertThat( assertThat(metadata, containsProperty("config.fourth"));
metadata,
containsProperty("config.fourth").ofType(
InnerClassProperties.Fourth.class));
assertThat(metadata, not(containsGroup("config.fourth"))); assertThat(metadata, not(containsGroup("config.fourth")));
} }
......
...@@ -119,10 +119,13 @@ public class ConfigurationMetadataMatchers { ...@@ -119,10 +119,13 @@ public class ConfigurationMetadataMatchers {
ConfigurationMetadata metadata = (ConfigurationMetadata) item; ConfigurationMetadata metadata = (ConfigurationMetadata) item;
ItemMetadata property = getFirstPropertyWithName(metadata, this.name); ItemMetadata property = getFirstPropertyWithName(metadata, this.name);
if (property == null) { if (property == null) {
description.appendText("missing property " + this.name); description.appendText("missing "
+ this.itemType.toString().toLowerCase() + " " + this.name);
} }
else { else {
description.appendText("was property ").appendValue(property); description.appendText(
"was " + this.itemType.toString().toLowerCase() + " ")
.appendValue(property);
} }
} }
......
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