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
This commit is contained in:
Phillip Webb
2014-11-21 14:16:37 -08:00
parent 2a9a749329
commit e56a1ba561
3 changed files with 26 additions and 22 deletions

View File

@@ -175,8 +175,9 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
ExecutableElement getter = entry.getValue();
ExecutableElement setter = members.getPublicSetters().get(name);
VariableElement field = members.getFields().get(name);
boolean isNested = getAnnotation(field,
nestedConfigurationPropertyAnnotation()) != null;
Element returnType = this.processingEnv.getTypeUtils().asElement(
getter.getReturnType());
boolean isNested = isNested(returnType, field, element);
boolean isCollection = this.typeUtils.isCollectionOrMap(getter
.getReturnType());
if (!isNested && (setter != null || isCollection)) {
@@ -203,24 +204,27 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
getter.getReturnType());
AnnotationMirror annotation = getAnnotation(getter,
configurationPropertiesAnnotation());
boolean isNested = getAnnotation(field,
nestedConfigurationPropertyAnnotation()) != null;
boolean isNested = isNested(returnType, field, element);
if (returnType != null && returnType instanceof TypeElement
&& annotation == null) {
TypeElement returns = (TypeElement) returnType;
if ((this.typeUtils.isEnclosedIn(returnType, element) && returnType
.getKind() != ElementKind.ENUM) || isNested) {
String nestedPrefix = ConfigurationMetadata
.nestedPrefix(prefix, name);
this.metadata.add(ItemMetadata.newGroup(nestedPrefix,
this.typeUtils.getType(returns),
this.typeUtils.getType(element), getter.toString()));
processTypeElement(nestedPrefix, returns);
}
&& annotation == null && isNested) {
String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, name);
this.metadata.add(ItemMetadata.newGroup(nestedPrefix,
this.typeUtils.getType(returnType),
this.typeUtils.getType(element), getter.toString()));
processTypeElement(nestedPrefix, (TypeElement) returnType);
}
}
}
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) {
return getAnnotation(element, "java.lang.Deprecated") != null;
}