Skip to content

Commit e5122d0

Browse files
committed
Avoid wrapping in plain RuntimeException in favor of IllegalStateException
1 parent e7a53e3 commit e5122d0

File tree

2 files changed

+47
-55
lines changed

2 files changed

+47
-55
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -83,18 +83,10 @@ public abstract class AbstractMethodMessageHandler<T>
8383

8484
protected final Log logger = LogFactory.getLog(getClass());
8585

86-
private Collection<String> destinationPrefixes = new ArrayList<String>();
87-
8886
private final List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<HandlerMethodArgumentResolver>(4);
8987

9088
private final List<HandlerMethodReturnValueHandler> customReturnValueHandlers = new ArrayList<HandlerMethodReturnValueHandler>(4);
9189

92-
private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
93-
94-
private HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite();
95-
96-
private ApplicationContext applicationContext;
97-
9890
private final Map<T, HandlerMethod> handlerMethods = new LinkedHashMap<T, HandlerMethod>();
9991

10092
private final MultiValueMap<String, T> destinationLookup = new LinkedMultiValueMap<String, T>();
@@ -105,6 +97,20 @@ public abstract class AbstractMethodMessageHandler<T>
10597
private final Map<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver> exceptionHandlerAdviceCache =
10698
new LinkedHashMap<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver>(64);
10799

100+
private Collection<String> destinationPrefixes = new ArrayList<String>();
101+
102+
private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
103+
104+
private HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite();
105+
106+
private ApplicationContext applicationContext;
107+
108+
/**
109+
* Return the configured destination prefixes.
110+
*/
111+
public Collection<String> getDestinationPrefixes() {
112+
return this.destinationPrefixes;
113+
}
108114

