Skip to content

Commit fa624cd

Browse files
committed
Avoid wrapping in plain RuntimeException in favor of IllegalStateException
(cherry picked from commit e5122d0)
1 parent cd37873 commit fa624cd

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

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

Lines changed: 49 additions & 45 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,17 +83,13 @@ 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();
90+
private final HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
9391

94-
private HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite();
95-
96-
private ApplicationContext applicationContext;
92+
private final HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite();
9793

9894
private final Map<T, HandlerMethod> handlerMethods = new LinkedHashMap<T, HandlerMethod>();
9995

@@ -105,6 +101,16 @@ public abstract class AbstractMethodMessageHandler<T>
105101
private final Map<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver> exceptionHandlerAdviceCache =
106102
new LinkedHashMap<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver>(64);
107103

104+
private Collection<String> destinationPrefixes = new ArrayList<String>();
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
/**
@@ -163,10 +169,10 @@ public void setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> c
163169
}
164170

165171
/**
166-
* Return the configured custom return value handlers, if any.
172+
* Return the configured argument resolvers, if any.
167173
*/
168-
public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers() {
169-
return this.customReturnValueHandlers;
174+
public List<HandlerMethodArgumentResolver> getArgumentResolvers() {
175+
return this.argumentResolvers.getResolvers();
170176
}
171177

172178
/**
@@ -182,8 +188,11 @@ public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
182188
this.argumentResolvers.addResolvers(argumentResolvers);
183189
}
184190

185-
public List<HandlerMethodArgumentResolver> getArgumentResolvers() {
186-
return this.argumentResolvers.getResolvers();
191+
/**
192+
* Return the configured return value handlers, if any.
193+
*/
194+
public List<HandlerMethodReturnValueHandler> getReturnValueHandlers() {
195+
return this.returnValueHandlers.getReturnValueHandlers();
187196
}
188197

189198
/**
@@ -199,27 +208,15 @@ public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnV
199208
this.returnValueHandlers.addHandlers(returnValueHandlers);
200209
}
201210

202-
public List<HandlerMethodReturnValueHandler> getReturnValueHandlers() {
203-
return this.returnValueHandlers.getReturnValueHandlers();
204-
}
205-
206-
/**
207-
* Return a map with all handler methods and their mappings.
208-
*/
209-
public Map<T, HandlerMethod> getHandlerMethods() {
210-
return Collections.unmodifiableMap(this.handlerMethods);
211+
public ApplicationContext getApplicationContext() {
212+
return this.applicationContext;
211213
}
212214

213215
@Override
214216
public void setApplicationContext(ApplicationContext applicationContext) {
215217
this.applicationContext = applicationContext;
216218
}
217219

218-
public ApplicationContext getApplicationContext() {
219-
return this.applicationContext;
220-
}
221-
222-
223220
@Override
224221
public void afterPropertiesSet() {
225222
if (this.argumentResolvers.getResolvers().isEmpty()) {
@@ -366,6 +363,13 @@ protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean, Abstract
366363
this.exceptionHandlerAdviceCache.put(bean, resolver);
367364
}
368365

366+
/**
367+
* Return a map with all handler methods and their mappings.
368+
*/
369+
public Map<T, HandlerMethod> getHandlerMethods() {
370+
return Collections.unmodifiableMap(this.handlerMethods);
371+
}
372+
369373

370374
@Override
371375
public void handleMessage(Message<?> message) throws MessagingException {
@@ -427,14 +431,14 @@ protected void handleMessageInternal(Message<?> message, String lookupDestinatio
427431
addMatchesToCollection(allMappings, message, matches);
428432
}
429433
if (matches.isEmpty()) {
430-
handleNoMatch(handlerMethods.keySet(), lookupDestination, message);
434+
handleNoMatch(this.handlerMethods.keySet(), lookupDestination, message);
431435
return;
432436
}
433437
Comparator<Match> comparator = new MatchComparator(getMappingComparator(message));
434438
Collections.sort(matches, comparator);
435439

436440
if (logger.isTraceEnabled()) {
437-
logger.trace("Found " + matches.size() + " methods: " + matches);
441+
logger.trace("Found " + matches.size() + " handler methods: " + matches);
438442
}
439443

440444
Match bestMatch = matches.get(0);
@@ -451,7 +455,6 @@ protected void handleMessageInternal(Message<?> message, String lookupDestinatio
451455
handleMatch(bestMatch.mapping, bestMatch.handlerMethod, lookupDestination, message);
452456
}
453457

454-
455458
private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> message, List<Match> matches) {
456459
for (T mapping : mappingsToCheck) {
457460
T match = getMatchingMapping(mapping, message);
@@ -470,6 +473,10 @@ private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> me
470473
*/
471474
protected abstract T getMatchingMapping(T mapping, Message<?> message);
472475

476+
protected void handleNoMatch(Set<T> ts, String lookupDestination, Message<?> message) {
477+
logger.debug("No matching message handler methods.");
478+
}
479+
473480
/**
474481
* Return a comparator for sorting matching mappings.
475482
* The returned comparator should sort 'better' matches higher.
@@ -478,7 +485,6 @@ private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> me
478485
*/
479486
protected abstract Comparator<T> getMappingComparator(Message<?> message);
480487

481-
482488
protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message) {
483489
if (logger.isDebugEnabled()) {
484490
logger.debug("Invoking " + handlerMethod.getShortLogMessage());
@@ -515,7 +521,7 @@ protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookup
515521
protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception ex, Message<?> message) {
516522
InvocableHandlerMethod invocable = getExceptionHandlerMethod(handlerMethod, ex);
517523
if (invocable == null) {
518-
logger.error("Unhandled exception", ex);
524+
logger.error("Unhandled exception from message handler method", ex);
519525
return;
520526
}
521527
invocable.setMessageMethodArgumentResolvers(this.argumentResolvers);
@@ -535,8 +541,6 @@ protected void processHandlerMethodException(HandlerMethod handlerMethod, Except
535541
}
536542
}
537543

538-
protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class<?> beanType);
539-
540544
/**
541545
* Find an {@code @MessageExceptionHandler} method for the given exception.
542546
* The default implementation searches methods in the class hierarchy of the
@@ -575,9 +579,9 @@ protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handler
575579
return null;
576580
}
577581

578-
protected void handleNoMatch(Set<T> ts, String lookupDestination, Message<?> message) {
579-
logger.debug("No matching methods.");
580-
}
582+
protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(
583+
Class<?> beanType);
584+
581585

582586
@Override
583587
public String toString() {
@@ -596,7 +600,7 @@ private class Match {
596600

597601
private final HandlerMethod handlerMethod;
598602

599-
private Match(T mapping, HandlerMethod handlerMethod) {
603+
public Match(T mapping, HandlerMethod handlerMethod) {
600604
this.mapping = mapping;
601605
this.handlerMethod = handlerMethod;
602606
}
@@ -622,13 +626,13 @@ public int compare(Match match1, Match match2) {
622626
}
623627
}
624628

629+
625630
private class ReturnValueListenableFutureCallback implements ListenableFutureCallback<Object> {
626631

627632
private final InvocableHandlerMethod handlerMethod;
628633

629634
private final Message<?> message;
630635

631-
632636
public ReturnValueListenableFutureCallback(InvocableHandlerMethod handlerMethod, Message<?> message) {
633637
this.handlerMethod = handlerMethod;
634638
this.message = message;
@@ -651,7 +655,7 @@ public void onFailure(Throwable ex) {
651655
}
652656

653657
private void handleFailure(Throwable ex) {
654-
Exception cause = (ex instanceof Exception ? (Exception) ex : new RuntimeException(ex));
658+
Exception cause = (ex instanceof Exception ? (Exception) ex : new IllegalStateException(ex));
655659
processHandlerMethodException(this.handlerMethod, cause, this.message);
656660
}
657661
}

0 commit comments

Comments
 (0)