From 12dd73d219625fa6959c2ab2a713006123a8188c Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 18 Dec 2018 14:32:46 -0500 Subject: [PATCH] Fix code smell for PayloadTypeConvertTransformers --- .../PayloadDeserializingTransformer.java | 23 ++++++--------- .../PayloadSerializingTransformer.java | 20 +++++++------ .../PayloadTypeConvertingTransformer.java | 28 +++++++++++++++---- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java index 7918058d9d..82f29d3b49 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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,7 +16,6 @@ package org.springframework.integration.transformer; -import org.springframework.core.convert.converter.Converter; import org.springframework.core.serializer.Deserializer; import org.springframework.integration.support.converter.WhiteListDeserializingConverter; import org.springframework.util.Assert; @@ -31,19 +30,20 @@ import org.springframework.util.Assert; * * @author Mark Fisher * @author Gary Russell + * @author Artem Bilan + * * @since 1.0.1 */ public class PayloadDeserializingTransformer extends PayloadTypeConvertingTransformer { - + /** + * Instantiate based on the {@link WhiteListDeserializingConverter} with the + * {@link org.springframework.core.serializer.DefaultDeserializer}. + */ public PayloadDeserializingTransformer() { doSetConverter(new WhiteListDeserializingConverter()); } - private void doSetConverter(Converter converter) { - this.converter = converter; - } - public void setDeserializer(Deserializer deserializer) { setConverter(new WhiteListDeserializingConverter(deserializer)); } @@ -58,14 +58,9 @@ public class PayloadDeserializingTransformer extends PayloadTypeConvertingTransf * @since 4.2.13 */ public void setWhiteListPatterns(String... patterns) { - Assert.isTrue(this.converter instanceof WhiteListDeserializingConverter, + Assert.isTrue(getConverter() instanceof WhiteListDeserializingConverter, "Patterns can only be provided when using a 'WhiteListDeserializingConverter'"); - ((WhiteListDeserializingConverter) this.converter).setWhiteListPatterns(patterns); - } - - @Override - protected Object transformPayload(byte[] payload) throws Exception { - return this.converter.convert(payload); + ((WhiteListDeserializingConverter) getConverter()).setWhiteListPatterns(patterns); } } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java index 8ae15cfa1d..141995d519 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2018 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. @@ -28,20 +28,22 @@ import org.springframework.core.serializer.support.SerializingConverter; * * @author Mark Fisher * @author Gary Russell + * @author Artem Bilan + * * @since 1.0.1 */ public class PayloadSerializingTransformer extends PayloadTypeConvertingTransformer { - public void setSerializer(Serializer serializer) { - this.setConverter(new SerializingConverter(serializer)); + /** + * Instantiate based on the {@link SerializingConverter} with the + * {@link org.springframework.core.serializer.DefaultSerializer}. + */ + public PayloadSerializingTransformer() { + doSetConverter(new SerializingConverter()); } - @Override - protected byte[] transformPayload(Object payload) throws Exception { - if (this.converter == null) { - this.setConverter(new SerializingConverter()); - } - return this.converter.convert(payload); + public void setSerializer(Serializer serializer) { + setConverter(new SerializingConverter(serializer)); } } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadTypeConvertingTransformer.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadTypeConvertingTransformer.java index 9406d54e73..dd9dc23c47 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadTypeConvertingTransformer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadTypeConvertingTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2018 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. @@ -24,24 +24,42 @@ import org.springframework.util.Assert; * Converter<Object, Object>. A reference to the delegate must be provided. * * @author Gary Russell + * @author Artem Bilan + * * @since 2.0 */ public class PayloadTypeConvertingTransformer extends AbstractPayloadTransformer { - protected Converter converter; + private Converter converter; /** * Specify the converter to use. - * * @param converter The Converter. */ public void setConverter(Converter converter) { + doSetConverter(converter); + } + + protected final void doSetConverter(Converter converter) { + Assert.notNull(converter, "'converter' must not be null"); this.converter = converter; } + /** + * Get the configured {@link Converter}. + * @return the converter. + */ + protected Converter getConverter() { + return this.converter; + } @Override - protected U transformPayload(T payload) throws Exception { - Assert.notNull(this.converter, this.getClass().getName() + " requires a Converter"); + protected void onInit() throws Exception { + super.onInit(); + Assert.notNull(this.converter, () -> getClass().getName() + " requires a Converter"); + } + + @Override + protected U transformPayload(T payload) { return this.converter.convert(payload); }