From 1296b4dfe687d1f4c6461c05f1a23f86caf199bb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 6 Oct 2020 16:06:10 +0200 Subject: [PATCH] Revert "Support constructor binding on 3rd party classes" This commit reverts the support of constructor binding on 3rd party classes using @ImportConfigurationPropertiesBean See gh-23172 Closes gh-23593 --- .../docs/asciidoc/spring-boot-features.adoc | 22 +-- ...figurationMetadataAnnotationProcessor.java | 119 +++--------- .../MetadataGenerationEnvironment.java | 33 +--- .../PropertyDescriptorResolver.java | 24 +-- .../ImportBeanTests.java | 81 -------- .../MetadataGenerationEnvironmentFactory.java | 4 +- .../PropertyDescriptorResolverTests.java | 8 +- ...figurationMetadataAnnotationProcessor.java | 14 -- .../ImportAsConfigurationPropertiesBean.java | 52 ----- .../ImportAsConfigurationPropertiesBeans.java | 38 ---- .../importbean/ImportAnnotatedJavaBean.java | 29 --- ...rtJavaBeanConfigurationPropertiesBean.java | 29 --- ...ltipleTypeConfigurationPropertiesBean.java | 30 --- ...rtRepeatedConfigurationPropertiesBean.java | 30 --- ...alueObjectConfigurationPropertiesBean.java | 29 --- .../importbean/ImportedAnnotatedJavaBean.java | 39 ---- .../importbean/ImportedJavaBean.java | 36 ---- .../importbean/ImportedValueObject.java | 36 ---- .../ConfigurationPropertiesBean.java | 91 +++------ ...ConfigurationPropertiesBeanDefinition.java | 47 ----- .../ConfigurationPropertiesBeanRegistrar.java | 32 +-- ...tionPropertiesBindConstructorProvider.java | 27 +-- .../ConfigurationPropertiesBinder.java | 10 +- .../ConfigurationPropertiesScanRegistrar.java | 2 +- ...onPropertiesValueObjectBeanDefinition.java | 21 +- .../EnableConfigurationProperties.java | 3 +- ...nableConfigurationPropertiesRegistrar.java | 4 +- .../ImportAsConfigurationPropertiesBean.java | 100 ---------- ...sConfigurationPropertiesBeanRegistrar.java | 76 -------- .../ImportAsConfigurationPropertiesBeans.java | 51 ----- .../context/properties/bind/Bindable.java | 45 +---- ...gurationPropertiesBeanDefinitionTests.java | 57 ------ ...igurationPropertiesBeanRegistrarTests.java | 46 +---- .../ConfigurationPropertiesBeanTests.java | 54 +----- ...igurationPropertiesScanRegistrarTests.java | 11 +- ...ConfigurationPropertiesRegistrarTests.java | 11 +- ...ortAsConfigurationPropertiesBeanTests.java | 182 ------------------ .../properties/bind/BindableTests.java | 18 +- ...nfigurationPropertiesBeanRegistrarTests.kt | 11 +- 39 files changed, 139 insertions(+), 1413 deletions(-) delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ImportBeanTests.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/ImportAsConfigurationPropertiesBean.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/ImportAsConfigurationPropertiesBeans.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportAnnotatedJavaBean.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportJavaBeanConfigurationPropertiesBean.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportMultipleTypeConfigurationPropertiesBean.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportRepeatedConfigurationPropertiesBean.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportValueObjectConfigurationPropertiesBean.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedAnnotatedJavaBean.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedJavaBean.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedValueObject.java delete mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanDefinition.java delete mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBean.java delete mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeanRegistrar.java delete mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeans.java delete mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanDefinitionTests.java delete mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeanTests.java diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 169a7f75cd..57e36e35f5 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -1242,7 +1242,7 @@ Spring Boot provides infrastructure to bind `@ConfigurationProperties` types and You can either enable configuration properties on a class-by-class basis or enable configuration property scanning that works in a similar manner to component scanning. Sometimes, classes annotated with `@ConfigurationProperties` might not be suitable for scanning, for example, if you're developing your own auto-configuration or you want to enable them conditionally. -In these cases, specify the list of types to process using the `@EnableConfigurationProperties` or `@ImportAsConfigurationPropertiesBean` annotations. +In these cases, specify the list of types to process using the `@EnableConfigurationProperties` annotation. This can be done on any `@Configuration` class, as shown in the following example: [source,java,indent=0] @@ -1268,7 +1268,7 @@ If you want to define specific packages to scan, you can do so as shown in the f [NOTE] ==== -When the `@ConfigurationProperties` bean is registered using configuration property scanning or via `@EnableConfigurationProperties` or `@ImportAsConfigurationPropertiesBean`, the bean has a conventional name: `-`, where `` is the environment key prefix specified in the `@ConfigurationProperties` annotation and `` is the fully qualified name of the bean. +When the `@ConfigurationProperties` bean is registered using configuration property scanning or via `@EnableConfigurationProperties`, the bean has a conventional name: `-`, where `` is the environment key prefix specified in the `@ConfigurationProperties` annotation and `` is the fully qualified name of the bean. If the annotation does not provide any prefix, only the fully qualified name of the bean is used. The bean name in the example above is `acme-com.example.AcmeProperties`. @@ -1336,26 +1336,12 @@ To configure a bean from the `Environment` properties, add `@ConfigurationProper ---- @ConfigurationProperties(prefix = "another") @Bean - public ExampleItem exampleItem() { + public AnotherComponent anotherComponent() { ... } ---- -Any JavaBean property defined with the `another` prefix is mapped onto that `ExampleItem` bean in manner similar to the preceding `AcmeProperties` example. - -If you want to use constructor binding with a third-party class, you can't use a `@Bean` method since Spring will need to create the object instance. -For those situations, you can use an `@ImportAsConfigurationPropertiesBean` annotation on your `@Configuration` or `@SpringBootApplication` class. - -[source,java,indent=0] ----- - @SpringBootApplication - @ImportAsConfigurationPropertiesBean(type = ExampleItem.class, prefix = "another") - public class MyApp { - ... - } ----- - -TIP: `@ImportAsConfigurationPropertiesBean` also works for JavaBean bindings as long as the type has a single no-arg constructor +Any JavaBean property defined with the `another` prefix is mapped onto that `AnotherComponent` bean in manner similar to the preceding `AcmeProperties` example. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java index 4c8ee05839..6a1e2d23fb 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java @@ -22,7 +22,6 @@ import java.io.StringWriter; import java.time.Duration; import java.util.Collections; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -41,7 +40,6 @@ import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeKind; -import javax.lang.model.type.TypeMirror; import javax.lang.model.util.ElementFilter; import javax.tools.Diagnostic.Kind; @@ -82,10 +80,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor static final String NAME_ANNOTATION = "org.springframework.boot.context.properties.bind.Name"; - static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION = "org.springframework.boot.context.properties.ImportAsConfigurationPropertiesBean"; - - static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION = "org.springframework.boot.context.properties.ImportAsConfigurationPropertiesBeans"; - private static final Set SUPPORTED_OPTIONS = Collections .unmodifiableSet(Collections.singleton(ADDITIONAL_METADATA_LOCATIONS_OPTION)); @@ -127,14 +121,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor return NAME_ANNOTATION; } - protected String importAsConfigurationPropertiesBeanAnnotation() { - return IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION; - } - - protected String importAsConfigurationPropertiesBeansAnnotation() { - return IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION; - } - @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); @@ -153,16 +139,22 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor this.metadataEnv = new MetadataGenerationEnvironment(env, configurationPropertiesAnnotation(), nestedConfigurationPropertyAnnotation(), deprecatedConfigurationPropertyAnnotation(), constructorBindingAnnotation(), defaultValueAnnotation(), endpointAnnotation(), - readOperationAnnotation(), nameAnnotation(), importAsConfigurationPropertiesBeanAnnotation(), - importAsConfigurationPropertiesBeansAnnotation()); + readOperationAnnotation(), nameAnnotation()); } @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { this.metadataCollector.processing(roundEnv); - processConfigurationProperties(roundEnv); - processEndpoint(roundEnv); - processImportAsConfigurationProperties(roundEnv); + TypeElement annotationType = this.metadataEnv.getConfigurationPropertiesAnnotationElement(); + if (annotationType != null) { // Is @ConfigurationProperties available + for (Element element : roundEnv.getElementsAnnotatedWith(annotationType)) { + processElement(element); + } + } + TypeElement endpointType = this.metadataEnv.getEndpointAnnotationElement(); + if (endpointType != null) { // Is @Endpoint available + getElementsAnnotatedOrMetaAnnotatedWith(roundEnv, endpointType).forEach(this::processEndpoint); + } if (roundEnv.processingOver()) { try { writeMetaData(); @@ -174,40 +166,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor return false; } - private void processConfigurationProperties(RoundEnvironment roundEnv) { - TypeElement annotationType = this.metadataEnv.getConfigurationPropertiesAnnotationElement(); - if (annotationType != null) { - for (Element element : roundEnv.getElementsAnnotatedWith(annotationType)) { - processElement(element); - } - } - } - - private void processEndpoint(RoundEnvironment roundEnv) { - TypeElement endpointType = this.metadataEnv.getEndpointAnnotationElement(); - if (endpointType != null) { - getElementsAnnotatedOrMetaAnnotatedWith(roundEnv, endpointType).forEach(this::processEndpoint); - } - } - - private void processImportAsConfigurationProperties(RoundEnvironment roundEnv) { - TypeElement importAsConfigurationPropertiesBeanType = this.metadataEnv - .getImportAsConfigurationPropertiesBeansAnnotation(); - TypeElement importAsConfigurationPropertiesBeansType = this.metadataEnv - .getImportAsConfigurationPropertiesBeansAnnotationElement(); - if (importAsConfigurationPropertiesBeanType == null && importAsConfigurationPropertiesBeansType == null) { - return; - } - Set elements = new LinkedHashSet<>(); - if (importAsConfigurationPropertiesBeanType != null) { - elements.addAll(roundEnv.getElementsAnnotatedWith(importAsConfigurationPropertiesBeanType)); - } - if (importAsConfigurationPropertiesBeansType != null) { - elements.addAll(roundEnv.getElementsAnnotatedWith(importAsConfigurationPropertiesBeansType)); - } - elements.forEach(this::processImportAsConfigurationPropertiesBean); - } - private Map> getElementsAnnotatedOrMetaAnnotatedWith(RoundEnvironment roundEnv, TypeElement annotation) { Map> result = new LinkedHashMap<>(); @@ -226,7 +184,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor if (annotation != null) { String prefix = getPrefix(annotation); if (element instanceof TypeElement) { - processAnnotatedTypeElement(prefix, (TypeElement) element, false, new Stack<>()); + processAnnotatedTypeElement(prefix, (TypeElement) element, new Stack<>()); } else if (element instanceof ExecutableElement) { processExecutableElement(prefix, (ExecutableElement) element, new Stack<>()); @@ -238,11 +196,10 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor } } - private void processAnnotatedTypeElement(String prefix, TypeElement element, boolean fromImport, - Stack seen) { + private void processAnnotatedTypeElement(String prefix, TypeElement element, Stack seen) { String type = this.metadataEnv.getTypeUtils().getQualifiedName(element); this.metadataCollector.add(ItemMetadata.newGroup(prefix, type, type, null)); - processTypeElement(prefix, element, fromImport, null, seen); + processTypeElement(prefix, element, null, seen); } private void processExecutableElement(String prefix, ExecutableElement element, Stack seen) { @@ -260,26 +217,25 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor } else { this.metadataCollector.add(group); - processTypeElement(prefix, (TypeElement) returns, false, element, seen); + processTypeElement(prefix, (TypeElement) returns, element, seen); } } } } - private void processTypeElement(String prefix, TypeElement element, boolean fromImport, ExecutableElement source, + private void processTypeElement(String prefix, TypeElement element, ExecutableElement source, Stack seen) { if (!seen.contains(element)) { seen.push(element); - new PropertyDescriptorResolver(this.metadataEnv).resolve(element, fromImport, source) - .forEach((descriptor) -> { - this.metadataCollector.add(descriptor.resolveItemMetadata(prefix, this.metadataEnv)); - if (descriptor.isNested(this.metadataEnv)) { - TypeElement nestedTypeElement = (TypeElement) this.metadataEnv.getTypeUtils() - .asElement(descriptor.getType()); - String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, descriptor.getName()); - processTypeElement(nestedPrefix, nestedTypeElement, false, source, seen); - } - }); + new PropertyDescriptorResolver(this.metadataEnv).resolve(element, source).forEach((descriptor) -> { + this.metadataCollector.add(descriptor.resolveItemMetadata(prefix, this.metadataEnv)); + if (descriptor.isNested(this.metadataEnv)) { + TypeElement nestedTypeElement = (TypeElement) this.metadataEnv.getTypeUtils() + .asElement(descriptor.getType()); + String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, descriptor.getName()); + processTypeElement(nestedPrefix, nestedTypeElement, source, seen); + } + }); seen.pop(); } } @@ -316,31 +272,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor } } - private void processImportAsConfigurationPropertiesBean(Element element) { - this.metadataEnv.getImportAsConfigurationPropertiesBeanAnnotations(element) - .forEach(this::processImportAsConfigurationPropertiesBean); - } - - @SuppressWarnings("unchecked") - private void processImportAsConfigurationPropertiesBean(AnnotationMirror annotation) { - String prefix = getPrefix(annotation); - processImportAsConfigurationPropertiesBeanTypes(prefix, - (List) this.metadataEnv.getAnnotationElementValues(annotation).get("type")); - processImportAsConfigurationPropertiesBeanTypes(prefix, - (List) this.metadataEnv.getAnnotationElementValues(annotation).get("value")); - } - - private void processImportAsConfigurationPropertiesBeanTypes(String prefix, List types) { - if (types != null) { - for (TypeMirror type : types) { - Element element = this.metadataEnv.getTypeUtils().asElement(type); - AnnotationMirror annotation = this.metadataEnv.getConfigurationPropertiesAnnotation(element); - prefix = (annotation != null) ? getPrefix(annotation) : prefix; - processAnnotatedTypeElement(prefix, (TypeElement) element, true, new Stack<>()); - } - } - } - private boolean hasMainReadOperation(TypeElement element) { for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())) { if (this.metadataEnv.getReadOperationAnnotation(method) != null diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java index 9bd98bc226..6d127561a5 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java @@ -97,15 +97,10 @@ class MetadataGenerationEnvironment { private final String nameAnnotation; - private final String importAsConfigurationPropertiesBeanAnnotation; - - private final String importAsConfigurationPropertiesBeansAnnotation; - MetadataGenerationEnvironment(ProcessingEnvironment environment, String configurationPropertiesAnnotation, String nestedConfigurationPropertyAnnotation, String deprecatedConfigurationPropertyAnnotation, String constructorBindingAnnotation, String defaultValueAnnotation, String endpointAnnotation, - String readOperationAnnotation, String nameAnnotation, String importAsConfigurationPropertiesBeanAnnotation, - String importAsConfigurationPropertiesBeansAnnotation) { + String readOperationAnnotation, String nameAnnotation) { this.typeUtils = new TypeUtils(environment); this.elements = environment.getElementUtils(); this.messager = environment.getMessager(); @@ -118,8 +113,6 @@ class MetadataGenerationEnvironment { this.endpointAnnotation = endpointAnnotation; this.readOperationAnnotation = readOperationAnnotation; this.nameAnnotation = nameAnnotation; - this.importAsConfigurationPropertiesBeanAnnotation = importAsConfigurationPropertiesBeanAnnotation; - this.importAsConfigurationPropertiesBeansAnnotation = importAsConfigurationPropertiesBeansAnnotation; } private static FieldValuesParser resolveFieldValuesParser(ProcessingEnvironment env) { @@ -265,14 +258,6 @@ class MetadataGenerationEnvironment { return this.elements.getTypeElement(this.configurationPropertiesAnnotation); } - TypeElement getImportAsConfigurationPropertiesBeansAnnotation() { - return this.elements.getTypeElement(this.importAsConfigurationPropertiesBeanAnnotation); - } - - TypeElement getImportAsConfigurationPropertiesBeansAnnotationElement() { - return this.elements.getTypeElement(this.importAsConfigurationPropertiesBeansAnnotation); - } - AnnotationMirror getConfigurationPropertiesAnnotation(Element element) { return getAnnotation(element, this.configurationPropertiesAnnotation); } @@ -297,22 +282,6 @@ class MetadataGenerationEnvironment { return getAnnotation(element, this.nameAnnotation); } - List getImportAsConfigurationPropertiesBeanAnnotations(Element element) { - List annotations = new ArrayList<>(); - AnnotationMirror importBean = getAnnotation(element, this.importAsConfigurationPropertiesBeanAnnotation); - if (importBean != null) { - annotations.add(importBean); - } - AnnotationMirror importBeans = getAnnotation(element, this.importAsConfigurationPropertiesBeansAnnotation); - if (importBeans != null) { - AnnotationValue value = importBeans.getElementValues().values().iterator().next(); - for (Object contained : (List) value.getValue()) { - annotations.add((AnnotationMirror) contained); - } - } - return Collections.unmodifiableList(annotations); - } - boolean hasNullableAnnotation(Element element) { return getAnnotation(element, NULLABLE_ANNOTATION) != null; } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolver.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolver.java index 48812c5cb1..886b55f9d2 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolver.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolver.java @@ -49,19 +49,16 @@ class PropertyDescriptorResolver { * specified {@link TypeElement type} based on the specified {@link ExecutableElement * factory method}, if any. * @param type the target type - * @param fromImport it the type was imported via a - * {@code @ImportAsConfigurationPropertiesBean} * @param factoryMethod the method that triggered the metadata for that {@code type} * or {@code null} * @return the candidate properties for metadata generation */ - Stream> resolve(TypeElement type, boolean fromImport, ExecutableElement factoryMethod) { + Stream> resolve(TypeElement type, ExecutableElement factoryMethod) { TypeElementMembers members = new TypeElementMembers(this.environment, type); if (factoryMethod != null) { return resolveJavaBeanProperties(type, factoryMethod, members); } - return resolve(ConfigurationPropertiesTypeElement.of(type, fromImport, this.environment), factoryMethod, - members); + return resolve(ConfigurationPropertiesTypeElement.of(type, this.environment), factoryMethod, members); } private Stream> resolve(ConfigurationPropertiesTypeElement type, @@ -181,29 +178,20 @@ class PropertyDescriptorResolver { return boundConstructor; } - static ConfigurationPropertiesTypeElement of(TypeElement type, boolean fromImport, - MetadataGenerationEnvironment env) { + static ConfigurationPropertiesTypeElement of(TypeElement type, MetadataGenerationEnvironment env) { + boolean constructorBoundType = isConstructorBoundType(type, env); List constructors = ElementFilter.constructorsIn(type.getEnclosedElements()); List boundConstructors = constructors.stream() .filter(env::hasConstructorBindingAnnotation).collect(Collectors.toList()); - boolean constructorBoundType = isConstructorBoundType(type, fromImport, constructors, env); return new ConfigurationPropertiesTypeElement(type, constructorBoundType, constructors, boundConstructors); } - private static boolean isConstructorBoundType(TypeElement type, boolean fromImport, - List constructors, MetadataGenerationEnvironment env) { + private static boolean isConstructorBoundType(TypeElement type, MetadataGenerationEnvironment env) { if (env.hasConstructorBindingAnnotation(type)) { return true; } if (type.getNestingKind() == NestingKind.MEMBER) { - return isConstructorBoundType((TypeElement) type.getEnclosingElement(), false, constructors, env); - } - if (fromImport) { - for (ExecutableElement constructor : constructors) { - if (!constructor.getParameters().isEmpty()) { - return true; - } - } + return isConstructorBoundType((TypeElement) type.getEnclosingElement(), env); } return false; } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ImportBeanTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ImportBeanTests.java deleted file mode 100644 index fcdf385f93..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ImportBeanTests.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.configurationprocessor; - -import org.junit.jupiter.api.Test; - -import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata; -import org.springframework.boot.configurationprocessor.metadata.Metadata; -import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean; -import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBeans; -import org.springframework.boot.configurationsample.importbean.ImportAnnotatedJavaBean; -import org.springframework.boot.configurationsample.importbean.ImportJavaBeanConfigurationPropertiesBean; -import org.springframework.boot.configurationsample.importbean.ImportMultipleTypeConfigurationPropertiesBean; -import org.springframework.boot.configurationsample.importbean.ImportRepeatedConfigurationPropertiesBean; -import org.springframework.boot.configurationsample.importbean.ImportValueObjectConfigurationPropertiesBean; -import org.springframework.boot.configurationsample.importbean.ImportedAnnotatedJavaBean; -import org.springframework.boot.configurationsample.importbean.ImportedJavaBean; -import org.springframework.boot.configurationsample.importbean.ImportedValueObject; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Tests for {@link ImportAsConfigurationPropertiesBean} and - * {@link ImportAsConfigurationPropertiesBeans}. - * - * @author Phillip Webb - */ -public class ImportBeanTests extends AbstractMetadataGenerationTests { - - @Test - void importValueObjectConfigurationPropertiesBean() { - ConfigurationMetadata metadata = compile(ImportValueObjectConfigurationPropertiesBean.class); - assertThat(metadata) - .has(Metadata.withProperty("importbean.value", String.class).fromSource(ImportedValueObject.class)); - } - - @Test - void importJavaBeanConfigurationPropertiesBean() { - ConfigurationMetadata metadata = compile(ImportJavaBeanConfigurationPropertiesBean.class); - assertThat(metadata) - .has(Metadata.withProperty("importbean.name", String.class).fromSource(ImportedJavaBean.class)); - } - - @Test - void importMultipleTypeConfigurationPropertiesBean() { - ConfigurationMetadata metadata = compile(ImportMultipleTypeConfigurationPropertiesBean.class); - assertThat(metadata) - .has(Metadata.withProperty("importbean.value", String.class).fromSource(ImportedValueObject.class)); - assertThat(metadata) - .has(Metadata.withProperty("importbean.name", String.class).fromSource(ImportedJavaBean.class)); - } - - @Test - void importRepeatedConfigurationPropertiesBean() { - ConfigurationMetadata metadata = compile(ImportRepeatedConfigurationPropertiesBean.class); - assertThat(metadata).has(Metadata.withProperty("vo.value", String.class).fromSource(ImportedValueObject.class)); - assertThat(metadata).has(Metadata.withProperty("jb.name", String.class).fromSource(ImportedJavaBean.class)); - } - - @Test - void importAnnotatedJavaBean() { - ConfigurationMetadata metadata = compile(ImportAnnotatedJavaBean.class); - assertThat(metadata) - .has(Metadata.withProperty("test.name", String.class).fromSource(ImportedAnnotatedJavaBean.class)); - } - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironmentFactory.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironmentFactory.java index 50b740646d..0b67c60a03 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironmentFactory.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironmentFactory.java @@ -39,9 +39,7 @@ class MetadataGenerationEnvironmentFactory implements Function { PropertyDescriptorResolver resolver = new PropertyDescriptorResolver(metadataEnv); - assertThat(resolver.resolve(type, false, null).map(PropertyDescriptor::getName)) - .containsExactly("third", "second", "first"); - assertThat(resolver.resolve(type, false, null) + assertThat(resolver.resolve(type, null).map(PropertyDescriptor::getName)).containsExactly("third", + "second", "first"); + assertThat(resolver.resolve(type, null) .map((descriptor) -> descriptor.resolveItemMetadata("test", metadataEnv)) .map(ItemMetadata::getDefaultValue)).containsExactly("three", "two", "one"); }); @@ -155,7 +155,7 @@ class PropertyDescriptorResolverTests { Consumer>> stream) { return (element, metadataEnv) -> { PropertyDescriptorResolver resolver = new PropertyDescriptorResolver(metadataEnv); - stream.accept(resolver.resolve(element, false, null)); + stream.accept(resolver.resolve(element, null)); }; } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestConfigurationMetadataAnnotationProcessor.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestConfigurationMetadataAnnotationProcessor.java index c87a62d1c0..149a746c0e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestConfigurationMetadataAnnotationProcessor.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestConfigurationMetadataAnnotationProcessor.java @@ -57,10 +57,6 @@ public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationM public static final String NAME_ANNOTATION = "org.springframework.boot.configurationsample.Name"; - public static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION = "org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean"; - - public static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION = "org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBeans"; - private ConfigurationMetadata metadata; private final File outputLocation; @@ -109,16 +105,6 @@ public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationM return NAME_ANNOTATION; } - @Override - protected String importAsConfigurationPropertiesBeanAnnotation() { - return IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION; - } - - @Override - protected String importAsConfigurationPropertiesBeansAnnotation() { - return IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION; - } - @Override protected ConfigurationMetadata writeMetaData() throws Exception { super.writeMetaData(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/ImportAsConfigurationPropertiesBean.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/ImportAsConfigurationPropertiesBean.java deleted file mode 100644 index 5ac68b7dc1..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/ImportAsConfigurationPropertiesBean.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Repeatable; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.springframework.core.annotation.AliasFor; - -/** - * Alternative to Spring Boot's {@code ImportAsConfigurationPropertiesBean} for testing - * (removes the need for a dependency on the real annotation). - * - * @author Phillip Webb - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@ConfigurationProperties -@Repeatable(ImportAsConfigurationPropertiesBeans.class) -public @interface ImportAsConfigurationPropertiesBean { - - Class[] type(); - - @AliasFor(annotation = ConfigurationProperties.class) - String prefix() default ""; - - @AliasFor(annotation = ConfigurationProperties.class) - boolean ignoreInvalidFields() default false; - - @AliasFor(annotation = ConfigurationProperties.class) - boolean ignoreUnknownFields() default true; - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/ImportAsConfigurationPropertiesBeans.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/ImportAsConfigurationPropertiesBeans.java deleted file mode 100644 index f279aa65b0..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/ImportAsConfigurationPropertiesBeans.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Alternative to Spring Boot's {@code ImportAsConfigurationPropertiesBeans} for testing - * (removes the need for a dependency on the real annotation). - * - * @author Phillip Webb - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -public @interface ImportAsConfigurationPropertiesBeans { - - ImportAsConfigurationPropertiesBean[] value(); - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportAnnotatedJavaBean.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportAnnotatedJavaBean.java deleted file mode 100644 index fdd9f69da0..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportAnnotatedJavaBean.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.importbean; - -import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean; - -/** - * An import of a java bean. - * - * @author Phillip Webb - */ -@ImportAsConfigurationPropertiesBean(type = ImportedAnnotatedJavaBean.class) -public class ImportAnnotatedJavaBean { - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportJavaBeanConfigurationPropertiesBean.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportJavaBeanConfigurationPropertiesBean.java deleted file mode 100644 index a4e596abf3..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportJavaBeanConfigurationPropertiesBean.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.importbean; - -import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean; - -/** - * An import of a java bean. - * - * @author Phillip Webb - */ -@ImportAsConfigurationPropertiesBean(type = ImportedJavaBean.class, prefix = "importbean") -public class ImportJavaBeanConfigurationPropertiesBean { - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportMultipleTypeConfigurationPropertiesBean.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportMultipleTypeConfigurationPropertiesBean.java deleted file mode 100644 index 5e0bde77ea..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportMultipleTypeConfigurationPropertiesBean.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.importbean; - -import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean; - -/** - * An import of a java bean and a value object. - * - * @author Phillip Webb - */ -@ImportAsConfigurationPropertiesBean(type = { ImportedJavaBean.class, ImportedValueObject.class }, - prefix = "importbean") -public class ImportMultipleTypeConfigurationPropertiesBean { - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportRepeatedConfigurationPropertiesBean.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportRepeatedConfigurationPropertiesBean.java deleted file mode 100644 index 1e22552191..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportRepeatedConfigurationPropertiesBean.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.importbean; - -import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean; - -/** - * An import of a java bean and a value object. - * - * @author Phillip Webb - */ -@ImportAsConfigurationPropertiesBean(type = ImportedJavaBean.class, prefix = "jb") -@ImportAsConfigurationPropertiesBean(type = ImportedValueObject.class, prefix = "vo") -public class ImportRepeatedConfigurationPropertiesBean { - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportValueObjectConfigurationPropertiesBean.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportValueObjectConfigurationPropertiesBean.java deleted file mode 100644 index 66d58edf62..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportValueObjectConfigurationPropertiesBean.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.importbean; - -import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean; - -/** - * An import of a value object. - * - * @author Phillip Webb - */ -@ImportAsConfigurationPropertiesBean(type = ImportedValueObject.class, prefix = "importbean") -public class ImportValueObjectConfigurationPropertiesBean { - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedAnnotatedJavaBean.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedAnnotatedJavaBean.java deleted file mode 100644 index f7dad4ded4..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedAnnotatedJavaBean.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.importbean; - -import org.springframework.boot.configurationsample.ConfigurationProperties; - -/** - * Java bean that can be imported. - * - * @author Phillip Webb - */ -@ConfigurationProperties(prefix = "test") -public class ImportedAnnotatedJavaBean { - - private String name; - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedJavaBean.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedJavaBean.java deleted file mode 100644 index cbb7ed71f5..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedJavaBean.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.importbean; - -/** - * Java bean that can be imported. - * - * @author Phillip Webb - */ -public class ImportedJavaBean { - - private String name; - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedValueObject.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedValueObject.java deleted file mode 100644 index 3dc35d11cf..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/importbean/ImportedValueObject.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.importbean; - -/** - * Value object that can be imported. - * - * @author Phillip Webb - */ -public class ImportedValueObject { - - private final String value; - - public ImportedValueObject(String value) { - this.value = value; - } - - public String getValue() { - return this.value; - } - -} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java index 25c0b1182f..b5628a5f68 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java @@ -18,7 +18,6 @@ package org.springframework.boot.context.properties; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; -import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.Iterator; import java.util.LinkedHashMap; @@ -76,7 +75,7 @@ public final class ConfigurationPropertiesBean { this.instance = instance; this.annotation = annotation; this.bindTarget = bindTarget; - this.bindMethod = BindMethod.forBindable(bindTarget); + this.bindMethod = BindMethod.forType(bindTarget.getType().resolve()); } /** @@ -176,8 +175,7 @@ public final class ConfigurationPropertiesBean { if (beanFactory.findAnnotationOnBean(beanName, ConfigurationProperties.class) != null) { return true; } - BeanDefinition beanDefinition = findBeanDefinition(beanFactory, beanName); - Method factoryMethod = findFactoryMethod(beanFactory, beanDefinition); + Method factoryMethod = findFactoryMethod(beanFactory, beanName); return findMergedAnnotation(factoryMethod, ConfigurationProperties.class).isPresent(); } catch (NoSuchBeanDefinitionException ex) { @@ -199,41 +197,33 @@ public final class ConfigurationPropertiesBean { * {@link ConfigurationProperties @ConfigurationProperties} */ public static ConfigurationPropertiesBean get(ApplicationContext applicationContext, Object bean, String beanName) { - ConfigurableListableBeanFactory beanFactory = getBeanFactory(applicationContext); - BeanDefinition beanDefinition = findBeanDefinition(beanFactory, beanName); - Method factoryMethod = findFactoryMethod(beanFactory, beanDefinition); - ConfigurationProperties annotation = findAnnotation(beanDefinition); - boolean deduceBindConstructor = (beanDefinition instanceof ConfigurationPropertiesValueObjectBeanDefinition) - ? ((ConfigurationPropertiesValueObjectBeanDefinition) beanDefinition).isDeduceBindConstructor() : false; - return create(beanName, bean, bean.getClass(), factoryMethod, annotation, deduceBindConstructor); + Method factoryMethod = findFactoryMethod(applicationContext, beanName); + return create(beanName, bean, bean.getClass(), factoryMethod); } - private static ConfigurableListableBeanFactory getBeanFactory(ApplicationContext applicationContext) { + private static Method findFactoryMethod(ApplicationContext applicationContext, String beanName) { if (applicationContext instanceof ConfigurableApplicationContext) { - return ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); + return findFactoryMethod((ConfigurableApplicationContext) applicationContext, beanName); } return null; } - private static BeanDefinition findBeanDefinition(ConfigurableListableBeanFactory beanFactory, String beanName) { - if (beanFactory != null && beanFactory.containsBeanDefinition(beanName)) { - return beanFactory.getMergedBeanDefinition(beanName); - } - return null; + private static Method findFactoryMethod(ConfigurableApplicationContext applicationContext, String beanName) { + return findFactoryMethod(applicationContext.getBeanFactory(), beanName); } - private static Method findFactoryMethod(ConfigurableListableBeanFactory beanFactory, - BeanDefinition beanDefinition) { - if (beanFactory == null || beanDefinition == null) { - return null; - } - if (beanDefinition instanceof RootBeanDefinition) { - Method resolvedFactoryMethod = ((RootBeanDefinition) beanDefinition).getResolvedFactoryMethod(); - if (resolvedFactoryMethod != null) { - return resolvedFactoryMethod; + private static Method findFactoryMethod(ConfigurableListableBeanFactory beanFactory, String beanName) { + if (beanFactory.containsBeanDefinition(beanName)) { + BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName); + if (beanDefinition instanceof RootBeanDefinition) { + Method resolvedFactoryMethod = ((RootBeanDefinition) beanDefinition).getResolvedFactoryMethod(); + if (resolvedFactoryMethod != null) { + return resolvedFactoryMethod; + } } + return findFactoryMethodUsingReflection(beanFactory, beanDefinition); } - return findFactoryMethodUsingReflection(beanFactory, beanDefinition); + return null; } private static Method findFactoryMethodUsingReflection(ConfigurableListableBeanFactory beanFactory, @@ -256,19 +246,15 @@ public final class ConfigurationPropertiesBean { return factoryMethod.get(); } - static ConfigurationPropertiesBean forValueObject(Class beanClass, String beanName, - MergedAnnotation annotation, boolean deduceBindConstructor) { - ConfigurationPropertiesBean propertiesBean = create(beanName, null, beanClass, null, - annotation.isPresent() ? annotation.synthesize() : null, deduceBindConstructor); + static ConfigurationPropertiesBean forValueObject(Class beanClass, String beanName) { + ConfigurationPropertiesBean propertiesBean = create(beanName, null, beanClass, null); Assert.state(propertiesBean != null && propertiesBean.getBindMethod() == BindMethod.VALUE_OBJECT, () -> "Bean '" + beanName + "' is not a @ConfigurationProperties value object"); return propertiesBean; } - private static ConfigurationPropertiesBean create(String name, Object instance, Class type, Method factory, - ConfigurationProperties annotation, boolean deduceBindConstructor) { - annotation = (annotation != null) ? annotation - : findAnnotation(instance, type, factory, ConfigurationProperties.class); + private static ConfigurationPropertiesBean create(String name, Object instance, Class type, Method factory) { + ConfigurationProperties annotation = findAnnotation(instance, type, factory, ConfigurationProperties.class); if (annotation == null) { return null; } @@ -281,19 +267,9 @@ public final class ConfigurationPropertiesBean { if (instance != null) { bindTarget = bindTarget.withExistingValue(instance); } - if (deduceBindConstructor) { - bindTarget = bindTarget.withAttribute( - ConfigurationPropertiesBindConstructorProvider.DEDUCE_BIND_CONSTRUCTOR_ATTRIUBTE, true); - } return new ConfigurationPropertiesBean(name, instance, annotation, bindTarget); } - private static ConfigurationProperties findAnnotation(BeanDefinition beanDefinition) { - MergedAnnotation annotation = ConfigurationPropertiesBeanDefinition - .getAnnotation(beanDefinition); - return (annotation.isPresent()) ? annotation.synthesize() : null; - } - private static A findAnnotation(Object instance, Class type, Method factory, Class annotationType) { MergedAnnotation annotation = MergedAnnotation.missing(); @@ -332,27 +308,8 @@ public final class ConfigurationPropertiesBean { VALUE_OBJECT; static BindMethod forType(Class type) { - return forType(type, false); - } - - static BindMethod forType(Class type, boolean deduceBindConstructor) { - Constructor constructor = ConfigurationPropertiesBindConstructorProvider.INSTANCE - .getBindConstructor(type, deduceBindConstructor, false); - if (deduceBindConstructor) { - Assert.state(constructor != null, - () -> "Unable to deduce @ConfigurationProperties bind method for " + type.getName()); - } - return hasParameters(constructor) ? VALUE_OBJECT : JAVA_BEAN; - } - - static BindMethod forBindable(Bindable bindable) { - Constructor constructor = ConfigurationPropertiesBindConstructorProvider.INSTANCE - .getBindConstructor(bindable, false); - return hasParameters(constructor) ? VALUE_OBJECT : JAVA_BEAN; - } - - private static boolean hasParameters(Constructor constructor) { - return constructor != null && constructor.getParameterCount() > 0; + return (ConfigurationPropertiesBindConstructorProvider.INSTANCE.getBindConstructor(type, false) != null) + ? VALUE_OBJECT : JAVA_BEAN; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanDefinition.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanDefinition.java deleted file mode 100644 index 3b2a07eaca..0000000000 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanDefinition.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.context.properties; - -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.GenericBeanDefinition; -import org.springframework.core.Conventions; -import org.springframework.core.annotation.MergedAnnotation; - -/** - * {@link BeanDefinition} that is used for registering - * {@link ConfigurationProperties @ConfigurationProperties} beans. - * - * @author Phillip Webb - */ -class ConfigurationPropertiesBeanDefinition extends GenericBeanDefinition { - - private static final String ANNOTATION_ATTRIBUTE = Conventions - .getQualifiedAttributeName(ConfigurationPropertiesBeanDefinition.class, "annotation"); - - ConfigurationPropertiesBeanDefinition(Class beanClass, MergedAnnotation annotation) { - setBeanClass(beanClass); - setAttribute(ANNOTATION_ATTRIBUTE, annotation); - } - - @SuppressWarnings("unchecked") - static MergedAnnotation getAnnotation(BeanDefinition beanDefinition) { - MergedAnnotation annotation = (beanDefinition != null) - ? (MergedAnnotation) beanDefinition.getAttribute(ANNOTATION_ATTRIBUTE) : null; - return (annotation != null) ? annotation : MergedAnnotation.missing(); - } - -} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrar.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrar.java index b78672f550..791e31e32e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrar.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2019 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. @@ -20,6 +20,7 @@ import org.springframework.beans.factory.HierarchicalBeanFactory; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.boot.context.properties.ConfigurationPropertiesBean.BindMethod; import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.annotation.MergedAnnotations; @@ -46,14 +47,16 @@ final class ConfigurationPropertiesBeanRegistrar { this.beanFactory = (BeanFactory) this.registry; } - void register(Class type, MergedAnnotation annotation, boolean deduceBindConstructor) { - MergedAnnotation typeAnnotation = MergedAnnotations + void register(Class type) { + MergedAnnotation annotation = MergedAnnotations .from(type, SearchStrategy.TYPE_HIERARCHY).get(ConfigurationProperties.class); - annotation = (!typeAnnotation.isPresent()) ? annotation : typeAnnotation; - annotation = (annotation != null) ? annotation : MergedAnnotation.missing(); + register(type, annotation); + } + + void register(Class type, MergedAnnotation annotation) { String name = getName(type, annotation); if (!containsBeanDefinition(name)) { - registerBeanDefinition(name, type, annotation, deduceBindConstructor); + registerBeanDefinition(name, type, annotation); } } @@ -78,20 +81,19 @@ final class ConfigurationPropertiesBeanRegistrar { } private void registerBeanDefinition(String beanName, Class type, - MergedAnnotation annotation, boolean deduceBindConstructor) { + MergedAnnotation annotation) { Assert.state(annotation.isPresent(), () -> "No " + ConfigurationProperties.class.getSimpleName() + " annotation found on '" + type.getName() + "'."); - this.registry.registerBeanDefinition(beanName, - createBeanDefinition(beanName, type, annotation, deduceBindConstructor)); + this.registry.registerBeanDefinition(beanName, createBeanDefinition(beanName, type)); } - private BeanDefinition createBeanDefinition(String beanName, Class type, - MergedAnnotation annotation, boolean deduceBindConstructor) { - if (BindMethod.forType(type, deduceBindConstructor) == BindMethod.VALUE_OBJECT) { - return new ConfigurationPropertiesValueObjectBeanDefinition(this.beanFactory, beanName, type, annotation, - deduceBindConstructor); + private BeanDefinition createBeanDefinition(String beanName, Class type) { + if (BindMethod.forType(type) == BindMethod.VALUE_OBJECT) { + return new ConfigurationPropertiesValueObjectBeanDefinition(this.beanFactory, beanName, type); } - return new ConfigurationPropertiesBeanDefinition(type, annotation); + GenericBeanDefinition definition = new GenericBeanDefinition(); + definition.setBeanClass(type); + return definition; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindConstructorProvider.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindConstructorProvider.java index eed5ed737c..d9e4723c66 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindConstructorProvider.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindConstructorProvider.java @@ -21,7 +21,6 @@ import java.lang.reflect.Constructor; import org.springframework.beans.BeanUtils; import org.springframework.boot.context.properties.bind.BindConstructorProvider; import org.springframework.boot.context.properties.bind.Bindable; -import org.springframework.core.Conventions; import org.springframework.core.KotlinDetector; import org.springframework.core.annotation.MergedAnnotations; import org.springframework.util.Assert; @@ -37,35 +36,19 @@ class ConfigurationPropertiesBindConstructorProvider implements BindConstructorP static final ConfigurationPropertiesBindConstructorProvider INSTANCE = new ConfigurationPropertiesBindConstructorProvider(); - static final String DEDUCE_BIND_CONSTRUCTOR_ATTRIUBTE = Conventions - .getQualifiedAttributeName(ConfigurationPropertiesBindConstructorProvider.class, "deduceBindConstructor"); - @Override public Constructor getBindConstructor(Bindable bindable, boolean isNestedConstructorBinding) { - Boolean deduceBindConstructor = (Boolean) bindable.getAttribute(DEDUCE_BIND_CONSTRUCTOR_ATTRIUBTE); - return getBindConstructor(bindable.getType().resolve(), Boolean.TRUE.equals(deduceBindConstructor), - isNestedConstructorBinding); + return getBindConstructor(bindable.getType().resolve(), isNestedConstructorBinding); } - Constructor getBindConstructor(Class type, boolean deduceBindConstructor, - boolean isNestedConstructorBinding) { + Constructor getBindConstructor(Class type, boolean isNestedConstructorBinding) { if (type == null) { return null; } Constructor constructor = findConstructorBindingAnnotatedConstructor(type); - if (constructor != null) { - return constructor; - } - boolean isConstructorBindingAnnotatedType = isConstructorBindingAnnotatedType(type); - if (deduceBindConstructor || isNestedConstructorBinding || isConstructorBindingAnnotatedType) { + if (constructor == null && (isConstructorBindingAnnotatedType(type) || isNestedConstructorBinding)) { constructor = deduceBindConstructor(type); } - if (deduceBindConstructor && isConstructorBindingAnnotatedType && !isNestedConstructorBinding) { - Assert.state(constructor != null, - () -> "Unable to deduce constructor for @ConstructorBinding class " + type.getName()); - Assert.state(constructor.getParameterCount() > 0, - () -> "Deduced no-args constructor for @ConstructorBinding class " + type.getName()); - } return constructor; } @@ -103,7 +86,7 @@ class ConfigurationPropertiesBindConstructorProvider implements BindConstructorP return deducedKotlinBindConstructor(type); } Constructor[] constructors = type.getDeclaredConstructors(); - if (constructors.length == 1) { + if (constructors.length == 1 && constructors[0].getParameterCount() > 0) { return constructors[0]; } return null; @@ -111,7 +94,7 @@ class ConfigurationPropertiesBindConstructorProvider implements BindConstructorP private Constructor deducedKotlinBindConstructor(Class type) { Constructor primaryConstructor = BeanUtils.findPrimaryConstructor(type); - if (primaryConstructor != null) { + if (primaryConstructor != null && primaryConstructor.getParameterCount() > 0) { return primaryConstructor; } return null; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java index 0d8ada28bd..c78fc0898a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java @@ -25,7 +25,6 @@ import org.springframework.beans.BeansException; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.FactoryBean; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -207,14 +206,7 @@ class ConfigurationPropertiesBinder { } static ConfigurationPropertiesBinder get(BeanFactory beanFactory) { - try { - return beanFactory.getBean(BEAN_NAME, ConfigurationPropertiesBinder.class); - } - catch (NoSuchBeanDefinitionException ex) { - throw new NoSuchBeanDefinitionException(ex.getBeanName(), - "Unable to find ConfigurationPropertiesBinder bean '" + BEAN_NAME - + "', ensure @EnableConfigurationProperties has been specified"); - } + return beanFactory.getBean(BEAN_NAME, ConfigurationPropertiesBinder.class); } /** diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java index 582f735cf4..334e7f7eec 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java @@ -108,7 +108,7 @@ class ConfigurationPropertiesScanRegistrar implements ImportBeanDefinitionRegist private void register(ConfigurationPropertiesBeanRegistrar registrar, Class type) { if (!isComponent(type)) { - registrar.register(type, null, false); + registrar.register(type); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesValueObjectBeanDefinition.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesValueObjectBeanDefinition.java index 95671a6181..08af79b96d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesValueObjectBeanDefinition.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesValueObjectBeanDefinition.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2019 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. @@ -18,7 +18,7 @@ package org.springframework.boot.context.properties; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.core.annotation.MergedAnnotation; +import org.springframework.beans.factory.support.GenericBeanDefinition; /** * {@link BeanDefinition} that is used for registering @@ -29,30 +29,21 @@ import org.springframework.core.annotation.MergedAnnotation; * @author Madhura Bhave * @author Phillip Webb */ -final class ConfigurationPropertiesValueObjectBeanDefinition extends ConfigurationPropertiesBeanDefinition { +final class ConfigurationPropertiesValueObjectBeanDefinition extends GenericBeanDefinition { private final BeanFactory beanFactory; private final String beanName; - private final boolean deduceBindConstructor; - - ConfigurationPropertiesValueObjectBeanDefinition(BeanFactory beanFactory, String beanName, Class beanClass, - MergedAnnotation annotation, boolean deduceBindConstructor) { - super(beanClass, annotation); + ConfigurationPropertiesValueObjectBeanDefinition(BeanFactory beanFactory, String beanName, Class beanClass) { this.beanFactory = beanFactory; this.beanName = beanName; - this.deduceBindConstructor = deduceBindConstructor; + setBeanClass(beanClass); setInstanceSupplier(this::createBean); } - boolean isDeduceBindConstructor() { - return this.deduceBindConstructor; - } - private Object createBean() { - ConfigurationPropertiesBean bean = ConfigurationPropertiesBean.forValueObject(getBeanClass(), this.beanName, - getAnnotation(this), this.deduceBindConstructor); + ConfigurationPropertiesBean bean = ConfigurationPropertiesBean.forValueObject(getBeanClass(), this.beanName); ConfigurationPropertiesBinder binder = ConfigurationPropertiesBinder.get(this.beanFactory); try { return binder.bindOrCreate(bean); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationProperties.java index 1b2d51488f..f16136cb2b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2019 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. @@ -51,7 +51,6 @@ public @interface EnableConfigurationProperties { * {@link ConfigurationProperties @ConfigurationProperties} annotated beans with * Spring. Standard Spring Beans will also be scanned regardless of this value. * @return {@code @ConfigurationProperties} annotated beans to register - * @see ImportAsConfigurationPropertiesBean */ Class[] value() default {}; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.java index fe093de589..8ce026b280 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.java @@ -37,9 +37,7 @@ class EnableConfigurationPropertiesRegistrar implements ImportBeanDefinitionRegi public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { registerInfrastructureBeans(registry); ConfigurationPropertiesBeanRegistrar beanRegistrar = new ConfigurationPropertiesBeanRegistrar(registry); - for (Class type : getTypes(metadata)) { - beanRegistrar.register(type, null, false); - } + getTypes(metadata).forEach(beanRegistrar::register); } private Set> getTypes(AnnotationMetadata metadata) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBean.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBean.java deleted file mode 100644 index 442b53d767..0000000000 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBean.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.context.properties; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Repeatable; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.core.annotation.AliasFor; - -/** - * Imports classes as {@link ConfigurationProperties @ConfigurationProperties} beans. Can - * be used to import {@link ConfigurationProperties @ConfigurationProperties} annotated - * types or third-party classes as configuration property beans. - *

- * Classes imported via this annotation that have a default constructor will use - * {@code setter} binding, those with a non-default constructor will use - * {@link ConstructorBinding @ConstructorBinding}. If you are looking to inject beans into - * a constructor, you should use a regular {@link Configuration @Configuration} class - * {@code @Bean} method instead. - *

- * The {@code @ConfigurationProperties} alias attributes defined on this class will only - * be used if the imported class is not itself annotated - * with{@code @ConfigurationProperties}. - * - * @author Phillip Webb - * @since 2.4.0 - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@EnableConfigurationProperties -@ConfigurationProperties -@Repeatable(ImportAsConfigurationPropertiesBeans.class) -@Import(ImportAsConfigurationPropertiesBeanRegistrar.class) -public @interface ImportAsConfigurationPropertiesBean { - - /** - * One or more types that should be imported as a bean. - * @return the types to import - */ - @AliasFor("type") - Class[] value() default {}; - - /** - * One or more types that should be imported as a bean. - * @return the types to import - */ - @AliasFor("value") - Class[] type() default {}; - - /** - * The prefix of the properties that are valid to bind to this object. A valid prefix - * is defined by one or more words separated with dots (e.g. - * {@code "acme.system.feature"}). - * @return the prefix of the properties to bind - * @see ConfigurationProperties#prefix() - */ - @AliasFor(annotation = ConfigurationProperties.class) - String prefix() default ""; - - /** - * Flag to indicate that when binding to this object invalid fields should be ignored. - * Invalid means invalid according to the binder that is used, and usually this means - * fields of the wrong type (or that cannot be coerced into the correct type). - * @return the flag value (default false) - * @see ConfigurationProperties#ignoreInvalidFields() - */ - @AliasFor(annotation = ConfigurationProperties.class) - boolean ignoreInvalidFields() default false; - - /** - * Flag to indicate that when binding to this object unknown fields should be ignored. - * An unknown field could be a sign of a mistake in the Properties. - * @return the flag value (default true) - * @see ConfigurationProperties#ignoreUnknownFields() - */ - @AliasFor(annotation = ConfigurationProperties.class) - boolean ignoreUnknownFields() default true; - -} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeanRegistrar.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeanRegistrar.java deleted file mode 100644 index d2d60c4a2b..0000000000 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeanRegistrar.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.context.properties; - -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.beans.factory.support.BeanNameGenerator; -import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; -import org.springframework.core.annotation.MergedAnnotation; -import org.springframework.core.annotation.MergedAnnotations; -import org.springframework.core.type.AnnotationMetadata; -import org.springframework.util.Assert; -import org.springframework.util.ObjectUtils; - -/** - * {@link ImportBeanDefinitionRegistrar} for - * {@link ImportAsConfigurationPropertiesBean @ImportAsConfigurationPropertiesBean}. - * - * @author Phillip Webb - */ -class ImportAsConfigurationPropertiesBeanRegistrar implements ImportBeanDefinitionRegistrar { - - @Override - public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, - BeanNameGenerator importBeanNameGenerator) { - try { - ConfigurationPropertiesBeanRegistrar registrar = new ConfigurationPropertiesBeanRegistrar(registry); - MergedAnnotations annotations = importingClassMetadata.getAnnotations(); - registerBeans(registrar, annotations.get(ImportAsConfigurationPropertiesBeans.class)); - registerBean(registrar, annotations.get(ImportAsConfigurationPropertiesBean.class)); - } - catch (RuntimeException ex) { - throw new IllegalStateException("Unable process @ImportAsConfigurationPropertiesBean annotations from " - + importingClassMetadata.getClassName(), ex); - } - } - - private void registerBeans(ConfigurationPropertiesBeanRegistrar registrar, - MergedAnnotation annotation) { - if (!annotation.isPresent()) { - return; - } - for (MergedAnnotation containedAnnotation : annotation - .getAnnotationArray(MergedAnnotation.VALUE, ImportAsConfigurationPropertiesBean.class)) { - registerBean(registrar, containedAnnotation); - } - } - - private void registerBean(ConfigurationPropertiesBeanRegistrar registrar, - MergedAnnotation annotation) { - if (!annotation.isPresent()) { - return; - } - Class[] types = annotation.getClassArray("type"); - Assert.state(!ObjectUtils.isEmpty(types), "@ImportAsConfigurationPropertiesBean must declare types to import"); - MergedAnnotation configurationPropertiesAnnotation = MergedAnnotations - .from(annotation.synthesize()).get(ConfigurationProperties.class); - for (Class type : types) { - registrar.register(type, configurationPropertiesAnnotation, true); - } - } - -} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeans.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeans.java deleted file mode 100644 index 53a1aaf2c3..0000000000 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeans.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.context.properties; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.springframework.context.annotation.Import; - -/** - * Container annotation that aggregates several - * {@link ImportAsConfigurationPropertiesBean @ImportAsConfigurationPropertiesBean} - * annotations. - * - * @author Phillip Webb - * @since 2.4.0 - * @see ImportAsConfigurationPropertiesBean - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@EnableConfigurationProperties -@Import(ImportAsConfigurationPropertiesBeanRegistrar.class) -public @interface ImportAsConfigurationPropertiesBeans { - - /** - * The contained - * {@link ImportAsConfigurationPropertiesBean @ImportAsConfigurationPropertiesBean} - * annotations. - * @return the contained annotations - */ - ImportAsConfigurationPropertiesBean[] value(); - -} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java index e307b5256a..a7f2bdbaaf 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2019 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. @@ -18,8 +18,6 @@ package org.springframework.boot.context.properties.bind; import java.lang.annotation.Annotation; import java.lang.reflect.Array; -import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -44,8 +42,6 @@ public final class Bindable { private static final Annotation[] NO_ANNOTATIONS = {}; - private static final Map NO_ATTRIBUTES = Collections.emptyMap(); - private final ResolvableType type; private final ResolvableType boxedType; @@ -54,15 +50,11 @@ public final class Bindable { private final Annotation[] annotations; - private final Map attributes; - - private Bindable(ResolvableType type, ResolvableType boxedType, Supplier value, Annotation[] annotations, - Map attributes) { + private Bindable(ResolvableType type, ResolvableType boxedType, Supplier value, Annotation[] annotations) { this.type = type; this.boxedType = boxedType; this.value = value; this.annotations = annotations; - this.attributes = attributes; } /** @@ -113,16 +105,6 @@ public final class Bindable { return null; } - /** - * Return the value of an attribute that has been associated with this - * {@link Bindable}. - * @param name the attribute name - * @return the associated attribute value or {@code null} - */ - public Object getAttribute(String name) { - return this.attributes.get(name); - } - @Override public boolean equals(Object obj) { if (this == obj) { @@ -135,7 +117,6 @@ public final class Bindable { boolean result = true; result = result && nullSafeEquals(this.type.resolve(), other.type.resolve()); result = result && nullSafeEquals(this.annotations, other.annotations); - result = result && nullSafeEquals(this.attributes, other.attributes); return result; } @@ -145,7 +126,6 @@ public final class Bindable { int result = 1; result = prime * result + ObjectUtils.nullSafeHashCode(this.type); result = prime * result + ObjectUtils.nullSafeHashCode(this.annotations); - result = prime * result + ObjectUtils.nullSafeHashCode(this.attributes); return result; } @@ -155,7 +135,6 @@ public final class Bindable { creator.append("type", this.type); creator.append("value", (this.value != null) ? "provided" : "none"); creator.append("annotations", this.annotations); - creator.append("attributes", this.attributes); return creator.toString(); } @@ -170,19 +149,7 @@ public final class Bindable { */ public Bindable withAnnotations(Annotation... annotations) { return new Bindable<>(this.type, this.boxedType, this.value, - (annotations != null) ? annotations : NO_ANNOTATIONS, this.attributes); - } - - /** - * Create an updated {@link Bindable} instance with the specified attribute. - * @param name the attribute name - * @param value the attribute value - * @return an updated {@link Bindable} - */ - public Bindable withAttribute(String name, Object value) { - Map attributes = new HashMap<>(this.attributes); - attributes.put(name, value); - return new Bindable<>(this.type, this.boxedType, this.value, this.annotations, attributes); + (annotations != null) ? annotations : NO_ANNOTATIONS); } /** @@ -195,7 +162,7 @@ public final class Bindable { existingValue == null || this.type.isArray() || this.boxedType.resolve().isInstance(existingValue), () -> "ExistingValue must be an instance of " + this.type); Supplier value = (existingValue != null) ? () -> existingValue : null; - return new Bindable<>(this.type, this.boxedType, value, this.annotations, this.attributes); + return new Bindable<>(this.type, this.boxedType, value, this.annotations); } /** @@ -204,7 +171,7 @@ public final class Bindable { * @return an updated {@link Bindable} */ public Bindable withSuppliedValue(Supplier suppliedValue) { - return new Bindable<>(this.type, this.boxedType, suppliedValue, this.annotations, this.attributes); + return new Bindable<>(this.type, this.boxedType, suppliedValue, this.annotations); } /** @@ -277,7 +244,7 @@ public final class Bindable { public static Bindable of(ResolvableType type) { Assert.notNull(type, "Type must not be null"); ResolvableType boxedType = box(type); - return new Bindable<>(type, boxedType, null, NO_ANNOTATIONS, NO_ATTRIBUTES); + return new Bindable<>(type, boxedType, null, NO_ANNOTATIONS); } private static ResolvableType box(ResolvableType type) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanDefinitionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanDefinitionTests.java deleted file mode 100644 index f9a9752303..0000000000 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanDefinitionTests.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.context.properties; - -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.GenericBeanDefinition; -import org.springframework.core.annotation.MergedAnnotation; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Tests for {@link ConfigurationPropertiesBeanDefinition}. - * - * @author Phillip Webb - */ -class ConfigurationPropertiesBeanDefinitionTests { - - @Test - void getAnnotationGetsAnnotation() { - MergedAnnotation annotation = MergedAnnotation.of(ConfigurationProperties.class); - BeanDefinition definition = new ConfigurationPropertiesBeanDefinition(Example.class, annotation); - assertThat(ConfigurationPropertiesBeanDefinition.getAnnotation(definition)).isSameAs(annotation); - } - - @Test - void getAnnotationWhenNullReturnsMissing() { - assertThat(ConfigurationPropertiesBeanDefinition.getAnnotation(null)).isEqualTo(MergedAnnotation.missing()); - } - - @Test - void getAnnotationWhenNoAttributeReturnsMissing() { - GenericBeanDefinition definition = new GenericBeanDefinition(); - assertThat(ConfigurationPropertiesBeanDefinition.getAnnotation(definition)) - .isEqualTo(MergedAnnotation.missing()); - } - - static class Example { - - } - -} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrarTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrarTests.java index a5fd998977..865c41f640 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrarTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrarTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2019 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. @@ -22,7 +22,6 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; -import org.springframework.core.annotation.MergedAnnotation; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; @@ -43,7 +42,7 @@ class ConfigurationPropertiesBeanRegistrarTests { @Test void registerWhenNotAlreadyRegisteredAddBeanDefinition() { String beanName = "beancp-" + BeanConfigurationProperties.class.getName(); - this.registrar.register(BeanConfigurationProperties.class, null, false); + this.registrar.register(BeanConfigurationProperties.class); BeanDefinition definition = this.registry.getBeanDefinition(beanName); assertThat(definition).isNotNull(); assertThat(definition.getBeanClassName()).isEqualTo(BeanConfigurationProperties.class.getName()); @@ -53,7 +52,7 @@ class ConfigurationPropertiesBeanRegistrarTests { void registerWhenAlreadyContainsNameDoesNotReplace() { String beanName = "beancp-" + BeanConfigurationProperties.class.getName(); this.registry.registerBeanDefinition(beanName, new GenericBeanDefinition()); - this.registrar.register(BeanConfigurationProperties.class, null, false); + this.registrar.register(BeanConfigurationProperties.class); BeanDefinition definition = this.registry.getBeanDefinition(beanName); assertThat(definition).isNotNull(); assertThat(definition.getBeanClassName()).isNull(); @@ -62,42 +61,24 @@ class ConfigurationPropertiesBeanRegistrarTests { @Test void registerWhenNoAnnotationThrowsException() { assertThatIllegalStateException() - .isThrownBy(() -> this.registrar.register(NoAnnotationConfigurationProperties.class, null, false)) + .isThrownBy(() -> this.registrar.register(NoAnnotationConfigurationProperties.class)) .withMessageContaining("No ConfigurationProperties annotation found"); } @Test void registerWhenValueObjectRegistersValueObjectBeanDefinition() { String beanName = "valuecp-" + ValueObjectConfigurationProperties.class.getName(); - this.registrar.register(ValueObjectConfigurationProperties.class, null, false); + this.registrar.register(ValueObjectConfigurationProperties.class); BeanDefinition definition = this.registry.getBeanDefinition(beanName); assertThat(definition).isInstanceOf(ConfigurationPropertiesValueObjectBeanDefinition.class); } @Test - void registerWhenNotValueObjectRegistersConfigurationPropertiesBeanDefinition() { + void registerWhenNotValueObjectRegistersGenericBeanDefinition() { String beanName = MultiConstructorBeanConfigurationProperties.class.getName(); - this.registrar.register(MultiConstructorBeanConfigurationProperties.class, null, false); + this.registrar.register(MultiConstructorBeanConfigurationProperties.class); BeanDefinition definition = this.registry.getBeanDefinition(beanName); - assertThat(definition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); - } - - @Test - void registerWhenDeduceBindConstructorRegistersValueObjectBeanDefinition() { - String beanName = DeducedValueObjectConfigurationProperties.class.getName(); - MergedAnnotation annotation = MergedAnnotation.of(ConfigurationProperties.class); - this.registrar.register(DeducedValueObjectConfigurationProperties.class, annotation, true); - BeanDefinition definition = this.registry.getBeanDefinition(beanName); - assertThat(definition).isExactlyInstanceOf(ConfigurationPropertiesValueObjectBeanDefinition.class); - } - - @Test - void registerWhenDeduceBindConstructorRegistersJavaBeanObjectBeanDefinition() { - String beanName = DeducedJavaBeanConfigurationProperties.class.getName(); - MergedAnnotation annotation = MergedAnnotation.of(ConfigurationProperties.class); - this.registrar.register(DeducedJavaBeanConfigurationProperties.class, annotation, true); - BeanDefinition definition = this.registry.getBeanDefinition(beanName); - assertThat(definition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); + assertThat(definition).isInstanceOf(GenericBeanDefinition.class); } @ConfigurationProperties(prefix = "beancp") @@ -129,15 +110,4 @@ class ConfigurationPropertiesBeanRegistrarTests { } - static class DeducedValueObjectConfigurationProperties { - - DeducedValueObjectConfigurationProperties(String name) { - } - - } - - static class DeducedJavaBeanConfigurationProperties { - - } - } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java index a8b3e300c1..867b68150f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2019 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. @@ -30,7 +30,6 @@ import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportSelector; import org.springframework.context.annotation.Lazy; import org.springframework.core.ResolvableType; -import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.type.AnnotationMetadata; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; @@ -203,8 +202,8 @@ class ConfigurationPropertiesBeanTests { @Test void forValueObjectReturnsBean() { - ConfigurationPropertiesBean propertiesBean = ConfigurationPropertiesBean.forValueObject( - ConstructorBindingOnConstructor.class, "valueObjectBean", MergedAnnotation.missing(), false); + ConfigurationPropertiesBean propertiesBean = ConfigurationPropertiesBean + .forValueObject(ConstructorBindingOnConstructor.class, "valueObjectBean"); assertThat(propertiesBean.getName()).isEqualTo("valueObjectBean"); assertThat(propertiesBean.getInstance()).isNull(); assertThat(propertiesBean.getType()).isEqualTo(ConstructorBindingOnConstructor.class); @@ -214,34 +213,17 @@ class ConfigurationPropertiesBeanTests { assertThat(target.getType()).isEqualTo(ResolvableType.forClass(ConstructorBindingOnConstructor.class)); assertThat(target.getValue()).isNull(); assertThat(ConfigurationPropertiesBindConstructorProvider.INSTANCE - .getBindConstructor(ConstructorBindingOnConstructor.class, false, false)).isNotNull(); - } - - @Test - void forValueObjectWhenDeduceConstructorReturnsBean() { - ConfigurationPropertiesBean propertiesBean = ConfigurationPropertiesBean - .forValueObject(DeducedConstructorBinding.class, "valueObjectBean", MergedAnnotation.missing(), true); - assertThat(propertiesBean.getName()).isEqualTo("valueObjectBean"); - assertThat(propertiesBean.getInstance()).isNull(); - assertThat(propertiesBean.getType()).isEqualTo(DeducedConstructorBinding.class); - assertThat(propertiesBean.getBindMethod()).isEqualTo(BindMethod.VALUE_OBJECT); - assertThat(propertiesBean.getAnnotation()).isNotNull(); - Bindable target = propertiesBean.asBindTarget(); - assertThat(target.getType()).isEqualTo(ResolvableType.forClass(DeducedConstructorBinding.class)); - assertThat(target.getValue()).isNull(); - assertThat(ConfigurationPropertiesBindConstructorProvider.INSTANCE - .getBindConstructor(ConstructorBindingOnConstructor.class, false, false)).isNotNull(); + .getBindConstructor(ConstructorBindingOnConstructor.class, false)).isNotNull(); } @Test void forValueObjectWhenJavaBeanBindTypeThrowsException() { assertThatIllegalStateException() - .isThrownBy(() -> ConfigurationPropertiesBean.forValueObject(AnnotatedBean.class, "annotatedBean", - MergedAnnotation.missing(), false)) + .isThrownBy(() -> ConfigurationPropertiesBean.forValueObject(AnnotatedBean.class, "annotatedBean")) .withMessage("Bean 'annotatedBean' is not a @ConfigurationProperties value object"); assertThatIllegalStateException() - .isThrownBy(() -> ConfigurationPropertiesBean.forValueObject(NonAnnotatedBean.class, "nonAnnotatedBean", - MergedAnnotation.missing(), false)) + .isThrownBy( + () -> ConfigurationPropertiesBean.forValueObject(NonAnnotatedBean.class, "nonAnnotatedBean")) .withMessage("Bean 'nonAnnotatedBean' is not a @ConfigurationProperties value object"); } @@ -259,7 +241,7 @@ class ConfigurationPropertiesBeanTests { } @Test - void bindTypeForTypeWhenConstructorBindingOnConstructorReturnsValueObject() { + void bindTypeForTypeWhenNoConstructorBindingOnConstructorReturnsValueObject() { BindMethod bindType = BindMethod.forType(ConstructorBindingOnConstructor.class); assertThat(bindType).isEqualTo(BindMethod.VALUE_OBJECT); } @@ -272,18 +254,6 @@ class ConfigurationPropertiesBeanTests { + " has more than one @ConstructorBinding constructor"); } - @Test - void bindTypeForTypeWhenDeducedConstructorBindingOnValueObjectReturnsValueObject() { - BindMethod bindType = BindMethod.forType(DeducedConstructorBinding.class, true); - assertThat(bindType).isEqualTo(BindMethod.VALUE_OBJECT); - } - - @Test - void bindTypeForTypeWhenDeducedConstructorBindingOnJavaBeanReturnsJavABean() { - BindMethod bindType = BindMethod.forType(NonAnnotatedBean.class, true); - assertThat(bindType).isEqualTo(BindMethod.JAVA_BEAN); - } - private void get(Class configuration, String beanName, ThrowingConsumer consumer) throws Throwable { get(configuration, beanName, true, consumer); @@ -504,14 +474,6 @@ class ConfigurationPropertiesBeanTests { } - @ConfigurationProperties - static class DeducedConstructorBinding { - - DeducedConstructorBinding(String name) { - } - - } - @Configuration(proxyBeanMethods = false) @Import(NonAnnotatedBeanConfigurationImportSelector.class) static class NonAnnotatedBeanImportConfiguration { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java index c527f6d2df..553c0a7c08 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.boot.context.properties.scan.combined.c.CombinedConfiguration; import org.springframework.boot.context.properties.scan.combined.d.OtherCombinedConfiguration; import org.springframework.boot.context.properties.scan.valid.ConfigurationPropertiesScanConfiguration; @@ -52,8 +53,8 @@ class ConfigurationPropertiesScanRegistrarTests { "foo-org.springframework.boot.context.properties.scan.valid.ConfigurationPropertiesScanConfiguration$FooProperties"); BeanDefinition barDefinition = this.beanFactory.getBeanDefinition( "bar-org.springframework.boot.context.properties.scan.valid.ConfigurationPropertiesScanConfiguration$BarProperties"); - assertThat(bingDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); - assertThat(fooDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); + assertThat(bingDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); + assertThat(fooDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); assertThat(barDefinition).isExactlyInstanceOf(ConfigurationPropertiesValueObjectBeanDefinition.class); } @@ -65,7 +66,7 @@ class ConfigurationPropertiesScanRegistrarTests { getAnnotationMetadata(ConfigurationPropertiesScanConfiguration.TestConfiguration.class), beanFactory); BeanDefinition fooDefinition = beanFactory.getBeanDefinition( "foo-org.springframework.boot.context.properties.scan.valid.ConfigurationPropertiesScanConfiguration$FooProperties"); - assertThat(fooDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); + assertThat(fooDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); } @Test @@ -84,11 +85,11 @@ class ConfigurationPropertiesScanRegistrarTests { "b.first-org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration$BFirstProperties"); BeanDefinition bSecondDefinition = beanFactory.getBeanDefinition( "b.second-org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration$BSecondProperties"); - assertThat(aDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); + assertThat(aDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); // Constructor injection assertThat(bFirstDefinition).isExactlyInstanceOf(ConfigurationPropertiesValueObjectBeanDefinition.class); // Post-processing injection - assertThat(bSecondDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); + assertThat(bSecondDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); } @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrarTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrarTests.java index 913822ab05..d9c0442e89 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrarTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrarTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2019 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. @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.core.type.AnnotationMetadata; import static org.assertj.core.api.Assertions.assertThat; @@ -51,11 +52,11 @@ class EnableConfigurationPropertiesRegistrarTests { } @Test - void typeWithDefaultConstructorShouldRegisterConfigurationPropertiesBeanDefinition() throws Exception { + void typeWithDefaultConstructorShouldRegisterGenericBeanDefinition() throws Exception { register(TestConfiguration.class); BeanDefinition beanDefinition = this.beanFactory .getBeanDefinition("foo-" + getClass().getName() + "$FooProperties"); - assertThat(beanDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); + assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); } @Test @@ -67,11 +68,11 @@ class EnableConfigurationPropertiesRegistrarTests { } @Test - void typeWithMultipleConstructorsShouldRegisterConfigurationPropertiesBeanDefinition() throws Exception { + void typeWithMultipleConstructorsShouldRegisterGenericBeanDefinition() throws Exception { register(TestConfiguration.class); BeanDefinition beanDefinition = this.beanFactory .getBeanDefinition("bing-" + getClass().getName() + "$BingProperties"); - assertThat(beanDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); + assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); } @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeanTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeanTests.java deleted file mode 100644 index ca4c2dd0a4..0000000000 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ImportAsConfigurationPropertiesBeanTests.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright 2012-2020 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 - * - * https://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.context.properties; - -import org.junit.jupiter.api.Test; - -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.support.TestPropertySourceUtils; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalStateException; - -/** - * Tests for {@link ImportAsConfigurationPropertiesBean}. - * - * @author Phillip Webb - */ -class ImportAsConfigurationPropertiesBeanTests { - - @Test - void importJavaBean() { - try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { - TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context.getEnvironment(), "test.name=spring"); - context.register(JavaBeanConfig.class); - context.refresh(); - assertThat(context.getBean(JavaBean.class).getName()).isEqualTo("spring"); - } - } - - @Test - void importValueObject() { - try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { - TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context.getEnvironment(), "test.value=spring"); - context.register(ValueObjectConfig.class); - context.refresh(); - assertThat(context.getBean(ValueObject.class).getValue()).isEqualTo("spring"); - } - } - - @Test - void importMultiConstructorValueObjectFails() { - try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { - TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context.getEnvironment(), "test.name=spring"); - context.register(MultiConstructorValueObjectConfig.class); - assertThatIllegalStateException().isThrownBy(context::refresh).havingCause() - .withMessageContaining("Unable to deduce"); - } - } - - @Test - void importMultipleTypes() { - try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { - TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context.getEnvironment(), "test.name=spring", - "test.value=boot"); - context.register(ImportMultipleTypesConfig.class); - context.refresh(); - assertThat(context.getBean(JavaBean.class).getName()).isEqualTo("spring"); - assertThat(context.getBean(ValueObject.class).getValue()).isEqualTo("boot"); - } - } - - @Test - void importRepeatedAnnotations() { - try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { - TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context.getEnvironment(), "jb.name=spring", - "vo.value=boot"); - context.register(ImportRepeatedAnnotationsConfig.class); - context.refresh(); - assertThat(context.getBean(JavaBean.class).getName()).isEqualTo("spring"); - assertThat(context.getBean(ValueObject.class).getValue()).isEqualTo("boot"); - } - } - - @Test - void importAnnoatedBeanConfig() { - try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { - TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context.getEnvironment(), "onbean.name=spring"); - context.register(ImportAnnotatedClassConfig.class); - context.refresh(); - assertThat(context.getBean(JavaBean.class).getName()).isEqualTo("spring"); - } - } - - @Configuration - @ImportAsConfigurationPropertiesBean(prefix = "test", type = JavaBean.class) - static class JavaBeanConfig { - - } - - static class JavaBean { - - private String name; - - String getName() { - return this.name; - } - - void setName(String name) { - this.name = name; - } - - } - - @Configuration - @ImportAsConfigurationPropertiesBean(prefix = "test", type = ValueObject.class) - static class ValueObjectConfig { - - } - - static class ValueObject { - - private final String value; - - ValueObject(String value) { - this.value = value; - } - - String getValue() { - return this.value; - } - - } - - @Configuration - @ImportAsConfigurationPropertiesBean(prefix = "test", type = MultiConstructorValueObject.class) - static class MultiConstructorValueObjectConfig { - - } - - static class MultiConstructorValueObject { - - MultiConstructorValueObject() { - } - - MultiConstructorValueObject(String name) { - } - - MultiConstructorValueObject(String name, int age) { - } - - } - - @Configuration - @ImportAsConfigurationPropertiesBean(type = { ValueObject.class, JavaBean.class }, prefix = "test") - static class ImportMultipleTypesConfig { - - } - - @Configuration - @ImportAsConfigurationPropertiesBean(type = ValueObject.class, prefix = "vo") - @ImportAsConfigurationPropertiesBean(type = JavaBean.class, prefix = "jb") - static class ImportRepeatedAnnotationsConfig { - - } - - @Configuration - @ImportAsConfigurationPropertiesBean(AnnotatedJavaBean.class) - static class ImportAnnotatedClassConfig { - - } - - @ConfigurationProperties(prefix = "onbean") - static class AnnotatedJavaBean extends JavaBean { - - } - -} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java index 701689abe7..f44a5518f0 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java @@ -139,19 +139,6 @@ class BindableTests { assertThat(Bindable.of(String.class).withAnnotations(annotation).getAnnotation(Bean.class)).isNull(); } - @Test - void withAttributeShouldSetAttribute() { - Bindable bindable = Bindable.of(String.class); - Bindable withOne = bindable.withAttribute("one", 1); - Bindable withOneAndTwo = withOne.withAttribute("two", 2); - assertThat(bindable.getAttribute("one")).isNull(); - assertThat(bindable.getAttribute("two")).isNull(); - assertThat(withOne.getAttribute("one")).isEqualTo(1); - assertThat(withOne.getAttribute("two")).isNull(); - assertThat(withOneAndTwo.getAttribute("one")).isEqualTo(1); - assertThat(withOneAndTwo.getAttribute("two")).isEqualTo(2); - } - @Test void toStringShouldShowDetails() { Annotation annotation = AnnotationUtils.synthesizeAnnotation(TestAnnotation.class); @@ -167,10 +154,9 @@ class BindableTests { Bindable bindable1 = Bindable.of(String.class).withExistingValue("foo").withAnnotations(annotation); Bindable bindable2 = Bindable.of(String.class).withExistingValue("foo").withAnnotations(annotation); Bindable bindable3 = Bindable.of(String.class).withExistingValue("fof").withAnnotations(annotation); - Bindable bindable4 = Bindable.of(String.class).withExistingValue("foo").withAnnotations(annotation) - .withAttribute("bar", "bar"); assertThat(bindable1.hashCode()).isEqualTo(bindable2.hashCode()); - assertThat(bindable1).isEqualTo(bindable1).isEqualTo(bindable2).isEqualTo(bindable3).isNotEqualTo(bindable4); + assertThat(bindable1).isEqualTo(bindable1).isEqualTo(bindable2); + assertThat(bindable1).isEqualTo(bindable3); } @Test // gh-18218 diff --git a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinConfigurationPropertiesBeanRegistrarTests.kt b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinConfigurationPropertiesBeanRegistrarTests.kt index 38d40a9dd0..1144590a6f 100644 --- a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinConfigurationPropertiesBeanRegistrarTests.kt +++ b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinConfigurationPropertiesBeanRegistrarTests.kt @@ -3,6 +3,7 @@ package org.springframework.boot.context.properties import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.springframework.beans.factory.support.DefaultListableBeanFactory +import org.springframework.beans.factory.support.GenericBeanDefinition import org.springframework.core.type.AnnotationMetadata import org.springframework.core.type.classreading.SimpleMetadataReaderFactory @@ -20,15 +21,15 @@ class KotlinConfigurationPropertiesBeanRegistrarTests { @Test fun `type with default constructor should register generic bean definition`() { - this.registrar.register(FooProperties::class.java, null, false) + this.registrar.register(FooProperties::class.java) val beanDefinition = this.beanFactory.getBeanDefinition( "foo-org.springframework.boot.context.properties.KotlinConfigurationPropertiesBeanRegistrarTests\$FooProperties") - assertThat(beanDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition::class.java) + assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java) } @Test fun `type with primary constructor and no autowired should register configuration properties bean definition`() { - this.registrar.register(BarProperties::class.java, null, false) + this.registrar.register(BarProperties::class.java) val beanDefinition = this.beanFactory.getBeanDefinition( "bar-org.springframework.boot.context.properties.KotlinConfigurationPropertiesBeanRegistrarTests\$BarProperties") assertThat(beanDefinition).isExactlyInstanceOf( @@ -37,10 +38,10 @@ class KotlinConfigurationPropertiesBeanRegistrarTests { @Test fun `type with no primary constructor should register generic bean definition`() { - this.registrar.register(BingProperties::class.java, null, false) + this.registrar.register(BingProperties::class.java) val beanDefinition = this.beanFactory.getBeanDefinition( "bing-org.springframework.boot.context.properties.KotlinConfigurationPropertiesBeanRegistrarTests\$BingProperties") - assertThat(beanDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition::class.java) + assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java) } @ConfigurationProperties(prefix = "foo")