Prevent several registration of the same config pojo

This commit detects case where the same set of keys are exposed several
times and prevents the compilation to complete. Previously, duplicate
keys were silently added to the meta-data.

Closes gh-5939
This commit is contained in:
Stephane Nicoll
2016-06-20 09:19:02 +02:00
parent 1afb653859
commit 5407cf5f7a
5 changed files with 133 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@@ -172,11 +172,17 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
Element returns = this.processingEnv.getTypeUtils()
.asElement(element.getReturnType());
if (returns instanceof TypeElement) {
this.metadataCollector.add(
ItemMetadata.newGroup(prefix, this.typeUtils.getType(returns),
this.typeUtils.getType(element.getEnclosingElement()),
element.toString()));
processTypeElement(prefix, (TypeElement) returns);
ItemMetadata group = ItemMetadata.newGroup(prefix, this.typeUtils.getType(returns),
this.typeUtils.getType(element.getEnclosingElement()),
element.toString());
if (this.metadataCollector.hasSimilarGroup(group)) {
this.processingEnv.getMessager().printMessage(Kind.ERROR,
"Duplicate `@ConfigurationProperties` definition for prefix '" + prefix + "'", element);
}
else {
this.metadataCollector.add(group);
processTypeElement(prefix, (TypeElement) returns);
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@@ -77,6 +77,20 @@ public class MetadataCollector {
this.metadataItems.add(metadata);
}
public boolean hasSimilarGroup(ItemMetadata metadata) {
if (!metadata.isOfItemType(ItemMetadata.ItemType.GROUP)) {
throw new IllegalStateException("item " + metadata + " must be a group");
}
for (ItemMetadata existing : this.metadataItems) {
if (existing.isOfItemType(ItemMetadata.ItemType.GROUP)
&& existing.getName().equals(metadata.getName())
&& existing.getType().equals(metadata.getType())) {
return true;
}
}
return false;
}
public ConfigurationMetadata getMetadata() {
ConfigurationMetadata metadata = new ConfigurationMetadata();
for (ItemMetadata item : this.metadataItems) {