Skip to content

Commit aa219bf

Browse files
committed
* Rename TransformerSpec -> TransformerEndpointSpec for better context
meaning of the class * Introduce `KotlinTransformerEndpointSpec` as an extension of the `TransformerEndpointSpec` to have an `inline fun <reified P> transformer(crossinline function: (P) -> Any)` for Kotlin style * Add `KotlinIntegrationFlowDefinition.transformWith(KotlinTransformerEndpointSpec)` * Deprecate Kotlin methods which are covered by the mentioned `transformWith()` * Fix tests to use new API * Mentioned the change in the doc
1 parent 62c21ae commit aa219bf

File tree

11 files changed

+182
-40
lines changed

11 files changed

+182
-40
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dsl/BaseIntegrationFlowDefinition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,13 @@ public <P, T> B transform(@Nullable Class<P> expectedType, GenericTransformer<P,
781781
}
782782

783783
/**
784-
* Populate a {@link MessageTransformingHandler} into the endpoint with provided {@link TransformerSpec} options.
784+
* Populate a {@link MessageTransformingHandler} into the endpoint with provided {@link TransformerEndpointSpec} options.
785785
* One of the 'expression', 'ref', 'refName', 'processor' or 'function' must be provided.
786786
* @return the current {@link BaseIntegrationFlowDefinition}.
787787
* @since 6.2
788788
*/
789-
public B transformWith(Consumer<TransformerSpec> transformerConfigurer) {
790-
return register(new TransformerSpec(), transformerConfigurer);
789+
public B transformWith(Consumer<TransformerEndpointSpec> transformerConfigurer) {
790+
return register(new TransformerEndpointSpec(), transformerConfigurer);
791791
}
792792

793793
/**

spring-integration-core/src/main/java/org/springframework/integration/dsl/TransformerSpec.java renamed to spring-integration-core/src/main/java/org/springframework/integration/dsl/TransformerEndpointSpec.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*
4242
* @since 6.2
4343
*/
44-
public class TransformerSpec extends ConsumerEndpointSpec<TransformerSpec, MessageTransformingHandler> {
44+
public class TransformerEndpointSpec extends ConsumerEndpointSpec<TransformerEndpointSpec, MessageTransformingHandler> {
4545

4646
private final AtomicBoolean transformerSet = new AtomicBoolean();
4747

@@ -61,7 +61,7 @@ public class TransformerSpec extends ConsumerEndpointSpec<TransformerSpec, Messa
6161

6262
private MessageProcessorSpec<?> processor;
6363

64-
protected TransformerSpec() {
64+
protected TransformerEndpointSpec() {
6565
super(new MessageTransformingHandler());
6666
}
6767

@@ -70,7 +70,7 @@ protected TransformerSpec() {
7070
* @param expression the SpEL expression to use.
7171
* @return the TransformerSpec
7272
*/
73-
public TransformerSpec expression(String expression) {
73+
public TransformerEndpointSpec expression(String expression) {
7474
return expression(PARSER.parseExpression(expression));
7575
}
7676

@@ -79,7 +79,7 @@ public TransformerSpec expression(String expression) {
7979
* @param expression the SpEL expression to use.
8080
* @return the TransformerSpec
8181
*/
82-
public TransformerSpec expression(Expression expression) {
82+
public TransformerEndpointSpec expression(Expression expression) {
8383
assertTransformerSet();
8484
this.expression = expression;
8585
return this;
@@ -90,19 +90,19 @@ public TransformerSpec expression(Expression expression) {
9090
* @param ref the service to call as a transformer POJO.
9191
* @return the TransformerSpec
9292
*/
93-
public TransformerSpec ref(Object ref) {
93+
public TransformerEndpointSpec ref(Object ref) {
9494
assertTransformerSet();
9595
this.ref = ref;
9696
return this;
9797
}
9898

9999
/**
100100
* Provide a bean name to use a {@link MethodInvokingTransformer}
101-
* (based on {@link BeanNameMessageProcessor})for the target handler.
101+
* (based on {@link BeanNameMessageProcessor}) for the target handler.
102102
* @param refName the bean name for service to call as a transformer POJO.
103103
* @return the TransformerSpec
104104
*/
105-
public TransformerSpec refName(String refName) {
105+
public TransformerEndpointSpec refName(String refName) {
106106
assertTransformerSet();
107107
this.refName = refName;
108108
return this;
@@ -114,7 +114,7 @@ public TransformerSpec refName(String refName) {
114114
* @param method the service method name to call.
115115
* @return the TransformerSpec
116116
*/
117-
public TransformerSpec method(@Nullable String method) {
117+
public TransformerEndpointSpec method(@Nullable String method) {
118118
this.method = method;
119119
return this;
120120
}
@@ -126,7 +126,7 @@ public TransformerSpec method(@Nullable String method) {
126126
* @param <T> the output type.
127127
* @return the TransformerSpec
128128
*/
129-
public <P, T> TransformerSpec transformer(GenericTransformer<P, T> transformer) {
129+
public <P, T> TransformerEndpointSpec transformer(GenericTransformer<P, T> transformer) {
130130
assertTransformerSet();
131131
this.transformer = transformer;
132132
return this;
@@ -140,7 +140,7 @@ public <P, T> TransformerSpec transformer(GenericTransformer<P, T> transformer)
140140
* @param <P> the type ot expect.
141141
* @return the spec.
142142
*/
143-
public <P> TransformerSpec expectedType(@Nullable Class<P> expectedType) {
143+
public <P> TransformerEndpointSpec expectedType(@Nullable Class<P> expectedType) {
144144
this.expectedType = expectedType;
145145
return this;
146146
}
@@ -150,7 +150,7 @@ public <P> TransformerSpec expectedType(@Nullable Class<P> expectedType) {
150150
* @param processor the {@link MessageProcessorSpec} to use.
151151
* @return the TransformerSpec
152152
*/
153-
public TransformerSpec processor(MessageProcessorSpec<?> processor) {
153+
public TransformerEndpointSpec processor(MessageProcessorSpec<?> processor) {
154154
assertTransformerSet();
155155
this.processor = processor;
156156
return this;

spring-integration-core/src/main/kotlin/org/springframework/integration/dsl/KotlinIntegrationFlowDefinition.kt

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,16 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
9292
}
9393

9494
/**
95-
* Inline function for [IntegrationFlowDefinition.transform] providing a `transform<MyTypeIn, MyTypeOut>()` variant
95+
* Inline function for [IntegrationFlowDefinition.transform] providing a `transform<MyTypeIn>()` variant
9696
* with reified generic type.
9797
*/
98-
@Suppress("DEPRECATION")
98+
@Deprecated("since 6.2",
99+
ReplaceWith("""
100+
transformWith {
101+
transformer<Type> { }
102+
id("value")
103+
}"""))
104+
@Suppress("DEPRECATION", "REMOVAL")
99105
inline fun <reified P> transform(
100106
crossinline function: (P) -> Any,
101107
crossinline configurer: GenericEndpointSpec<MessageTransformingHandler>.() -> Unit
@@ -104,6 +110,16 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
104110
this.delegate.transform(P::class.java, { function(it) }) { configurer(it) }
105111
}
106112

113+
/**
114+
* Inline function for [IntegrationFlowDefinition.transformWith]
115+
* providing a `transform<MyTypeIn>()` variant
116+
* with reified generic type.
117+
* @since 6.2
118+
*/
119+
fun transformWith(configurer: KotlinTransformerEndpointSpec.() -> Unit) {
120+
this.delegate.register(KotlinTransformerEndpointSpec(), configurer)
121+
}
122+
107123
/**
108124
* Inline function for [IntegrationFlowDefinition.split] providing a `split<MyTypeIn>()` variant
109125
* with reified generic type.
@@ -306,7 +322,13 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
306322
* for the provided `Transformer` instance.
307323
* @since 5.3.1
308324
*/
309-
@Suppress("DEPRECATION")
325+
@Deprecated("since 6.2",
326+
ReplaceWith("""
327+
transformWith {
328+
transformer(transformer)
329+
id("value")
330+
}"""))
331+
@Suppress("DEPRECATION", "REMOVAL")
310332
fun transform(
311333
transformer: Transformer,
312334
endpointConfigurer: GenericEndpointSpec<MessageTransformingHandler>.() -> Unit = {}
@@ -315,11 +337,26 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
315337
this.delegate.transform(transformer) { endpointConfigurer(it) }
316338
}
317339

340+
/**
341+
* Populate the [Transformer] EI Pattern specific [MessageHandler] implementation
342+
* for the provided [Transformer] instance.
343+
* @since 6.2
344+
*/
345+
fun transform(transformer: Transformer) {
346+
this.delegate.transform(transformer)
347+
}
348+
318349
/**
319350
* Populate the [Transformer] EI Pattern specific [MessageHandler] implementation
320351
* for the SpEL [Expression].
321352
*/
322-
@Suppress("DEPRECATION")
353+
@Deprecated("since 6.2",
354+
ReplaceWith("""
355+
transformWith {
356+
expression("value")
357+
id("value")
358+
}"""))
359+
@Suppress("DEPRECATION", "REMOVAL")
323360
fun transform(
324361
expression: String,
325362
endpointConfigurer: GenericEndpointSpec<MessageTransformingHandler>.() -> Unit = {}
@@ -328,6 +365,16 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
328365
this.delegate.transform(expression, endpointConfigurer)
329366
}
330367

368+
369+
/**
370+
* Populate the [Transformer] EI Pattern specific [MessageHandler] implementation
371+
* for the SpEL [Expression].
372+
* @since 6.2
373+
*/
374+
fun transform(expression: String) {
375+
this.delegate.transform(expression)
376+
}
377+
331378
/**
332379
* Populate the [MessageTransformingHandler] for the [MethodInvokingTransformer]
333380
* to invoke the service method at runtime.
@@ -340,7 +387,14 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
340387
* Populate the [MessageTransformingHandler] for the [MethodInvokingTransformer]
341388
* to invoke the service method at runtime.
342389
*/
343-
@Suppress("DEPRECATION")
390+
@Deprecated("since 6.2",
391+
ReplaceWith("""
392+
transformWith {
393+
ref("value")
394+
method("value")
395+
id("value")
396+
}"""))
397+
@Suppress("DEPRECATION", "REMOVAL")
344398
fun transform(
345399
service: Any, methodName: String?,
346400
endpointConfigurer: GenericEndpointSpec<MessageTransformingHandler>.() -> Unit
@@ -354,7 +408,13 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
354408
* [org.springframework.integration.handler.MessageProcessor] from provided [MessageProcessorSpec].
355409
* In addition, accept options for the integration endpoint using [GenericEndpointSpec].
356410
*/
357-
@Suppress("DEPRECATION")
411+
@Deprecated("since 6.2",
412+
ReplaceWith("""
413+
transformWith {
414+
processor("value")
415+
id("value")
416+
}"""))
417+
@Suppress("DEPRECATION", "REMOVAL")
358418
fun transform(
359419
messageProcessorSpec: MessageProcessorSpec<*>,
360420
endpointConfigurer: GenericEndpointSpec<MessageTransformingHandler>.() -> Unit = {}
@@ -363,6 +423,15 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
363423
this.delegate.transform(messageProcessorSpec, endpointConfigurer)
364424
}
365425

426+
/**
427+
* Populate the [MessageTransformingHandler] instance for the
428+
* [org.springframework.integration.handler.MessageProcessor] from provided [MessageProcessorSpec].
429+
* @since 6.2
430+
*/
431+
fun transform(messageProcessorSpec: MessageProcessorSpec<*>) {
432+
this.delegate.transform(messageProcessorSpec)
433+
}
434+
366435
/**
367436
* Populate a [MessageFilter] with [MessageSelector] for the provided SpEL expression.
368437
* In addition, accept options for the integration endpoint using [KotlinFilterEndpointSpec]:
@@ -571,7 +640,7 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
571640
delay {
572641
messageGroupId(groupId)
573642
}"""))
574-
@Suppress("DEPRECATION")
643+
@Suppress("DEPRECATION", "REMOVAL")
575644
fun delay(groupId: String, endpointConfigurer: DelayerEndpointSpec.() -> Unit = {}) {
576645
this.delegate.delay(groupId, endpointConfigurer)
577646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.dsl
18+
19+
import org.springframework.integration.transformer.MessageTransformingHandler
20+
21+
/**
22+
* A [TransformerEndpointSpec] wrapped for Kotlin DSL.
23+
*
24+
* @property delegate the [TransformerEndpointSpec] this instance is delegating to.
25+
*
26+
* @author Artem Bilan
27+
*
28+
* @since 6.2
29+
*/
30+
class KotlinTransformerEndpointSpec : TransformerEndpointSpec() {
31+
32+
/**
33+
* Provide a Kotlin function as a direct delegate for [MessageTransformingHandler].
34+
* @param function the function instance to use.
35+
* @param <P> the input type.
36+
*/
37+
inline fun <reified P> transformer(crossinline function: (P) -> Any) {
38+
expectedType(P::class.java)
39+
transformer<P, Any> { function(it) }
40+
}
41+
42+
}

spring-integration-core/src/test/java/org/springframework/integration/dsl/flows/IntegrationFlowTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
import org.springframework.integration.dsl.PollerSpec;
6565
import org.springframework.integration.dsl.Pollers;
6666
import org.springframework.integration.dsl.QueueChannelSpec;
67-
import org.springframework.integration.dsl.TransformerSpec;
67+
import org.springframework.integration.dsl.TransformerEndpointSpec;
6868
import org.springframework.integration.dsl.Transformers;
6969
import org.springframework.integration.endpoint.AbstractEndpoint;
7070
import org.springframework.integration.endpoint.EventDrivenConsumer;
@@ -684,7 +684,7 @@ public IntegrationFlow flow2() {
684684
.get();
685685
}
686686

687-
private void payloadSerializingTransformer(TransformerSpec spec) {
687+
private void payloadSerializingTransformer(TransformerEndpointSpec spec) {
688688
spec.transformer(new PayloadSerializingTransformer())
689689
.autoStartup(false)
690690
.id("payloadSerializingTransformer");

spring-integration-core/src/test/kotlin/org/springframework/integration/dsl/KotlinDslTests.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ class KotlinDslTests {
161161

162162
val integrationFlow =
163163
integrationFlow(publisher) {
164-
transform<Message<Int>>({ it.payload * 2 }) { id("foo") }
164+
transformWith {
165+
transformer<Message<Int>> { it.payload * 2 }
166+
id("foo")
167+
}
165168
channel(fluxChannel)
166169
}
167170

@@ -249,7 +252,10 @@ class KotlinDslTests {
249252
@Bean
250253
fun functionFlow() =
251254
integrationFlow<Function<ByteArray, String>>({ beanName("functionGateway") }) {
252-
transform(Transformers.objectToString()) { id("objectToStringTransformer") }
255+
transformWith {
256+
transformer(Transformers.objectToString())
257+
id("objectToStringTransformer")
258+
}
253259
transform<String> { it.uppercase() }
254260
split<Message<*>> { it.payload }
255261
split<String>({ it }) { id("splitterEndpoint") }
@@ -292,7 +298,10 @@ class KotlinDslTests {
292298
fun fixedSubscriberFlow() =
293299
integrationFlow("fixedSubscriberInput", true) {
294300
log<Any>(LoggingHandler.Level.WARN) { it.payload }
295-
transform("payload") { id("spelTransformer") }
301+
transformWith {
302+
expression("payload")
303+
id("spelTransformer")
304+
}
296305
}
297306

298307
@Bean

spring-integration-groovy/src/main/groovy/org/springframework/integration/groovy/dsl/GroovyIntegrationFlowDefinition.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import org.springframework.integration.dsl.ResequencerSpec
4747
import org.springframework.integration.dsl.RouterSpec
4848
import org.springframework.integration.dsl.ScatterGatherSpec
4949
import org.springframework.integration.dsl.SplitterEndpointSpec
50-
import org.springframework.integration.dsl.TransformerSpec
50+
import org.springframework.integration.dsl.TransformerEndpointSpec
5151
import org.springframework.integration.dsl.WireTapSpec
5252
import org.springframework.integration.filter.MethodInvokingSelector
5353
import org.springframework.integration.handler.BridgeHandler
@@ -355,8 +355,8 @@ class GroovyIntegrationFlowDefinition {
355355
* @since 6.2
356356
*/
357357
GroovyIntegrationFlowDefinition transform(
358-
@DelegatesTo(value = TransformerSpec, strategy = Closure.DELEGATE_FIRST)
359-
@ClosureParams(value = SimpleType.class, options = 'org.springframework.integration.dsl.TransformerSpec')
358+
@DelegatesTo(value = TransformerEndpointSpec, strategy = Closure.DELEGATE_FIRST)
359+
@ClosureParams(value = SimpleType.class, options = 'org.springframework.integration.dsl.TransformerEndpointSpec')
360360
Closure<?> transformerConfigurer) {
361361

362362
this.delegate.transformWith createConfigurerIfAny(transformerConfigurer)

0 commit comments

Comments
 (0)