109115
/**
110116
* When this property is configured only messages to destinations matching
@@ -125,10 +131,10 @@ public void setDestinationPrefixes(Collection<String> prefixes) {
125131
}
126132

127133
/**
128-
* Return the configured destination prefixes.
134+
* Return the configured custom argument resolvers, if any.
129135
*/
130-
public Collection<String> getDestinationPrefixes() {
131-
return this.destinationPrefixes;
136+
public List<HandlerMethodArgumentResolver> getCustomArgumentResolvers() {
137+
return this.customArgumentResolvers;
132138
}
133139

134140
/**
@@ -144,10 +150,10 @@ public void setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> custo
144150
}
145151

146152
/**
147-
* Return the configured custom argument resolvers, if any.
153+
* Return the configured custom return value handlers, if any.
148154
*/
149-
public List<HandlerMethodArgumentResolver> getCustomArgumentResolvers() {
150-
return this.customArgumentResolvers;
155+
public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers() {
156+
return this.customReturnValueHandlers;
151157
}
152158

153159
/**
@@ -162,11 +168,8 @@ public void setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> c
162168
}
163169
}
164170

165-
/**
166-
* Return the configured custom return value handlers, if any.
167-
*/
168-
public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers() {
169-
return this.customReturnValueHandlers;
171+
public List<HandlerMethodArgumentResolver> getArgumentResolvers() {
172+
return this.argumentResolvers.getResolvers();
170173
}
171174

172175
/**
@@ -182,8 +185,8 @@ public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
182185
this.argumentResolvers.addResolvers(argumentResolvers);
183186
}
184187

185-
public List<HandlerMethodArgumentResolver> getArgumentResolvers() {
186-
return this.argumentResolvers.getResolvers();
188+
public List<HandlerMethodReturnValueHandler> getReturnValueHandlers() {
189+
return this.returnValueHandlers.getReturnValueHandlers();
187190
}
188191

189192
/**
@@ -199,26 +202,21 @@ public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnV
199202
this.returnValueHandlers.addHandlers(returnValueHandlers);
200203
}
201204

202-
public List<HandlerMethodReturnValueHandler> getReturnValueHandlers() {
203-
return this.returnValueHandlers.getReturnValueHandlers();
204-
}
205-
206205
/**
207206
* Return a map with all handler methods and their mappings.
208207
*/
209208
public Map<T, HandlerMethod> getHandlerMethods() {
210209
return Collections.unmodifiableMap(this.handlerMethods);
211210
}
212211

213-
@Override
214-
public void setApplicationContext(ApplicationContext applicationContext) {
215-
this.applicationContext = applicationContext;
216-
}
217-
218212
public ApplicationContext getApplicationContext() {
219213
return this.applicationContext;
220214
}
221215

216+
@Override
217+
public void setApplicationContext(ApplicationContext applicationContext) {
218+
this.applicationContext = applicationContext;
219+
}
222220

223221
@Override
224222
public void afterPropertiesSet() {
@@ -434,7 +432,7 @@ protected void handleMessageInternal(Message<?> message, String lookupDestinatio
434432
Collections.sort(matches, comparator);
435433

436434
if (logger.isTraceEnabled()) {
437-
logger.trace("Found " + matches.size() + " methods: " + matches);
435+
logger.trace("Found " + matches.size() + " handler methods: " + matches);
438436
}
439437

440438
Match bestMatch = matches.get(0);
@@ -472,7 +470,7 @@ private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> me
472470

473471

474472
protected void handleNoMatch(Set<T> ts, String lookupDestination, Message<?> message) {
475-
logger.debug("No matching methods.");
473+
logger.debug("No matching message handler methods.");
476474
}
477475

478476
/**
@@ -520,7 +518,7 @@ protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookup
520518
protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception ex, Message<?> message) {
521519
InvocableHandlerMethod invocable = getExceptionHandlerMethod(handlerMethod, ex);
522520
if (invocable == null) {
523-
logger.error("Unhandled exception", ex);
521+
logger.error("Unhandled exception from message handler method", ex);
524522
return;
525523
}
526524
invocable.setMessageMethodArgumentResolvers(this.argumentResolvers);
@@ -598,7 +596,7 @@ private class Match {
598596

599597
private final HandlerMethod handlerMethod;
600598

601-
private Match(T mapping, HandlerMethod handlerMethod) {
599+
public Match(T mapping, HandlerMethod handlerMethod) {
602600
this.mapping = mapping;
603601
this.handlerMethod = handlerMethod;
604602
}
@@ -631,7 +629,6 @@ private class ReturnValueListenableFutureCallback implements ListenableFutureCal
631629

632630
private final Message<?> message;
633631

634-
635632
public ReturnValueListenableFutureCallback(InvocableHandlerMethod handlerMethod, Message<?> message) {
636633
this.handlerMethod = handlerMethod;
637634
this.message = message;
@@ -654,7 +651,7 @@ public void onFailure(Throwable ex) {
654651
}
655652

656653
private void handleFailure(Throwable ex) {
657-
Exception cause = (ex instanceof Exception ? (Exception) ex : new RuntimeException(ex));
654+
Exception cause = (ex instanceof Exception ? (Exception) ex : new IllegalStateException(ex));
658655
processHandlerMethodException(this.handlerMethod, cause, this.message);
659656
}
660657
}

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public MockHttpServletRequest buildRequest(ServletContext servletContext) {
105105
String httpMethod = this.webRequest.getHttpMethod().name();
106106
UriComponents uriComponents = uriComponents();
107107

108-
MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(servletContext, httpMethod,
109-
uriComponents.getPath());
108+
MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(
109+
servletContext, httpMethod, uriComponents.getPath());
110110
parent(request, this.parentBuilder);
111111
request.setServerName(uriComponents.getHost()); // needs to be first for additional headers
112112
authType(request);
@@ -123,7 +123,7 @@ public MockHttpServletRequest buildRequest(ServletContext servletContext) {
123123
request.setProtocol("HTTP/1.1");
124124
request.setQueryString(uriComponents.getQuery());
125125
request.setScheme(uriComponents.getScheme());
126-
pathInfo(uriComponents,request);
126+
request.setPathInfo(null);
127127

128128
return postProcess(request);
129129
}
@@ -223,14 +223,14 @@ private void content(MockHttpServletRequest request, String charset) {
223223
try {
224224
request.setContent(requestBody.getBytes(charset));
225225
}
226-
catch (UnsupportedEncodingException e) {
227-
throw new RuntimeException(e);
226+
catch (UnsupportedEncodingException ex) {
227+
throw new IllegalStateException(ex);
228228
}
229229
}
230230

231231
private void contentType(MockHttpServletRequest request) {
232232
String contentType = header("Content-Type");
233-
request.setContentType(contentType == null ? MediaType.ALL_VALUE.toString() : contentType);
233+
request.setContentType(contentType != null ? contentType : MediaType.ALL_VALUE);
234234
}
235235

236236
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) {
@@ -245,8 +245,8 @@ private void contextPath(MockHttpServletRequest request, UriComponents uriCompon
245245
}
246246
else {
247247
if (!uriComponents.getPath().startsWith(this.contextPath)) {
248-
throw new IllegalArgumentException(uriComponents.getPath() + " should start with contextPath "
249-
+ this.contextPath);
248+
throw new IllegalArgumentException(uriComponents.getPath() + " should start with contextPath " +
249+
this.contextPath);
250250
}
251251
request.setContextPath(this.contextPath);
252252
}
@@ -375,8 +375,8 @@ private String urlDecode(String value) {
375375
try {
376376
return URLDecoder.decode(value, "UTF-8");
377377
}
378-
catch (UnsupportedEncodingException e) {
379-
throw new RuntimeException(e);
378+
catch (UnsupportedEncodingException ex) {
379+
throw new IllegalStateException(ex);
380380
}
381381
}
382382

@@ -397,10 +397,6 @@ private Locale parseLocale(String locale) {
397397
return new Locale(language, country, qualifier);
398398
}
399399

400-
private void pathInfo(UriComponents uriComponents, MockHttpServletRequest request) {
401-
request.setPathInfo(null);
402-
}
403-
404400
private void servletPath(MockHttpServletRequest request, String requestPath) {
405401
String servletPath = requestPath.substring(request.getContextPath().length());
406402
if ("".equals(servletPath)) {
@@ -455,6 +451,9 @@ public Object merge(Object parent) {
455451
return this;
456452
}
457453

454+
private CookieManager getCookieManager() {
455+
return this.webClient.getCookieManager();
456+
}
458457

459458
/**
460459
* An extension to {@link MockHttpServletRequest} that ensures that
@@ -519,8 +518,4 @@ public void invalidate() {
519518
}
520519
}
521520

522-
private CookieManager getCookieManager() {
523-
return this.webClient.getCookieManager();
524-
}
525-
526521
}

0 commit comments

Comments
 (0)