From 5407cf5f7acdd67037875282e03bb339faa0b8c0 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 20 Jun 2016 09:19:02 +0200 Subject: [PATCH] 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 --- ...figurationMetadataAnnotationProcessor.java | 18 ++++--- .../MetadataCollector.java | 16 ++++++- ...ationMetadataAnnotationProcessorTests.java | 19 ++++++++ .../DoubleRegistrationProperties.java | 39 +++++++++++++++ .../InvalidDoubleRegistrationProperties.java | 48 +++++++++++++++++++ 5 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/DoubleRegistrationProperties.java create mode 100644 spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/InvalidDoubleRegistrationProperties.java 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 4875ab8fe7..fed7366cf5 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 @@ -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); + } } } } diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataCollector.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataCollector.java index 7b7ff32457..b16cc08207 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataCollector.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataCollector.java @@ -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) { 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 2fe51dbaac..c33fb0f731 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 @@ -56,11 +56,13 @@ import org.springframework.boot.configurationsample.simple.SimpleTypeProperties; import org.springframework.boot.configurationsample.specific.BoxingPojo; import org.springframework.boot.configurationsample.specific.BuilderPojo; import org.springframework.boot.configurationsample.specific.DeprecatedUnrelatedMethodPojo; +import org.springframework.boot.configurationsample.specific.DoubleRegistrationProperties; import org.springframework.boot.configurationsample.specific.ExcludedTypesPojo; import org.springframework.boot.configurationsample.specific.InnerClassAnnotatedGetterConfig; import org.springframework.boot.configurationsample.specific.InnerClassProperties; import org.springframework.boot.configurationsample.specific.InnerClassRootConfig; import org.springframework.boot.configurationsample.specific.InvalidAccessorProperties; +import org.springframework.boot.configurationsample.specific.InvalidDoubleRegistrationProperties; import org.springframework.boot.configurationsample.specific.SimplePojo; import org.springframework.util.FileCopyUtils; @@ -342,6 +344,23 @@ public class ConfigurationMetadataAnnotationProcessorTests { assertThat(metadata.getItems()).hasSize(1); } + @Test + public void doubleRegistration() throws IOException { + ConfigurationMetadata metadata = compile(DoubleRegistrationProperties.class); + assertThat(metadata).has(Metadata.withGroup("one")); + assertThat(metadata).has(Metadata.withGroup("two")); + assertThat(metadata).has(Metadata.withProperty("one.value")); + assertThat(metadata).has(Metadata.withProperty("two.value")); + assertThat(metadata.getItems()).hasSize(4); + } + + @Test + public void invalidDoubleRegistration() throws IOException { + this.thrown.expect(IllegalStateException.class); + this.thrown.expectMessage("Compilation failed"); + compile(InvalidDoubleRegistrationProperties.class); + } + @Test public void lombokDataProperties() throws Exception { ConfigurationMetadata metadata = compile(LombokSimpleDataProperties.class); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/DoubleRegistrationProperties.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/DoubleRegistrationProperties.java new file mode 100644 index 0000000000..d03030e10e --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/DoubleRegistrationProperties.java @@ -0,0 +1,39 @@ +/* + * 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. + * 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; + +/** + * Test that the same type can be registered several times if the prefix is + * different. + * + * @author Stephane Nicoll + */ +public class DoubleRegistrationProperties { + + @ConfigurationProperties("one") + public SimplePojo one() { + return new SimplePojo(); + } + + @ConfigurationProperties("two") + public SimplePojo two() { + return new SimplePojo(); + } + +} diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/InvalidDoubleRegistrationProperties.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/InvalidDoubleRegistrationProperties.java new file mode 100644 index 0000000000..1bca18686c --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/InvalidDoubleRegistrationProperties.java @@ -0,0 +1,48 @@ +/* + * 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. + * 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; + +/** + * Test that compilation fails if the same type is registered twice with the + * same prefix. + * + * @author Stephane Nicoll + */ +public class InvalidDoubleRegistrationProperties { + + @ConfigurationProperties("foo") + public Foo foo() { + return new Foo(); + } + + @ConfigurationProperties("foo") + public static class Foo { + + private String name; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + } + +}