@@ -42,6 +42,14 @@
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<version>1.4.21</version>
|
||||
<configuration>
|
||||
<args>
|
||||
<arg>-Xjsr305=strict</arg>
|
||||
</args>
|
||||
<compilerPlugins>
|
||||
<plugin>spring</plugin>
|
||||
</compilerPlugins>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
@@ -68,6 +76,13 @@
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-allopen</artifactId>
|
||||
<version>1.4.21</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
@@ -36,6 +36,7 @@ import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
@@ -62,7 +63,6 @@ public class KotlinLambdaToFunctionAutoConfiguration {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
|
||||
/**
|
||||
* Will transform all discovered Kotlin's Function lambdas to java
|
||||
* Supplier, Function and Consumer, retaining the original Kotlin type
|
||||
@@ -71,7 +71,7 @@ public class KotlinLambdaToFunctionAutoConfiguration {
|
||||
* @return the bean factory post processor
|
||||
*/
|
||||
@Bean
|
||||
public BeanFactoryPostProcessor kotlinToFunctionTransformer() {
|
||||
public BeanFactoryPostProcessor kotlinToFunctionTransformerOld() {
|
||||
return new BeanFactoryPostProcessor() {
|
||||
|
||||
@Override
|
||||
@@ -82,14 +82,17 @@ public class KotlinLambdaToFunctionAutoConfiguration {
|
||||
for (String beanDefinitionName : beanDefinitionNames) {
|
||||
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName);
|
||||
|
||||
ResolvableType rt = beanDefinition.getResolvableType();
|
||||
if (rt.getType().getTypeName().startsWith("kotlin.jvm.functions.Function")) {
|
||||
RootBeanDefinition cbd = new RootBeanDefinition(KotlinFunctionWrapper.class);
|
||||
ConstructorArgumentValues ca = new ConstructorArgumentValues();
|
||||
ca.addGenericArgumentValue(beanDefinition);
|
||||
cbd.setConstructorArgumentValues(ca);
|
||||
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(beanDefinitionName + FunctionRegistration.REGISTRATION_NAME_SUFFIX, cbd);
|
||||
if (beanDefinition instanceof AnnotatedBeanDefinition && ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata() != null) {
|
||||
String typeName = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata().getReturnTypeName();
|
||||
if (typeName.startsWith("kotlin.jvm.functions.Function")) {
|
||||
RootBeanDefinition cbd = new RootBeanDefinition(KotlinFunctionWrapper.class);
|
||||
ConstructorArgumentValues ca = new ConstructorArgumentValues();
|
||||
ca.addGenericArgumentValue(beanDefinition);
|
||||
cbd.setConstructorArgumentValues(ca);
|
||||
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(beanDefinitionName + FunctionRegistration.REGISTRATION_NAME_SUFFIX, cbd);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -76,6 +76,13 @@ public class ContextFunctionCatalogAutoConfigurationKotlinTests {
|
||||
assertThat(functionType.getRawType().getTypeName()).isEqualTo(Supplier.class.getName());
|
||||
assertThat(functionType.getActualTypeArguments().length).isEqualTo(1);
|
||||
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo(String.class.getName());
|
||||
|
||||
function = this.context.getBean("kotlinPojoFunction");
|
||||
functionType = (ParameterizedType) FunctionTypeUtils.discoverFunctionType(function, "kotlinPojoFunction", this.context);
|
||||
assertThat(functionType.getRawType().getTypeName()).isEqualTo(Function.class.getName());
|
||||
assertThat(functionType.getActualTypeArguments().length).isEqualTo(2);
|
||||
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo(Person.class.getName());
|
||||
assertThat(functionType.getActualTypeArguments()[1].getTypeName()).isEqualTo(String.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -27,24 +27,29 @@ import java.util.function.Function
|
||||
*/
|
||||
@EnableAutoConfiguration
|
||||
@Configuration
|
||||
open class KotlinLambdasConfiguration {
|
||||
class KotlinLambdasConfiguration {
|
||||
@Bean
|
||||
open fun kotlinFunction(): (String) -> String {
|
||||
fun kotlinFunction(): (String) -> String {
|
||||
return { it.toUpperCase() }
|
||||
}
|
||||
|
||||
@Bean
|
||||
fun kotlinPojoFunction(): (Person) -> String {
|
||||
return { it.name.toString()}
|
||||
}
|
||||
|
||||
@Bean
|
||||
open fun kotlinConsumer(): (String) -> Unit {
|
||||
fun kotlinConsumer(): (String) -> Unit {
|
||||
return { println(it) }
|
||||
}
|
||||
|
||||
@Bean
|
||||
open fun kotlinSupplier(): () -> String {
|
||||
fun kotlinSupplier(): () -> String {
|
||||
return { "Hello" }
|
||||
}
|
||||
|
||||
@Bean
|
||||
open fun javaFunction(): Function<String, String> {
|
||||
fun javaFunction(): Function<String, String> {
|
||||
return Function { x -> x }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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.cloud.function.kotlin
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import java.util.function.Function
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
class Person {
|
||||
|
||||
var name:String? = null;
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user