Fix code smell for PayloadTypeConvertTransformers

This commit is contained in:
Artem Bilan
2018-12-18 14:32:46 -05:00
committed by Gary Russell
parent 3dd8b63576
commit 12dd73d219
3 changed files with 43 additions and 28 deletions

View File

@@ -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<byte[], Object> {
/**
* Instantiate based on the {@link WhiteListDeserializingConverter} with the
* {@link org.springframework.core.serializer.DefaultDeserializer}.
*/
public PayloadDeserializingTransformer() {
doSetConverter(new WhiteListDeserializingConverter());
}
private void doSetConverter(Converter<byte[], Object> converter) {
this.converter = converter;
}
public void setDeserializer(Deserializer<Object> 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);
}
}

View File

@@ -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<Object, byte[]> {
public void setSerializer(Serializer<Object> 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<Object> serializer) {
setConverter(new SerializingConverter(serializer));
}
}

View File

@@ -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&lt;Object, Object&gt;. A reference to the delegate must be provided.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*/
public class PayloadTypeConvertingTransformer<T, U> extends AbstractPayloadTransformer<T, U> {
protected Converter<T, U> converter;
private Converter<T, U> converter;
/**
* Specify the converter to use.
*
* @param converter The Converter.
*/
public void setConverter(Converter<T, U> converter) {
doSetConverter(converter);
}
protected final void doSetConverter(Converter<T, U> converter) {
Assert.notNull(converter, "'converter' must not be null");
this.converter = converter;
}
/**
* Get the configured {@link Converter}.
* @return the converter.
*/
protected Converter<T, U> getConverter() {
return this.converter;
}
@Override
protected U transformPayload(T payload) throws Exception {
Assert.notNull(this.converter, this.getClass().getName() + " requires a Converter<Object, Object>");
protected void onInit() throws Exception {
super.onInit();
Assert.notNull(this.converter, () -> getClass().getName() + " requires a Converter<Object, Object>");
}
@Override
protected U transformPayload(T payload) {
return this.converter.convert(payload);
}