Skip to content

Commit b138ab8

Browse files
garyrussellartembilan
authored andcommitted
Avoid throws Exception where possible - Phase II
Final phase III to follow. * Polishing * Polishing
1 parent 3d87ac6 commit b138ab8

File tree

39 files changed

+308
-291
lines changed

39 files changed

+308
-291
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/inbound/AmqpInboundGateway.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ protected class Listener implements ChannelAwareMessageListener {
253253

254254
@SuppressWarnings("unchecked")
255255
@Override
256-
public void onMessage(final Message message, final Channel channel) throws Exception {
256+
public void onMessage(final Message message, final Channel channel) {
257257
if (AmqpInboundGateway.this.retryTemplate == null) {
258258
try {
259259
org.springframework.messaging.Message<Object> converted = convert(message, channel);

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2018 the original author or authors.
2+
* Copyright 2013-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.
@@ -18,6 +18,7 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.File;
21+
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.net.URL;
2324

@@ -36,7 +37,7 @@
3637
*/
3738
public final class JsonPathUtils {
3839

39-
public static <T> T evaluate(Object json, String jsonPath, Predicate... predicates) throws Exception {
40+
public static <T> T evaluate(Object json, String jsonPath, Predicate... predicates) throws IOException {
4041
if (json instanceof String) {
4142
return JsonPath.read((String) json, jsonPath, predicates);
4243
}

spring-integration-core/src/main/java/org/springframework/integration/mapping/InboundMessageMapper.java

+3-5
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.
@@ -35,10 +35,9 @@ public interface InboundMessageMapper<T> {
3535
* Convert a provided object to the {@link Message}.
3636
* @param object the object for message payload or some other conversion logic
3737
* @return the message as a result of mapping
38-
* @throws Exception the exception thrown by the underlying mapper implementation
3938
*/
4039
@Nullable
41-
default Message<?> toMessage(T object) throws Exception { // NOSONAR - TODO remove Exception in 5.2
40+
default Message<?> toMessage(T object) {
4241
return toMessage(object, null);
4342
}
4443

@@ -48,10 +47,9 @@ default Message<?> toMessage(T object) throws Exception { // NOSONAR - TODO remo
4847
* @param object the object for message payload or some other conversion logic
4948
* @param headers additional headers for building message. Can be null
5049
* @return the message as a result of mapping
51-
* @throws Exception the exception thrown by the underlying mapper implementation
5250
* @since 5.0
5351
*/
5452
@Nullable
55-
Message<?> toMessage(T object, @Nullable Map<String, Object> headers) throws Exception; // NOSONAR
53+
Message<?> toMessage(T object, @Nullable Map<String, Object> headers);
5654

5755
}

spring-integration-core/src/main/java/org/springframework/integration/mapping/OutboundMessageMapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
public interface OutboundMessageMapper<T> {
3030

3131
@Nullable
32-
T fromMessage(Message<?> message) throws Exception; // NOSONAR
32+
T fromMessage(Message<?> message);
3333

3434
}

spring-integration-core/src/main/java/org/springframework/integration/metadata/PropertiesPersistingMetadataStore.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 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.
@@ -91,7 +91,7 @@ public void setFileName(String fileName) {
9191
}
9292

9393
@Override
94-
public void afterPropertiesSet() throws Exception {
94+
public void afterPropertiesSet() {
9595
File baseDir = new File(this.baseDirectory);
9696
baseDir.mkdirs();
9797
this.file = new File(baseDir, this.fileName);
@@ -195,7 +195,7 @@ public boolean replace(String key, String oldValue, String newValue) {
195195
}
196196

197197
@Override
198-
public void close() throws IOException {
198+
public void close() {
199199
flush();
200200
}
201201

@@ -205,7 +205,7 @@ public void flush() {
205205
}
206206

207207
@Override
208-
public void destroy() throws Exception {
208+
public void destroy() {
209209
flush();
210210
}
211211

@@ -254,7 +254,7 @@ private void loadMetadata() {
254254
inputStream.close();
255255
}
256256
}
257-
catch (Exception e2) {
257+
catch (@SuppressWarnings("unused") Exception e2) {
258258
// non fatal
259259
this.logger.warn("Failed to close InputStream for: " + this.file.getAbsolutePath());
260260
}

spring-integration-core/src/main/java/org/springframework/integration/routingslip/ExpressionEvaluatingRoutingSlipRouteStrategy.java

+2-2
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.
@@ -90,7 +90,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
9090
}
9191

9292
@Override
93-
public void afterPropertiesSet() throws Exception {
93+
public void afterPropertiesSet() {
9494
if (this.evaluationContext == null) {
9595
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
9696
}

spring-integration-core/src/main/java/org/springframework/integration/store/MessageGroupStoreReaper.java

+3-3
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.
@@ -90,12 +90,12 @@ public void setMessageGroupStore(MessageGroupStore messageGroupStore) {
9090
}
9191

9292
@Override
93-
public void afterPropertiesSet() throws Exception {
93+
public void afterPropertiesSet() {
9494
Assert.state(this.messageGroupStore != null, "A MessageGroupStore must be provided");
9595
}
9696

9797
@Override
98-
public void destroy() throws Exception {
98+
public void destroy() {
9999
if (this.expireOnDestroy) {
100100
if (this.isRunning()) {
101101
logger.info("Expiring all messages from message group store: " + this.messageGroupStore);

spring-integration-core/src/main/java/org/springframework/integration/support/converter/SimpleMessageConverter.java

+3-3
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-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.
@@ -134,7 +134,7 @@ private class DefaultInboundMessageMapper implements InboundMessageMapper<Object
134134
}
135135

136136
@Override
137-
public Message<?> toMessage(Object object, @Nullable Map<String, Object> headers) throws Exception {
137+
public Message<?> toMessage(Object object, @Nullable Map<String, Object> headers) {
138138
if (object == null) {
139139
return null;
140140
}
@@ -157,7 +157,7 @@ private static class DefaultOutboundMessageMapper implements OutboundMessageMapp
157157
}
158158

159159
@Override
160-
public Object fromMessage(Message<?> message) throws Exception {
160+
public Object fromMessage(Message<?> message) {
161161
return (message != null) ? message.getPayload() : null;
162162
}
163163

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

+9-9
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-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.
@@ -69,15 +69,15 @@ protected MessageBuilderFactory getMessageBuilderFactory() {
6969
}
7070

7171
@Override
72-
public Message<?> doInParser(JsonInboundMessageMapper messageMapper, String jsonMessage,
73-
@Nullable Map<String, Object> headers) throws Exception {
72+
public Message<?> doInParser(JsonInboundMessageMapper messageMapperToUse, String jsonMessage,
73+
@Nullable Map<String, Object> headers) {
7474

7575
if (this.messageMapper == null) {
76-
this.messageMapper = messageMapper;
76+
this.messageMapper = messageMapperToUse;
7777
}
7878
P parser = this.createJsonParser(jsonMessage);
7979

80-
if (messageMapper.isMapToPayload()) {
80+
if (messageMapperToUse.isMapToPayload()) {
8181
Object payload = readPayload(parser, jsonMessage);
8282
return getMessageBuilderFactory()
8383
.withPayload(payload)
@@ -89,7 +89,7 @@ public Message<?> doInParser(JsonInboundMessageMapper messageMapper, String json
8989
}
9090
}
9191

92-
protected Object readPayload(P parser, String jsonMessage) throws Exception {
92+
protected Object readPayload(P parser, String jsonMessage) {
9393
try {
9494
return this.objectMapper.fromJson(parser, this.messageMapper.getPayloadType());
9595
}
@@ -99,7 +99,7 @@ protected Object readPayload(P parser, String jsonMessage) throws Exception {
9999
}
100100
}
101101

102-
protected Object readHeader(P parser, String headerName, String jsonMessage) throws Exception {
102+
protected Object readHeader(P parser, String headerName, String jsonMessage) {
103103
Class<?> headerType = this.messageMapper.getHeaderTypes().getOrDefault(headerName, Object.class);
104104
try {
105105
return this.objectMapper.fromJson(parser, (Type) headerType);
@@ -111,8 +111,8 @@ protected Object readHeader(P parser, String headerName, String jsonMessage) thr
111111
}
112112

113113
protected abstract Message<?> parseWithHeaders(P parser, String jsonMessage,
114-
@Nullable Map<String, Object> headers) throws Exception;
114+
@Nullable Map<String, Object> headers);
115115

116-
protected abstract P createJsonParser(String jsonMessage) throws Exception;
116+
protected abstract P createJsonParser(String jsonMessage);
117117

118118
}

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-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,6 +17,7 @@
1717
package org.springframework.integration.support.json;
1818

1919
import java.io.File;
20+
import java.io.IOException;
2021
import java.io.InputStream;
2122
import java.io.Reader;
2223
import java.lang.reflect.Type;
@@ -57,17 +58,17 @@ protected ClassLoader getClassLoader() {
5758
}
5859

5960
@Override
60-
public <T> T fromJson(Object json, Class<T> valueType) throws Exception {
61-
return this.fromJson(json, this.constructType(valueType));
61+
public <T> T fromJson(Object json, Class<T> valueType) throws IOException {
62+
return fromJson(json, this.constructType(valueType));
6263
}
6364

6465
@Override
65-
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws Exception {
66-
J javaType = this.extractJavaType(javaTypes);
66+
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws IOException {
67+
J javaType = extractJavaType(javaTypes);
6768
return this.fromJson(json, javaType);
6869
}
6970

70-
protected J createJavaType(Map<String, Object> javaTypes, String javaTypeKey) throws Exception {
71+
protected J createJavaType(Map<String, Object> javaTypes, String javaTypeKey) {
7172
Object classValue = javaTypes.get(javaTypeKey);
7273
if (classValue == null) {
7374
throw new IllegalArgumentException("Could not resolve '" + javaTypeKey + "' in 'javaTypes'.");
@@ -78,16 +79,21 @@ protected J createJavaType(Map<String, Object> javaTypes, String javaTypeKey) th
7879
aClass = (Class<?>) classValue;
7980
}
8081
else {
81-
aClass = ClassUtils.forName(classValue.toString(), this.classLoader);
82+
try {
83+
aClass = ClassUtils.forName(classValue.toString(), this.classLoader);
84+
}
85+
catch (ClassNotFoundException | LinkageError e) {
86+
throw new IllegalStateException(e);
87+
}
8288
}
8389

8490
return this.constructType(aClass);
8591
}
8692
}
8793

88-
protected abstract <T> T fromJson(Object json, J type) throws Exception;
94+
protected abstract <T> T fromJson(Object json, J type) throws IOException;
8995

90-
protected abstract J extractJavaType(Map<String, Object> javaTypes) throws Exception;
96+
protected abstract J extractJavaType(Map<String, Object> javaTypes);
9197

9298
protected abstract J constructType(Type type);
9399

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public void setMapToPayload(boolean mapToPayload) {
6666
this.mapToPayload = mapToPayload;
6767
}
6868

69-
protected abstract Object readPayload(P parser, String jsonMessage) throws Exception;
69+
protected abstract Object readPayload(P parser, String jsonMessage);
7070

71-
protected abstract Map<String, Object> readHeaders(P parser, String jsonMessage) throws Exception;
71+
protected abstract Map<String, Object> readHeaders(P parser, String jsonMessage);
7272

7373
}

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.FileReader;
21+
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.io.PipedReader;
2324
import java.io.PipedWriter;
@@ -80,7 +81,7 @@ public void setBeanClassLoader(ClassLoader classLoader) {
8081
}
8182

8283
@Override
83-
public String toJson(Object value) throws Exception {
84+
public String toJson(Object value) {
8485
return this.objectMapper.writeValueAsString(value);
8586
}
8687

@@ -91,15 +92,15 @@ public void toJson(Object value, Writer writer) {
9192

9293
@Override
9394
@SuppressWarnings("unchecked")
94-
public Map<String, Object> toJsonNode(final Object value) throws Exception {
95+
public Map<String, Object> toJsonNode(final Object value) throws IOException {
9596
PipedReader in = new PipedReader();
9697
final PipedWriter out = new PipedWriter(in);
9798
Executors.newSingleThreadExecutor().execute(() -> toJson(value, out));
9899
return (Map<String, Object>) this.slurper.parse(in);
99100
}
100101

101102
@Override
102-
public <T> T fromJson(Object json, Class<T> type) throws Exception {
103+
public <T> T fromJson(Object json, Class<T> type) {
103104
if (json instanceof String) {
104105
return this.objectMapper.readValue((String) json, type);
105106
}
@@ -126,14 +127,14 @@ else if (json instanceof Reader) {
126127

127128
@Override
128129
@SuppressWarnings({ "unchecked", "rawtypes" })
129-
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws Exception {
130+
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws IOException {
130131
JsonParserAndMapper parser = this.objectMapper.parser();
131132

132-
Class<?> classType = this.createJavaType(javaTypes, JsonHeaders.TYPE_ID);
133+
Class<?> classType = createJavaType(javaTypes, JsonHeaders.TYPE_ID);
133134

134-
Class<?> contentClassType = this.createJavaType(javaTypes, JsonHeaders.CONTENT_TYPE_ID);
135+
Class<?> contentClassType = createJavaType(javaTypes, JsonHeaders.CONTENT_TYPE_ID);
135136

136-
Class<?> keyClassType = this.createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
137+
Class<?> keyClassType = createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
137138

138139
if (keyClassType != null) {
139140
logger.warn("Boon doesn't support the Map 'key' conversion. Will be returned raw Map<String, Object>");
@@ -190,21 +191,26 @@ else if (json instanceof Reader) {
190191
return (T) fromJson(json, classType);
191192
}
192193

193-
protected Class<?> createJavaType(Map<String, Object> javaTypes, String javaTypeKey) throws Exception {
194+
protected Class<?> createJavaType(Map<String, Object> javaTypes, String javaTypeKey) {
194195
Object classValue = javaTypes.get(javaTypeKey);
195196
if (classValue instanceof Class<?>) {
196197
return (Class<?>) classValue;
197198
}
198199
else if (classValue != null) {
199-
return ClassUtils.forName(classValue.toString(), this.classLoader);
200+
try {
201+
return ClassUtils.forName(classValue.toString(), this.classLoader);
202+
}
203+
catch (ClassNotFoundException | LinkageError e) {
204+
throw new IllegalStateException(e);
205+
}
200206
}
201207
else {
202208
return null;
203209
}
204210
}
205211

206212
@Override
207-
public <T> T fromJson(Object parser, Type valueType) throws Exception {
213+
public <T> T fromJson(Object parser, Type valueType) {
208214
throw new UnsupportedOperationException("Boon doesn't support JSON reader parser abstraction");
209215
}
210216

0 commit comments

Comments
 (0)