Skip to content

Commit 12dd73d

Browse files
artembilangaryrussell
authored andcommitted
Fix code smell for PayloadTypeConvertTransformers
1 parent 3dd8b63 commit 12dd73d

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.integration.transformer;
1818

19-
import org.springframework.core.convert.converter.Converter;
2019
import org.springframework.core.serializer.Deserializer;
2120
import org.springframework.integration.support.converter.WhiteListDeserializingConverter;
2221
import org.springframework.util.Assert;
@@ -31,19 +30,20 @@
3130
*
3231
* @author Mark Fisher
3332
* @author Gary Russell
33+
* @author Artem Bilan
34+
*
3435
* @since 1.0.1
3536
*/
3637
public class PayloadDeserializingTransformer extends PayloadTypeConvertingTransformer<byte[], Object> {
3738

38-
39+
/**
40+
* Instantiate based on the {@link WhiteListDeserializingConverter} with the
41+
* {@link org.springframework.core.serializer.DefaultDeserializer}.
42+
*/
3943
public PayloadDeserializingTransformer() {
4044
doSetConverter(new WhiteListDeserializingConverter());
4145
}
4246

43-
private void doSetConverter(Converter<byte[], Object> converter) {
44-
this.converter = converter;
45-
}
46-
4747
public void setDeserializer(Deserializer<Object> deserializer) {
4848
setConverter(new WhiteListDeserializingConverter(deserializer));
4949
}
@@ -58,14 +58,9 @@ public void setDeserializer(Deserializer<Object> deserializer) {
5858
* @since 4.2.13
5959
*/
6060
public void setWhiteListPatterns(String... patterns) {
61-
Assert.isTrue(this.converter instanceof WhiteListDeserializingConverter,
61+
Assert.isTrue(getConverter() instanceof WhiteListDeserializingConverter,
6262
"Patterns can only be provided when using a 'WhiteListDeserializingConverter'");
63-
((WhiteListDeserializingConverter) this.converter).setWhiteListPatterns(patterns);
64-
}
65-
66-
@Override
67-
protected Object transformPayload(byte[] payload) throws Exception {
68-
return this.converter.convert(payload);
63+
((WhiteListDeserializingConverter) getConverter()).setWhiteListPatterns(patterns);
6964
}
7065

7166
}

spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,20 +28,22 @@
2828
*
2929
* @author Mark Fisher
3030
* @author Gary Russell
31+
* @author Artem Bilan
32+
*
3133
* @since 1.0.1
3234
*/
3335
public class PayloadSerializingTransformer extends PayloadTypeConvertingTransformer<Object, byte[]> {
3436

35-
public void setSerializer(Serializer<Object> serializer) {
36-
this.setConverter(new SerializingConverter(serializer));
37+
/**
38+
* Instantiate based on the {@link SerializingConverter} with the
39+
* {@link org.springframework.core.serializer.DefaultSerializer}.
40+
*/
41+
public PayloadSerializingTransformer() {
42+
doSetConverter(new SerializingConverter());
3743
}
3844

39-
@Override
40-
protected byte[] transformPayload(Object payload) throws Exception {
41-
if (this.converter == null) {
42-
this.setConverter(new SerializingConverter());
43-
}
44-
return this.converter.convert(payload);
45+
public void setSerializer(Serializer<Object> serializer) {
46+
setConverter(new SerializingConverter(serializer));
4547
}
4648

4749
}

spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadTypeConvertingTransformer.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,24 +24,42 @@
2424
* Converter&lt;Object, Object&gt;. A reference to the delegate must be provided.
2525
*
2626
* @author Gary Russell
27+
* @author Artem Bilan
28+
*
2729
* @since 2.0
2830
*/
2931
public class PayloadTypeConvertingTransformer<T, U> extends AbstractPayloadTransformer<T, U> {
3032

31-
protected Converter<T, U> converter;
33+
private Converter<T, U> converter;
3234

3335
/**
3436
* Specify the converter to use.
35-
*
3637
* @param converter The Converter.
3738
*/
3839
public void setConverter(Converter<T, U> converter) {
40+
doSetConverter(converter);
41+
}
42+
43+
protected final void doSetConverter(Converter<T, U> converter) {
44+
Assert.notNull(converter, "'converter' must not be null");
3945
this.converter = converter;
4046
}
47+
/**
48+
* Get the configured {@link Converter}.
49+
* @return the converter.
50+
*/
51+
protected Converter<T, U> getConverter() {
52+
return this.converter;
53+
}
54+
55+
@Override
56+
protected void onInit() throws Exception {
57+
super.onInit();
58+
Assert.notNull(this.converter, () -> getClass().getName() + " requires a Converter<Object, Object>");
59+
}
4160

4261
@Override
43-
protected U transformPayload(T payload) throws Exception {
44-
Assert.notNull(this.converter, this.getClass().getName() + " requires a Converter<Object, Object>");
62+
protected U transformPayload(T payload) {
4563
return this.converter.convert(payload);
4664
}
4765

0 commit comments

Comments
 (0)