@@ -42,6 +42,14 @@
|
|||||||
<artifactId>kotlin-maven-plugin</artifactId>
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<version>1.4.21</version>
|
<version>1.4.21</version>
|
||||||
|
<configuration>
|
||||||
|
<args>
|
||||||
|
<arg>-Xjsr305=strict</arg>
|
||||||
|
</args>
|
||||||
|
<compilerPlugins>
|
||||||
|
<plugin>spring</plugin>
|
||||||
|
</compilerPlugins>
|
||||||
|
</configuration>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>compile</id>
|
<id>compile</id>
|
||||||
@@ -68,6 +76,13 @@
|
|||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-maven-allopen</artifactId>
|
||||||
|
<version>1.4.21</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.BeanFactoryAware;
|
||||||
import org.springframework.beans.factory.BeanNameAware;
|
import org.springframework.beans.factory.BeanNameAware;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
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.BeanDefinition;
|
||||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
@@ -62,7 +63,6 @@ public class KotlinLambdaToFunctionAutoConfiguration {
|
|||||||
|
|
||||||
protected final Log logger = LogFactory.getLog(getClass());
|
protected final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will transform all discovered Kotlin's Function lambdas to java
|
* Will transform all discovered Kotlin's Function lambdas to java
|
||||||
* Supplier, Function and Consumer, retaining the original Kotlin type
|
* Supplier, Function and Consumer, retaining the original Kotlin type
|
||||||
@@ -71,7 +71,7 @@ public class KotlinLambdaToFunctionAutoConfiguration {
|
|||||||
* @return the bean factory post processor
|
* @return the bean factory post processor
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public BeanFactoryPostProcessor kotlinToFunctionTransformer() {
|
public BeanFactoryPostProcessor kotlinToFunctionTransformerOld() {
|
||||||
return new BeanFactoryPostProcessor() {
|
return new BeanFactoryPostProcessor() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -82,14 +82,17 @@ public class KotlinLambdaToFunctionAutoConfiguration {
|
|||||||
for (String beanDefinitionName : beanDefinitionNames) {
|
for (String beanDefinitionName : beanDefinitionNames) {
|
||||||
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName);
|
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName);
|
||||||
|
|
||||||
ResolvableType rt = beanDefinition.getResolvableType();
|
if (beanDefinition instanceof AnnotatedBeanDefinition && ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata() != null) {
|
||||||
if (rt.getType().getTypeName().startsWith("kotlin.jvm.functions.Function")) {
|
String typeName = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata().getReturnTypeName();
|
||||||
RootBeanDefinition cbd = new RootBeanDefinition(KotlinFunctionWrapper.class);
|
if (typeName.startsWith("kotlin.jvm.functions.Function")) {
|
||||||
ConstructorArgumentValues ca = new ConstructorArgumentValues();
|
RootBeanDefinition cbd = new RootBeanDefinition(KotlinFunctionWrapper.class);
|
||||||
ca.addGenericArgumentValue(beanDefinition);
|
ConstructorArgumentValues ca = new ConstructorArgumentValues();
|
||||||
cbd.setConstructorArgumentValues(ca);
|
ca.addGenericArgumentValue(beanDefinition);
|
||||||
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(beanDefinitionName + FunctionRegistration.REGISTRATION_NAME_SUFFIX, cbd);
|
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.getRawType().getTypeName()).isEqualTo(Supplier.class.getName());
|
||||||
assertThat(functionType.getActualTypeArguments().length).isEqualTo(1);
|
assertThat(functionType.getActualTypeArguments().length).isEqualTo(1);
|
||||||
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo(String.class.getName());
|
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
|
@Test
|
||||||
|
|||||||
@@ -27,24 +27,29 @@ import java.util.function.Function
|
|||||||
*/
|
*/
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@Configuration
|
@Configuration
|
||||||
open class KotlinLambdasConfiguration {
|
class KotlinLambdasConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
open fun kotlinFunction(): (String) -> String {
|
fun kotlinFunction(): (String) -> String {
|
||||||
return { it.toUpperCase() }
|
return { it.toUpperCase() }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
open fun kotlinConsumer(): (String) -> Unit {
|
fun kotlinPojoFunction(): (Person) -> String {
|
||||||
|
return { it.name.toString()}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun kotlinConsumer(): (String) -> Unit {
|
||||||
return { println(it) }
|
return { println(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
open fun kotlinSupplier(): () -> String {
|
fun kotlinSupplier(): () -> String {
|
||||||
return { "Hello" }
|
return { "Hello" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
open fun javaFunction(): Function<String, String> {
|
fun javaFunction(): Function<String, String> {
|
||||||
return Function { x -> x }
|
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