Avoid throws Exception where possible - Phase III

This commit is contained in:
Gary Russell
2019-03-07 16:53:48 -05:00
committed by Artem Bilan
parent b138ab80f8
commit 78199dca9b
42 changed files with 297 additions and 219 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-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,6 +16,9 @@
package org.springframework.integration.json;
import java.io.IOException;
import java.io.UncheckedIOException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.integration.mapping.support.JsonHeaders;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
@@ -79,16 +82,21 @@ public class JsonToObjectTransformer extends AbstractTransformer implements Bean
}
@Override
protected Object doTransform(Message<?> message) throws Exception {
if (this.targetClass != null) {
return this.jsonObjectMapper.fromJson(message.getPayload(), this.targetClass);
protected Object doTransform(Message<?> message) {
try {
if (this.targetClass != null) {
return this.jsonObjectMapper.fromJson(message.getPayload(), this.targetClass);
}
else {
Object result = this.jsonObjectMapper.fromJson(message.getPayload(), message.getHeaders());
AbstractIntegrationMessageBuilder<Object> messageBuilder = this.getMessageBuilderFactory().withPayload(result)
.copyHeaders(message.getHeaders())
.removeHeaders(JsonHeaders.HEADERS.toArray(new String[3]));
return messageBuilder.build();
}
}
else {
Object result = this.jsonObjectMapper.fromJson(message.getPayload(), message.getHeaders());
AbstractIntegrationMessageBuilder<Object> messageBuilder = this.getMessageBuilderFactory().withPayload(result)
.copyHeaders(message.getHeaders())
.removeHeaders(JsonHeaders.HEADERS.toArray(new String[3]));
return messageBuilder.build();
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-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.
@@ -17,7 +17,9 @@
package org.springframework.integration.json;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.util.Map;
import org.springframework.integration.support.json.JsonObjectMapper;
@@ -111,7 +113,7 @@ public class ObjectToJsonTransformer extends AbstractTransformer {
}
@Override
protected Object doTransform(Message<?> message) throws Exception {
protected Object doTransform(Message<?> message) {
Object payload = buildJsonPayload(message.getPayload());
Map<String, Object> headers = new LinkedCaseInsensitiveMap<>();
@@ -137,23 +139,28 @@ public class ObjectToJsonTransformer extends AbstractTransformer {
.build();
}
private Object buildJsonPayload(Object payload) throws Exception {
switch (this.resultType) {
private Object buildJsonPayload(Object payload) {
try {
switch (this.resultType) {
case STRING:
return this.jsonObjectMapper.toJson(payload);
case STRING:
return this.jsonObjectMapper.toJson(payload);
case NODE:
return this.jsonObjectMapper.toJsonNode(payload);
case NODE:
return this.jsonObjectMapper.toJsonNode(payload);
case BYTES:
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
this.jsonObjectMapper.toJson(payload, new OutputStreamWriter(baos));
return baos.toByteArray();
}
case BYTES:
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
this.jsonObjectMapper.toJson(payload, new OutputStreamWriter(baos));
return baos.toByteArray();
}
default:
throw new IllegalArgumentException("Unsupported ResultType provided: " + this.resultType);
default:
throw new IllegalArgumentException("Unsupported ResultType provided: " + this.resultType);
}
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

View File

@@ -65,6 +65,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
*/
public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<JsonNode, JsonParser, JavaType> {
private static final String UNUSED = "unused";
private final ObjectMapper objectMapper;
public Jackson2JsonObjectMapper() {
@@ -188,7 +190,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk7.Jdk7Module", getClassLoader());
this.objectMapper.registerModule(BeanUtils.instantiateClass(jdk7Module));
}
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
// jackson-datatype-jdk7 not available
}
@@ -197,7 +199,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk8.Jdk8Module", getClassLoader());
this.objectMapper.registerModule(BeanUtils.instantiateClass(jdk8Module));
}
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
// jackson-datatype-jdk8 not available
}
@@ -206,7 +208,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", getClassLoader());
this.objectMapper.registerModule(BeanUtils.instantiateClass(javaTimeModule));
}
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
// jackson-datatype-jsr310 not available
}
@@ -217,7 +219,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
ClassUtils.forName("com.fasterxml.jackson.datatype.joda.JodaModule", getClassLoader());
this.objectMapper.registerModule(BeanUtils.instantiateClass(jodaModule));
}
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
// jackson-datatype-joda not available
}
}
@@ -229,7 +231,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", getClassLoader());
this.objectMapper.registerModule(BeanUtils.instantiateClass(kotlinModule));
}
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
//jackson-module-kotlin not available
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-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.
@@ -355,7 +355,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
}
@Override
public Void call() throws Exception {
public Void call() {
try {
while (isRunning()) {
try {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2018 the original author or authors.
* Copyright 2015-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.
@@ -67,6 +67,15 @@ public interface IntegrationManagement extends DisposableBean {
// no op
}
@Override
default void destroy() {
// no op
}
/**
* Toggles to inform the management configurer to not set these properties since
* the user has manually configured them in a bean definition. If true, the

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-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.
@@ -188,7 +188,7 @@ public class LifecycleMessageHandlerMetrics implements MessageHandlerMetrics, Li
}
@Override
public void destroy() throws Exception {
public void destroy() {
this.delegate.destroy();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-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.
@@ -127,7 +127,7 @@ public class LifecycleMessageSourceMetrics implements MessageSourceMetrics, Life
}
@Override
public void destroy() throws Exception {
public void destroy() {
this.delegate.destroy();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-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.
@@ -165,7 +165,7 @@ public class TransactionSynchronizationFactoryBean implements FactoryBean<Defaul
}
@Override
public DefaultTransactionSynchronizationFactory getObject() throws Exception {
public DefaultTransactionSynchronizationFactory getObject() {
if (this.channelResolver == null) {
this.channelResolver = new BeanFactoryMessageChannelDestinationResolver(this.beanFactory);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-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.
@@ -28,11 +28,12 @@ import org.springframework.messaging.Message;
*/
public abstract class AbstractPayloadTransformer<T, U> extends AbstractTransformer {
@Override
@SuppressWarnings("unchecked")
public final U doTransform(Message<?> message) throws Exception {
public final U doTransform(Message<?> message) {
return this.transformPayload((T) message.getPayload());
}
protected abstract U transformPayload(T payload) throws Exception;
protected abstract U transformPayload(T payload);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-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.
@@ -53,8 +53,7 @@ public abstract class AbstractTransformer extends IntegrationObjectSupport imple
*
* @param message The message.
* @return The result of the transformation.
* @throws Exception Any exception.
*/
protected abstract Object doTransform(Message<?> message) throws Exception;
protected abstract Object doTransform(Message<?> message);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-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.
@@ -51,7 +51,7 @@ public class ClaimCheckInTransformer extends AbstractTransformer {
}
@Override
protected Object doTransform(Message<?> message) throws Exception {
protected Object doTransform(Message<?> message) {
Assert.notNull(message, "message must not be null");
UUID id = message.getHeaders().getId();
Assert.notNull(id, "ID header must not be null");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-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.
@@ -60,7 +60,7 @@ public class ClaimCheckOutTransformer extends AbstractTransformer {
}
@Override
protected Object doTransform(Message<?> message) throws Exception {
protected Object doTransform(Message<?> message) {
Assert.notNull(message, "message must not be null");
Assert.isTrue(message.getPayload() instanceof UUID, "payload must be a UUID");
UUID id = (UUID) message.getPayload();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2018 the original author or authors.
* Copyright 2015-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,6 +16,9 @@
package org.springframework.integration.transformer;
import java.io.IOException;
import java.io.UncheckedIOException;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.codec.Codec;
@@ -79,10 +82,15 @@ public class DecodingTransformer<T> extends AbstractTransformer {
}
@Override
protected T doTransform(Message<?> message) throws Exception {
protected T doTransform(Message<?> message) {
Assert.isTrue(message.getPayload() instanceof byte[], "Message payload must be byte[]");
byte[] bytes = (byte[]) message.getPayload();
return this.codec.decode(bytes, this.type != null ? this.type : type(message));
try {
return this.codec.decode(bytes, this.type != null ? this.type : type(message));
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-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,6 +16,9 @@
package org.springframework.integration.transformer;
import java.io.IOException;
import java.io.UncheckedIOException;
import org.springframework.integration.codec.Codec;
import org.springframework.util.Assert;
@@ -37,8 +40,13 @@ public class EncodingPayloadTransformer<T> extends AbstractPayloadTransformer<T,
}
@Override
protected byte[] transformPayload(T payload) throws Exception {
return this.codec.encode(payload);
protected byte[] transformPayload(T payload) {
try {
return this.codec.encode(payload);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-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,13 +20,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
@@ -46,9 +41,7 @@ import org.springframework.messaging.MessageHeaders;
* @author Artem Bilan
* @author Gary Russell
*/
public class HeaderEnricher extends IntegrationObjectSupport implements Transformer, BeanNameAware, InitializingBean {
private static final Log logger = LogFactory.getLog(HeaderEnricher.class);
public class HeaderEnricher extends IntegrationObjectSupport implements Transformer {
private final Map<String, ? extends HeaderValueMessageProcessor<?>> headersToAdd;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-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.
@@ -76,7 +76,7 @@ public class MapToObjectTransformer extends AbstractPayloadTransformer<Map<?, ?>
}
@Override
protected Object transformPayload(Map<?, ?> payload) throws Exception {
protected Object transformPayload(Map<?, ?> payload) {
Object target = (this.targetClass != null)
? BeanUtils.instantiateClass(this.targetClass)
: this.getBeanFactory().getBean(this.targetBeanName);

View File

@@ -16,6 +16,8 @@
package org.springframework.integration.transformer;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -93,8 +95,14 @@ public class ObjectToMapTransformer extends AbstractPayloadTransformer<Object, M
@Override
@SuppressWarnings("unchecked")
protected Map<String, Object> transformPayload(Object payload) throws Exception {
Map<String, Object> result = this.jsonObjectMapper.fromJson(this.jsonObjectMapper.toJson(payload), Map.class);
protected Map<String, Object> transformPayload(Object payload) {
Map<String, Object> result;
try {
result = this.jsonObjectMapper.fromJson(this.jsonObjectMapper.toJson(payload), Map.class);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
if (this.shouldFlattenKeys) {
result = this.flattenMap(result);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-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,6 +16,8 @@
package org.springframework.integration.transformer;
import java.io.UnsupportedEncodingException;
import org.springframework.util.Assert;
@@ -52,9 +54,14 @@ public class ObjectToStringTransformer extends AbstractPayloadTransformer<Object
}
@Override
protected String transformPayload(Object payload) throws Exception {
protected String transformPayload(Object payload) {
if (payload instanceof byte[]) {
return new String((byte[]) payload, this.charset);
try {
return new String((byte[]) payload, this.charset);
}
catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
else if (payload instanceof char[]) {
return new String((char[]) payload);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-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.
@@ -18,7 +18,9 @@ package org.springframework.integration.transformer;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import org.springframework.integration.StaticMessageHeaderAccessor;
import org.springframework.messaging.Message;
@@ -55,16 +57,21 @@ public class StreamTransformer extends AbstractTransformer {
}
@Override
protected Object doTransform(Message<?> message) throws Exception {
Assert.isTrue(message.getPayload() instanceof InputStream, "payload must be an InputStream");
InputStream stream = (InputStream) message.getPayload();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileCopyUtils.copy(stream, baos);
Closeable closeableResource = StaticMessageHeaderAccessor.getCloseableResource(message);
if (closeableResource != null) {
closeableResource.close();
protected Object doTransform(Message<?> message) {
try {
Assert.isTrue(message.getPayload() instanceof InputStream, "payload must be an InputStream");
InputStream stream = (InputStream) message.getPayload();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileCopyUtils.copy(stream, baos);
Closeable closeableResource = StaticMessageHeaderAccessor.getCloseableResource(message);
if (closeableResource != null) {
closeableResource.close();
}
return this.charset == null ? baos.toByteArray() : baos.toString(this.charset);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
return this.charset == null ? baos.toByteArray() : baos.toString(this.charset);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-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.
@@ -62,7 +62,7 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
private final Pattern pattern = Pattern.compile("<([^>]+)>(.{15}) ([^ ]+) ([a-zA-Z0-9]{0,32})(.*)", Pattern.DOTALL);
@Override
protected Map<String, ?> transformPayload(Object payload) throws Exception {
protected Map<String, ?> transformPayload(Object payload) {
boolean isByteArray = payload instanceof byte[];
boolean isString = payload instanceof String;
Assert.isTrue(isByteArray || isString, "payload must be String or byte[]");
@@ -80,7 +80,7 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
try {
payload = new String(payloadBytes, "UTF-8");
}
catch (UnsupportedEncodingException e) {
catch (@SuppressWarnings("unused") UnsupportedEncodingException e) {
payload = new String(payloadBytes);
}
return transform(payload);
@@ -120,7 +120,7 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
}
map.put(TIMESTAMP, calendar.getTime());
}
catch (Exception e) {
catch (@SuppressWarnings("unused") Exception e) {
/*
* If we can't parse the timestamp, return it as an
* unmodified String. (Postel's law).

View File

@@ -294,7 +294,7 @@ public class DelegatingConsumerParserTests {
public static class MyTransformer extends AbstractTransformer {
@Override
protected Object doTransform(Message<?> message) throws Exception {
protected Object doTransform(Message<?> message) {
return message;
}

View File

@@ -61,9 +61,9 @@ public class PayloadTransformerTests {
}
@Override
public Integer transformPayload(String s) throws Exception {
public Integer transformPayload(String s) {
if (s.equals("bad")) {
throw new Exception("bad input!");
throw new IllegalStateException("bad input!");
}
return s.length();
}