Commit c30f9815 authored by Stephane Nicoll's avatar Stephane Nicoll

Fix detection of Autowired constructor with Kotlin

Previously, the import selector wrongly assumed that we should not
use constructor injection with Kotlin. Rather than looking up for the
primary constructor, we retrieve available constructors on the Java
counter-part.

This commit applies the same logic as in the constructor parameter
binder and checks for the primary constructor for Kotlin types.

See gh-8762
parent 76758023
...@@ -17,11 +17,13 @@ ...@@ -17,11 +17,13 @@
package org.springframework.boot.context.properties; package org.springframework.boot.context.properties;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
...@@ -30,6 +32,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; ...@@ -30,6 +32,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.context.annotation.ImportSelector; import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.KotlinDetector;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -52,6 +55,8 @@ import org.springframework.util.StringUtils; ...@@ -52,6 +55,8 @@ import org.springframework.util.StringUtils;
*/ */
class EnableConfigurationPropertiesImportSelector implements ImportSelector { class EnableConfigurationPropertiesImportSelector implements ImportSelector {
private static boolean KOTLIN_PRESENT = KotlinDetector.isKotlinPresent();
private static final String[] IMPORTS = { private static final String[] IMPORTS = {
ConfigurationPropertiesBeanRegistrar.class.getName(), ConfigurationPropertiesBeanRegistrar.class.getName(),
ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() }; ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() };
...@@ -145,13 +150,29 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector { ...@@ -145,13 +150,29 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector {
} }
private boolean canBindAtCreationTime(Class<?> type) { private boolean canBindAtCreationTime(Class<?> type) {
Constructor<?>[] constructors = type.getDeclaredConstructors(); List<Constructor<?>> constructors = determineConstructors(type);
boolean autowiredPresent = Arrays.stream(constructors).anyMatch( boolean autowiredPresent = constructors.stream().anyMatch(
(c) -> AnnotationUtils.findAnnotation(c, Autowired.class) != null); (c) -> AnnotationUtils.findAnnotation(c, Autowired.class) != null);
if (autowiredPresent) { if (autowiredPresent) {
return false; return false;
} }
return (constructors.length == 1 && constructors[0].getParameterCount() > 0); return (constructors.size() == 1
&& constructors.get(0).getParameterCount() > 0);
}
private List<Constructor<?>> determineConstructors(Class<?> type) {
List<Constructor<?>> constructors = new ArrayList<>();
if (KOTLIN_PRESENT && KotlinDetector.isKotlinType(type)) {
Constructor<?> primaryConstructor = BeanUtils
.findPrimaryConstructor(type);
if (primaryConstructor != null) {
constructors.add(primaryConstructor);
}
}
else {
constructors.addAll(Arrays.asList(type.getDeclaredConstructors()));
}
return constructors;
} }
} }
......
package org.springframework.boot.context.properties
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
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
/**
* Tests for `EnableConfigurationPropertiesImportSelector`.
*
* @author Stephane Nicoll
*/
@Suppress("unused")
class KotlinEnableConfigurationPropertiesImportSelectorTests {
private val registrar = EnableConfigurationPropertiesImportSelector.ConfigurationPropertiesBeanRegistrar()
private val beanFactory = DefaultListableBeanFactory()
@Test
fun `type with default constructor should register generic bean definition`() {
this.registrar.registerBeanDefinitions(
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory)
val beanDefinition = this.beanFactory.getBeanDefinition(
"foo-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$FooProperties")
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java)
}
@Test
fun `type with autowired on constructor should register generic bean definition`() {
this.registrar.registerBeanDefinitions(
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory)
val beanDefinition = this.beanFactory.getBeanDefinition(
"bar-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BarProperties")
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java)
}
@Test
fun `type with primary constructor and no autowired should register configuration properties bean definition`() {
this.registrar.registerBeanDefinitions(
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory)
val beanDefinition = this.beanFactory.getBeanDefinition(
"baz-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BazProperties")
assertThat(beanDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition::class.java)
}
@Test
fun `type with no primary constructor should register generic bean definition`() {
this.registrar.registerBeanDefinitions(
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory)
val beanDefinition = this.beanFactory.getBeanDefinition(
"bing-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BingProperties")
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java)
}
private fun getAnnotationMetadata(source: Class<*>): AnnotationMetadata {
return SimpleMetadataReaderFactory().getMetadataReader(source.name)
.annotationMetadata
}
@EnableConfigurationProperties(FooProperties::class, BarProperties::class, BazProperties::class, BingProperties::class)
class TestConfiguration
@ConfigurationProperties(prefix = "foo")
class FooProperties
@ConfigurationProperties(prefix = "bar")
class BarProperties @Autowired constructor(val foo: String)
@ConfigurationProperties(prefix = "baz")
class BazProperties(val name: String?, val counter: Int = 42)
@ConfigurationProperties(prefix = "bing")
class BingProperties {
constructor()
constructor(@Suppress("UNUSED_PARAMETER") foo: String)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment