Skip to content

Commit 78199dc

Browse files
garyrussellartembilan
authored andcommitted
Avoid throws Exception where possible - Phase III
1 parent b138ab8 commit 78199dc

File tree

42 files changed

+297
-219
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+297
-219
lines changed

spring-integration-core/src/main/java/org/springframework/integration/json/JsonToObjectTransformer.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2019 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,6 +16,9 @@
1616

1717
package org.springframework.integration.json;
1818

19+
import java.io.IOException;
20+
import java.io.UncheckedIOException;
21+
1922
import org.springframework.beans.factory.BeanClassLoaderAware;
2023
import org.springframework.integration.mapping.support.JsonHeaders;
2124
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
@@ -79,16 +82,21 @@ public String getComponentType() {
7982
}
8083

8184
@Override
82-
protected Object doTransform(Message<?> message) throws Exception {
83-
if (this.targetClass != null) {
84-
return this.jsonObjectMapper.fromJson(message.getPayload(), this.targetClass);
85+
protected Object doTransform(Message<?> message) {
86+
try {
87+
if (this.targetClass != null) {
88+
return this.jsonObjectMapper.fromJson(message.getPayload(), this.targetClass);
89+
}
90+
else {
91+
Object result = this.jsonObjectMapper.fromJson(message.getPayload(), message.getHeaders());
92+
AbstractIntegrationMessageBuilder<Object> messageBuilder = this.getMessageBuilderFactory().withPayload(result)
93+
.copyHeaders(message.getHeaders())
94+
.removeHeaders(JsonHeaders.HEADERS.toArray(new String[3]));
95+
return messageBuilder.build();
96+
}
8597
}
86-
else {
87-
Object result = this.jsonObjectMapper.fromJson(message.getPayload(), message.getHeaders());
88-
AbstractIntegrationMessageBuilder<Object> messageBuilder = this.getMessageBuilderFactory().withPayload(result)
89-
.copyHeaders(message.getHeaders())
90-
.removeHeaders(JsonHeaders.HEADERS.toArray(new String[3]));
91-
return messageBuilder.build();
98+
catch (IOException e) {
99+
throw new UncheckedIOException(e);
92100
}
93101
}
94102

spring-integration-core/src/main/java/org/springframework/integration/json/ObjectToJsonTransformer.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -17,7 +17,9 @@
1717
package org.springframework.integration.json;
1818

1919
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
2021
import java.io.OutputStreamWriter;
22+
import java.io.UncheckedIOException;
2123
import java.util.Map;
2224

2325
import org.springframework.integration.support.json.JsonObjectMapper;
@@ -111,7 +113,7 @@ public String getComponentType() {
111113
}
112114

113115
@Override
114-
protected Object doTransform(Message<?> message) throws Exception {
116+
protected Object doTransform(Message<?> message) {
115117
Object payload = buildJsonPayload(message.getPayload());
116118

117119
Map<String, Object> headers = new LinkedCaseInsensitiveMap<>();
@@ -137,23 +139,28 @@ else if (StringUtils.hasLength(this.contentType)) {
137139
.build();
138140
}
139141

140-
private Object buildJsonPayload(Object payload) throws Exception {
141-
switch (this.resultType) {
142+
private Object buildJsonPayload(Object payload) {
143+
try {
144+
switch (this.resultType) {
142145

143-
case STRING:
144-
return this.jsonObjectMapper.toJson(payload);
146+
case STRING:
147+
return this.jsonObjectMapper.toJson(payload);
145148

146-
case NODE:
147-
return this.jsonObjectMapper.toJsonNode(payload);
149+
case NODE:
150+
return this.jsonObjectMapper.toJsonNode(payload);
148151

149-
case BYTES:
150-
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
151-
this.jsonObjectMapper.toJson(payload, new OutputStreamWriter(baos));
152-
return baos.toByteArray();
153-
}
152+
case BYTES:
153+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
154+
this.jsonObjectMapper.toJson(payload, new OutputStreamWriter(baos));
155+
return baos.toByteArray();
156+
}
154157

155-
default:
156-
throw new IllegalArgumentException("Unsupported ResultType provided: " + this.resultType);
158+
default:
159+
throw new IllegalArgumentException("Unsupported ResultType provided: " + this.resultType);
160+
}
161+
}
162+
catch (IOException e) {
163+
throw new UncheckedIOException(e);
157164
}
158165
}
159166

spring-integration-core/src/main/java/org/springframework/integration/support/json/Jackson2JsonObjectMapper.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
*/
6666
public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<JsonNode, JsonParser, JavaType> {
6767

68+
private static final String UNUSED = "unused";
69+
6870
private final ObjectMapper objectMapper;
6971

7072
public Jackson2JsonObjectMapper() {
@@ -188,7 +190,7 @@ private void registerWellKnownModulesIfAvailable() {
188190
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk7.Jdk7Module", getClassLoader());
189191
this.objectMapper.registerModule(BeanUtils.instantiateClass(jdk7Module));
190192
}
191-
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
193+
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
192194
// jackson-datatype-jdk7 not available
193195
}
194196

@@ -197,7 +199,7 @@ private void registerWellKnownModulesIfAvailable() {
197199
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk8.Jdk8Module", getClassLoader());
198200
this.objectMapper.registerModule(BeanUtils.instantiateClass(jdk8Module));
199201
}
200-
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
202+
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
201203
// jackson-datatype-jdk8 not available
202204
}
203205

@@ -206,7 +208,7 @@ private void registerWellKnownModulesIfAvailable() {
206208
ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", getClassLoader());
207209
this.objectMapper.registerModule(BeanUtils.instantiateClass(javaTimeModule));
208210
}
209-
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
211+
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
210212
// jackson-datatype-jsr310 not available
211213
}
212214

@@ -217,7 +219,7 @@ private void registerWellKnownModulesIfAvailable() {
217219
ClassUtils.forName("com.fasterxml.jackson.datatype.joda.JodaModule", getClassLoader());
218220
this.objectMapper.registerModule(BeanUtils.instantiateClass(jodaModule));
219221
}
220-
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
222+
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
221223
// jackson-datatype-joda not available
222224
}
223225
}
@@ -229,7 +231,7 @@ private void registerWellKnownModulesIfAvailable() {
229231
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", getClassLoader());
230232
this.objectMapper.registerModule(BeanUtils.instantiateClass(kotlinModule));
231233
}
232-
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
234+
catch (@SuppressWarnings(UNUSED) ClassNotFoundException ex) {
233235
//jackson-module-kotlin not available
234236
}
235237
}

spring-integration-core/src/main/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -355,7 +355,7 @@ protected class LeaderSelector implements Callable<Void> {
355355
}
356356

357357
@Override
358-
public Void call() throws Exception {
358+
public Void call() {
359359
try {
360360
while (isRunning()) {
361361
try {

spring-integration-core/src/main/java/org/springframework/integration/support/management/IntegrationManagement.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2018 the original author or authors.
2+
* Copyright 2015-2019 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.
@@ -67,6 +67,15 @@ default void registerMetricsCaptor(MetricsCaptor captor) {
6767
// no op
6868
}
6969

70+
71+
72+
@Override
73+
default void destroy() {
74+
// no op
75+
}
76+
77+
78+
7079
/**
7180
* Toggles to inform the management configurer to not set these properties since
7281
* the user has manually configured them in a bean definition. If true, the

spring-integration-core/src/main/java/org/springframework/integration/support/management/LifecycleMessageHandlerMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -188,7 +188,7 @@ public ManagementOverrides getOverrides() {
188188
}
189189

190190
@Override
191-
public void destroy() throws Exception {
191+
public void destroy() {
192192
this.delegate.destroy();
193193
}
194194

spring-integration-core/src/main/java/org/springframework/integration/support/management/LifecycleMessageSourceMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -127,7 +127,7 @@ public ManagementOverrides getOverrides() {
127127
}
128128

129129
@Override
130-
public void destroy() throws Exception {
130+
public void destroy() {
131131
this.delegate.destroy();
132132
}
133133

spring-integration-core/src/main/java/org/springframework/integration/transaction/TransactionSynchronizationFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2019 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.
@@ -165,7 +165,7 @@ public TransactionSynchronizationFactoryBean afterRollback(String expression, Me
165165
}
166166

167167
@Override
168-
public DefaultTransactionSynchronizationFactory getObject() throws Exception {
168+
public DefaultTransactionSynchronizationFactory getObject() {
169169
if (this.channelResolver == null) {
170170
this.channelResolver = new BeanFactoryMessageChannelDestinationResolver(this.beanFactory);
171171
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2019 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,11 +28,12 @@
2828
*/
2929
public abstract class AbstractPayloadTransformer<T, U> extends AbstractTransformer {
3030

31+
@Override
3132
@SuppressWarnings("unchecked")
32-
public final U doTransform(Message<?> message) throws Exception {
33+
public final U doTransform(Message<?> message) {
3334
return this.transformPayload((T) message.getPayload());
3435
}
3536

36-
protected abstract U transformPayload(T payload) throws Exception;
37+
protected abstract U transformPayload(T payload);
3738

3839
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -53,8 +53,7 @@ public final Message<?> transform(Message<?> message) {
5353
*
5454
* @param message The message.
5555
* @return The result of the transformation.
56-
* @throws Exception Any exception.
5756
*/
58-
protected abstract Object doTransform(Message<?> message) throws Exception;
57+
protected abstract Object doTransform(Message<?> message);
5958

6059
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -51,7 +51,7 @@ public String getComponentType() {
5151
}
5252

5353
@Override
54-
protected Object doTransform(Message<?> message) throws Exception {
54+
protected Object doTransform(Message<?> message) {
5555
Assert.notNull(message, "message must not be null");
5656
UUID id = message.getHeaders().getId();
5757
Assert.notNull(id, "ID header must not be null");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -60,7 +60,7 @@ public String getComponentType() {
6060
}
6161

6262
@Override
63-
protected Object doTransform(Message<?> message) throws Exception {
63+
protected Object doTransform(Message<?> message) {
6464
Assert.notNull(message, "message must not be null");
6565
Assert.isTrue(message.getPayload() instanceof UUID, "payload must be a UUID");
6666
UUID id = (UUID) message.getPayload();

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2018 the original author or authors.
2+
* Copyright 2015-2019 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,6 +16,9 @@
1616

1717
package org.springframework.integration.transformer;
1818

19+
import java.io.IOException;
20+
import java.io.UncheckedIOException;
21+
1922
import org.springframework.expression.Expression;
2023
import org.springframework.expression.spel.support.StandardEvaluationContext;
2124
import org.springframework.integration.codec.Codec;
@@ -79,10 +82,15 @@ protected void onInit() {
7982
}
8083

8184
@Override
82-
protected T doTransform(Message<?> message) throws Exception {
85+
protected T doTransform(Message<?> message) {
8386
Assert.isTrue(message.getPayload() instanceof byte[], "Message payload must be byte[]");
8487
byte[] bytes = (byte[]) message.getPayload();
85-
return this.codec.decode(bytes, this.type != null ? this.type : type(message));
88+
try {
89+
return this.codec.decode(bytes, this.type != null ? this.type : type(message));
90+
}
91+
catch (IOException e) {
92+
throw new UncheckedIOException(e);
93+
}
8694
}
8795

8896
@SuppressWarnings("unchecked")

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2019 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,6 +16,9 @@
1616

1717
package org.springframework.integration.transformer;
1818

19+
import java.io.IOException;
20+
import java.io.UncheckedIOException;
21+
1922
import org.springframework.integration.codec.Codec;
2023
import org.springframework.util.Assert;
2124

@@ -37,8 +40,13 @@ public EncodingPayloadTransformer(Codec codec) {
3740
}
3841

3942
@Override
40-
protected byte[] transformPayload(T payload) throws Exception {
41-
return this.codec.encode(payload);
43+
protected byte[] transformPayload(T payload) {
44+
try {
45+
return this.codec.encode(payload);
46+
}
47+
catch (IOException e) {
48+
throw new UncheckedIOException(e);
49+
}
4250
}
4351

4452
}

0 commit comments

Comments
 (0)