@@ -112,8 +112,8 @@ public DelegatingInvocableHandler(List<InvocableHandlerMethod> handlers,
112
112
this .bean = bean ;
113
113
this .resolver = beanExpressionResolver ;
114
114
this .beanExpressionContext = beanExpressionContext ;
115
- this .beanFactory = beanFactory instanceof ConfigurableListableBeanFactory
116
- ? ( ConfigurableListableBeanFactory ) beanFactory
115
+ this .beanFactory = beanFactory instanceof ConfigurableListableBeanFactory configurableListableBeanFactory
116
+ ? configurableListableBeanFactory
117
117
: null ;
118
118
this .validator = validator == null ? null : new PayloadValidator (validator );
119
119
}
@@ -124,7 +124,7 @@ private void checkSpecial(@Nullable InvocableHandlerMethod handler) {
124
124
}
125
125
Parameter [] parameters = handler .getMethod ().getParameters ();
126
126
for (Parameter parameter : parameters ) {
127
- if (parameter . getType () .equals (ConsumerRecordMetadata . class )) {
127
+ if (ConsumerRecordMetadata . class .equals (parameter . getType () )) {
128
128
this .handlerMetadataAware .put (handler , true );
129
129
return ;
130
130
}
@@ -148,7 +148,7 @@ public Object getBean() {
148
148
* or the method raised an exception.
149
149
*/
150
150
public Object invoke (Message <?> message , Object ... providedArgs ) throws Exception { //NOSONAR
151
- Class <? extends Object > payloadClass = message .getPayload ().getClass ();
151
+ Class <?> payloadClass = message .getPayload ().getClass ();
152
152
InvocableHandlerMethod handler = getHandlerForPayload (payloadClass );
153
153
if (this .validator != null && this .defaultHandler != null ) {
154
154
MethodParameter parameter = this .payloadMethodParameters .get (handler );
@@ -175,7 +175,7 @@ public Object invoke(Message<?> message, Object... providedArgs) throws Exceptio
175
175
* @param payloadClass the payload class.
176
176
* @return the handler.
177
177
*/
178
- protected InvocableHandlerMethod getHandlerForPayload (Class <? extends Object > payloadClass ) {
178
+ protected InvocableHandlerMethod getHandlerForPayload (Class <?> payloadClass ) {
179
179
InvocableHandlerMethod handler = this .cachedHandlers .get (payloadClass );
180
180
if (handler == null ) {
181
181
handler = findHandlerForPayload (payloadClass );
@@ -246,36 +246,32 @@ protected InvocableHandlerMethod findHandlerForPayload(Class<? extends Object> p
246
246
InvocableHandlerMethod result = null ;
247
247
for (InvocableHandlerMethod handler : this .handlers ) {
248
248
if (matchHandlerMethod (payloadClass , handler )) {
249
- if (result != null ) {
250
- boolean resultIsDefault = result .equals (this .defaultHandler );
251
- if (!handler .equals (this .defaultHandler ) && !resultIsDefault ) {
249
+ if (result != null && !result .equals (this .defaultHandler )) {
250
+ if (!handler .equals (this .defaultHandler )) {
252
251
throw new KafkaException ("Ambiguous methods for payload type: " + payloadClass + ": " +
253
252
result .getMethod ().getName () + " and " + handler .getMethod ().getName ());
254
253
}
255
- if (!resultIsDefault ) {
256
- continue ; // otherwise replace the result with the actual match
257
- }
254
+ continue ; // otherwise replace the result with the actual match
258
255
}
259
256
result = handler ;
260
257
}
261
258
}
262
259
return result != null ? result : this .defaultHandler ;
263
260
}
264
261
265
- protected boolean matchHandlerMethod (Class <? extends Object > payloadClass , InvocableHandlerMethod handler ) {
262
+ protected boolean matchHandlerMethod (Class <?> payloadClass , InvocableHandlerMethod handler ) {
266
263
Method method = handler .getMethod ();
267
264
Annotation [][] parameterAnnotations = method .getParameterAnnotations ();
268
265
// Single param; no annotation or not @Header
269
266
if (parameterAnnotations .length == 1 ) {
270
267
MethodParameter methodParameter = new MethodParameter (method , 0 );
271
- if ((methodParameter .getParameterAnnotations ().length == 0
272
- || !methodParameter .hasParameterAnnotation (Header .class ))
273
- && methodParameter .getParameterType ().isAssignableFrom (payloadClass )) {
268
+ boolean isPayload = assignPayload (methodParameter , payloadClass );
269
+ if (isPayload ) {
274
270
if (this .validator != null ) {
275
271
this .payloadMethodParameters .put (handler , methodParameter );
276
272
}
277
- return true ;
278
273
}
274
+ return isPayload ;
279
275
}
280
276
281
277
MethodParameter foundCandidate = findCandidate (payloadClass , method , parameterAnnotations );
@@ -285,14 +281,12 @@ protected boolean matchHandlerMethod(Class<? extends Object> payloadClass, Invoc
285
281
return foundCandidate != null ;
286
282
}
287
283
288
- private MethodParameter findCandidate ( Class <? extends Object > payloadClass , Method method ,
289
- Annotation [][] parameterAnnotations ) {
284
+ @ Nullable
285
+ private MethodParameter findCandidate ( Class <?> payloadClass , Method method , Annotation [][] parameterAnnotations ) {
290
286
MethodParameter foundCandidate = null ;
291
287
for (int i = 0 ; i < parameterAnnotations .length ; i ++) {
292
288
MethodParameter methodParameter = new MethodParameter (method , i );
293
- if ((methodParameter .getParameterAnnotations ().length == 0
294
- || !methodParameter .hasParameterAnnotation (Header .class ))
295
- && methodParameter .getParameterType ().isAssignableFrom (payloadClass )) {
289
+ if (assignPayload (methodParameter , payloadClass )) {
296
290
if (foundCandidate != null ) {
297
291
throw new KafkaException ("Ambiguous payload parameter for " + method .toGenericString ());
298
292
}
@@ -316,15 +310,20 @@ public boolean hasDefaultHandler() {
316
310
return this .defaultHandler != null ;
317
311
}
318
312
313
+ private boolean assignPayload (MethodParameter methodParameter , Class <?> payloadClass ) {
314
+ return (methodParameter .getParameterAnnotations ().length == 0
315
+ || !methodParameter .hasParameterAnnotation (Header .class ))
316
+ && methodParameter .getParameterType ().isAssignableFrom (payloadClass );
317
+ }
318
+
319
319
private static final class PayloadValidator extends PayloadMethodArgumentResolver {
320
320
321
321
PayloadValidator (Validator validator ) {
322
322
super (new MessageConverter () { // Required but never used
323
323
324
324
@ Override
325
325
@ Nullable
326
- public Message <?> toMessage (Object payload , @ Nullable
327
- MessageHeaders headers ) {
326
+ public Message <?> toMessage (Object payload , @ Nullable MessageHeaders headers ) {
328
327
return null ;
329
328
}
330
329
0 commit comments