Turned on checkstyle
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2018 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,9 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import kotlin.Unit;
|
||||
import kotlin.jvm.functions.Function0;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@@ -36,16 +39,11 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.type.MethodMetadata;
|
||||
|
||||
import kotlin.Unit;
|
||||
import kotlin.jvm.functions.Function0;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
|
||||
/**
|
||||
* Configuration class which defines the required infrastructure to bootstrap Kotlin
|
||||
* lambdas as invocable functions within the context of the framework.
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -58,43 +56,65 @@ class KotlinLambdaToFunctionAutoConfiguration {
|
||||
* Will transform all discovered Kotlin's Function1 and Function0 lambdas to java
|
||||
* Supplier, Function and Consumer, retaining the original Kotlin type
|
||||
* characteristics. In other words the resulting bean could be cast to both java and
|
||||
* kotlin types (i.e., java Function<I,O> vs. kotlin Function1<I,O>)
|
||||
* kotlin types (i.e., java Function<I,O> vs. kotlin Function1<I,O>)
|
||||
* @return the bean factory post processor
|
||||
*/
|
||||
@Bean
|
||||
public BeanFactoryPostProcessor kotlinToFunctionTransformer() {
|
||||
return new BeanFactoryPostProcessor() {
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
public void postProcessBeanFactory(
|
||||
ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
|
||||
String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
|
||||
for (String beanDefinitionName : beanDefinitionNames) {
|
||||
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName);
|
||||
BeanDefinition beanDefinition = beanFactory
|
||||
.getBeanDefinition(beanDefinitionName);
|
||||
Object source = beanDefinition.getSource();
|
||||
if (source instanceof MethodMetadata) {
|
||||
String returnTypeName = ((MethodMetadata)source).getReturnTypeName();
|
||||
String returnTypeName = ((MethodMetadata) source)
|
||||
.getReturnTypeName();
|
||||
if (returnTypeName.startsWith("kotlin.jvm.functions.Function")) {
|
||||
FunctionType functionType = new FunctionType(FunctionContextUtils.findType(beanDefinitionName, beanFactory));
|
||||
FunctionType functionType = new FunctionType(
|
||||
FunctionContextUtils.findType(beanDefinitionName,
|
||||
beanFactory));
|
||||
if (returnTypeName.equals("kotlin.jvm.functions.Function1")) {
|
||||
if (Unit.class.isAssignableFrom(functionType.getOutputType())) {
|
||||
logger.debug("Transforming Kotlin lambda " + beanDefinitionName + " to java Consumer");
|
||||
this.register(beanDefinitionName, beanDefinition, KotlinConsumer.class, (BeanDefinitionRegistry) beanFactory);
|
||||
if (Unit.class
|
||||
.isAssignableFrom(functionType.getOutputType())) {
|
||||
KotlinLambdaToFunctionAutoConfiguration.this.logger
|
||||
.debug("Transforming Kotlin lambda "
|
||||
+ beanDefinitionName
|
||||
+ " to java Consumer");
|
||||
this.register(beanDefinitionName, beanDefinition,
|
||||
KotlinConsumer.class,
|
||||
(BeanDefinitionRegistry) beanFactory);
|
||||
}
|
||||
else {
|
||||
logger.debug("Transforming Kotlin lambda " + beanDefinitionName + " to java Function");
|
||||
this.register(beanDefinitionName, beanDefinition, KotlinFunction.class, (BeanDefinitionRegistry) beanFactory);
|
||||
KotlinLambdaToFunctionAutoConfiguration.this.logger
|
||||
.debug("Transforming Kotlin lambda "
|
||||
+ beanDefinitionName
|
||||
+ " to java Function");
|
||||
this.register(beanDefinitionName, beanDefinition,
|
||||
KotlinFunction.class,
|
||||
(BeanDefinitionRegistry) beanFactory);
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.debug("Transforming Kotlin lambda " + beanDefinitionName + " to java Supplier");
|
||||
this.register(beanDefinitionName, beanDefinition, KotlinSupplier.class, (BeanDefinitionRegistry) beanFactory);
|
||||
else {
|
||||
KotlinLambdaToFunctionAutoConfiguration.this.logger.debug(
|
||||
"Transforming Kotlin lambda " + beanDefinitionName
|
||||
+ " to java Supplier");
|
||||
this.register(beanDefinitionName, beanDefinition,
|
||||
KotlinSupplier.class,
|
||||
(BeanDefinitionRegistry) beanFactory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void register(String originalName, BeanDefinition originalDefinition, Class<?> clazz, BeanDefinitionRegistry registry) {
|
||||
private void register(String originalName, BeanDefinition originalDefinition,
|
||||
Class<?> clazz, BeanDefinitionRegistry registry) {
|
||||
RootBeanDefinition cbd = new RootBeanDefinition(clazz);
|
||||
ConstructorArgumentValues ca = new ConstructorArgumentValues();
|
||||
ca.addGenericArgumentValue(originalDefinition);
|
||||
@@ -106,10 +126,11 @@ class KotlinLambdaToFunctionAutoConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for Kotlin lambda to be represented as both Java Function<I,O> as well as
|
||||
* Kotlin's Function1<I,O>
|
||||
* Wrapper for Kotlin lambda to be represented as both Java Function<I,O> as
|
||||
* well as Kotlin's Function1<I,O>.
|
||||
*/
|
||||
private static class KotlinFunction<I, O> implements Function<I, O>, Function1<I, O> {
|
||||
private static final class KotlinFunction<I, O>
|
||||
implements Function<I, O>, Function1<I, O> {
|
||||
|
||||
private final Function1<I, O> kotlinLambda;
|
||||
|
||||
@@ -126,13 +147,15 @@ class KotlinLambdaToFunctionAutoConfiguration {
|
||||
public O invoke(I i) {
|
||||
return this.apply(i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for Kotlin lambda to be represented as both Java Consumer<I> as well as
|
||||
* Kotlin's Function1<I,Unit>
|
||||
* Wrapper for Kotlin lambda to be represented as both Java Consumer<I> as well
|
||||
* as Kotlin's Function1<I,Unit>.
|
||||
*/
|
||||
private static class KotlinConsumer<I, U> implements Consumer<I>, Function1<I, U> {
|
||||
private static final class KotlinConsumer<I, U>
|
||||
implements Consumer<I>, Function1<I, U> {
|
||||
|
||||
private final Function1<I, U> kotlinLambda;
|
||||
|
||||
@@ -149,13 +172,14 @@ class KotlinLambdaToFunctionAutoConfiguration {
|
||||
public void accept(I i) {
|
||||
this.kotlinLambda.invoke(i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for Kotlin lambda to be represented as both Java Supplier<O> as well as
|
||||
* Kotlin's Function0<O>
|
||||
* Wrapper for Kotlin lambda to be represented as both Java Supplier<O> as well
|
||||
* as Kotlin's Function0<O>.
|
||||
*/
|
||||
private static class KotlinSupplier<O> implements Supplier<O>, Function0<O> {
|
||||
private static final class KotlinSupplier<O> implements Supplier<O>, Function0<O> {
|
||||
|
||||
private final Function0<O> kotlinLambda;
|
||||
|
||||
@@ -172,6 +196,7 @@ class KotlinLambdaToFunctionAutoConfiguration {
|
||||
public O invoke() {
|
||||
return this.kotlinLambda.invoke();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2018 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.
|
||||
@@ -16,14 +16,16 @@
|
||||
|
||||
package org.springframework.cloud.function.kotlin;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import kotlin.jvm.functions.Function0;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
@@ -32,9 +34,7 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import kotlin.jvm.functions.Function0;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import reactor.core.publisher.Flux;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -42,60 +42,63 @@ import reactor.core.publisher.Flux;
|
||||
public class ContextFunctionCatalogAutoConfigurationKotlinTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
private FunctionCatalog catalog;
|
||||
|
||||
private FunctionInspector inspector;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void kotlinLambdas() {
|
||||
create(new Class[] {KotlinLambdasConfiguration.class, SimpleConfiguration.class});
|
||||
create(new Class[] { KotlinLambdasConfiguration.class,
|
||||
SimpleConfiguration.class });
|
||||
|
||||
assertThat(context.getBean("kotlinFunction")).isInstanceOf(Function.class);
|
||||
assertThat(context.getBean("kotlinFunction")).isInstanceOf(Function1.class);
|
||||
assertThat((Function<?, ?>) catalog.lookup(Function.class, "kotlinFunction"))
|
||||
assertThat(this.context.getBean("kotlinFunction")).isInstanceOf(Function.class);
|
||||
assertThat(this.context.getBean("kotlinFunction")).isInstanceOf(Function1.class);
|
||||
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "kotlinFunction"))
|
||||
.isInstanceOf(Function.class);
|
||||
assertThat(
|
||||
inspector.getInputType(catalog.lookup(Function.class, "kotlinFunction")))
|
||||
assertThat(this.inspector
|
||||
.getInputType(this.catalog.lookup(Function.class, "kotlinFunction")))
|
||||
.isAssignableFrom(String.class);
|
||||
assertThat(
|
||||
inspector.getOutputType(catalog.lookup(Function.class, "kotlinFunction")))
|
||||
assertThat(this.inspector
|
||||
.getOutputType(this.catalog.lookup(Function.class, "kotlinFunction")))
|
||||
.isAssignableFrom(String.class);
|
||||
|
||||
assertThat(context.getBean("kotlinConsumer")).isInstanceOf(Consumer.class);
|
||||
assertThat(context.getBean("kotlinConsumer")).isInstanceOf(Function1.class);
|
||||
assertThat((Function<?, ?>) catalog.lookup(Function.class, "kotlinConsumer"))
|
||||
assertThat(this.context.getBean("kotlinConsumer")).isInstanceOf(Consumer.class);
|
||||
assertThat(this.context.getBean("kotlinConsumer")).isInstanceOf(Function1.class);
|
||||
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "kotlinConsumer"))
|
||||
.isInstanceOf(Function.class);
|
||||
assertThat(
|
||||
inspector.getInputType(catalog.lookup(Function.class, "kotlinConsumer")))
|
||||
assertThat(this.inspector
|
||||
.getInputType(this.catalog.lookup(Function.class, "kotlinConsumer")))
|
||||
.isAssignableFrom(String.class);
|
||||
|
||||
assertThat(context.getBean("kotlinSupplier")).isInstanceOf(Supplier.class);
|
||||
assertThat(context.getBean("kotlinSupplier")).isInstanceOf(Function0.class);
|
||||
Supplier<Flux<String>> supplier = catalog.lookup(Supplier.class,
|
||||
assertThat(this.context.getBean("kotlinSupplier")).isInstanceOf(Supplier.class);
|
||||
assertThat(this.context.getBean("kotlinSupplier")).isInstanceOf(Function0.class);
|
||||
Supplier<Flux<String>> supplier = this.catalog.lookup(Supplier.class,
|
||||
"kotlinSupplier");
|
||||
assertThat(supplier.get().blockFirst()).isEqualTo("Hello");
|
||||
assertThat((Supplier<?>) catalog.lookup(Supplier.class, "kotlinSupplier"))
|
||||
assertThat((Supplier<?>) this.catalog.lookup(Supplier.class, "kotlinSupplier"))
|
||||
.isInstanceOf(Supplier.class);
|
||||
assertThat(
|
||||
inspector.getOutputType(catalog.lookup(Supplier.class, "kotlinSupplier")))
|
||||
assertThat(this.inspector
|
||||
.getOutputType(this.catalog.lookup(Supplier.class, "kotlinSupplier")))
|
||||
.isAssignableFrom(String.class);
|
||||
|
||||
Function<Flux<String>, Flux<String>> function = catalog.lookup(Function.class,
|
||||
"kotlinFunction|function2");
|
||||
Function<Flux<String>, Flux<String>> function = this.catalog
|
||||
.lookup(Function.class, "kotlinFunction|function2");
|
||||
assertThat(function.apply(Flux.just("Hello")).blockFirst())
|
||||
.isEqualTo("HELLOfunction2");
|
||||
}
|
||||
|
||||
private void create(Class<?>[] types, String... props) {
|
||||
context = new SpringApplicationBuilder(types).properties(props).run();
|
||||
catalog = context.getBean(FunctionCatalog.class);
|
||||
inspector = context.getBean(FunctionInspector.class);
|
||||
this.context = new SpringApplicationBuilder(types).properties(props).run();
|
||||
this.catalog = this.context.getBean(FunctionCatalog.class);
|
||||
this.inspector = this.context.getBean(FunctionInspector.class);
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@@ -106,5 +109,7 @@ public class ContextFunctionCatalogAutoConfigurationKotlinTests {
|
||||
public Function<String, String> function2() {
|
||||
return value -> value + "function2";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2018 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.
|
||||
@@ -27,18 +27,18 @@ import org.springframework.context.annotation.Configuration
|
||||
@EnableAutoConfiguration
|
||||
@Configuration
|
||||
open class KotlinLambdasConfiguration {
|
||||
@Bean
|
||||
open fun kotlinFunction(): (String) -> String {
|
||||
return { it.toUpperCase() }
|
||||
}
|
||||
@Bean
|
||||
open fun kotlinFunction(): (String) -> String {
|
||||
return { it.toUpperCase() }
|
||||
}
|
||||
|
||||
@Bean
|
||||
open fun kotlinConsumer(): (String) -> Unit {
|
||||
return { println(it) }
|
||||
}
|
||||
@Bean
|
||||
open fun kotlinConsumer(): (String) -> Unit {
|
||||
return { println(it) }
|
||||
}
|
||||
|
||||
@Bean
|
||||
open fun kotlinSupplier(): () -> String {
|
||||
return { "Hello" }
|
||||
}
|
||||
}
|
||||
@Bean
|
||||
open fun kotlinSupplier(): () -> String {
|
||||
return { "Hello" }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user