Skip to content

Commit 66a7162

Browse files
artembilangaryrussell
authored andcommitted
GH-842: Fix NPEs in KafkaListenerAnnotationBPP (#864)
* GH-842: Fix NPEs in KafkaListenerAnnotationBPP Fixes #842 **Cherry-pick to 2.1.x, 2.0.x and 1.3.x** * * Move null check to the `else` branch * * Do not use `if (resolved == null)` at all
1 parent 6bd4694 commit 66a7162

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ public class KafkaListenerAnnotationBeanPostProcessor<K, V>
118118
*/
119119
public static final String DEFAULT_KAFKA_LISTENER_CONTAINER_FACTORY_BEAN_NAME = "kafkaListenerContainerFactory";
120120

121-
private final Set<Class<?>> nonAnnotatedClasses =
122-
Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>(64));
121+
private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64));
123122

124123
private final Log logger = LogFactory.getLog(getClass());
125124

@@ -241,7 +240,7 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
241240
Class<?> targetClass = AopUtils.getTargetClass(bean);
242241
Collection<KafkaListener> classLevelListeners = findListenerAnnotations(targetClass);
243242
final boolean hasClassLevelListeners = classLevelListeners.size() > 0;
244-
final List<Method> multiMethods = new ArrayList<Method>();
243+
final List<Method> multiMethods = new ArrayList<>();
245244
Map<Method, Set<KafkaListener>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
246245
new MethodIntrospector.MetadataLookup<Set<KafkaListener>>() {
247246

@@ -288,7 +287,7 @@ public Set<KafkaListener> inspect(Method method) {
288287
* AnnotationUtils.getRepeatableAnnotations does not look at interfaces
289288
*/
290289
private Collection<KafkaListener> findListenerAnnotations(Class<?> clazz) {
291-
Set<KafkaListener> listeners = new HashSet<KafkaListener>();
290+
Set<KafkaListener> listeners = new HashSet<>();
292291
KafkaListener ann = AnnotationUtils.findAnnotation(clazz, KafkaListener.class);
293292
if (ann != null) {
294293
listeners.add(ann);
@@ -318,12 +317,13 @@ private Set<KafkaListener> findListenerAnnotations(Method method) {
318317

319318
private void processMultiMethodListeners(Collection<KafkaListener> classLevelListeners, List<Method> multiMethods,
320319
Object bean, String beanName) {
321-
List<Method> checkedMethods = new ArrayList<Method>();
320+
321+
List<Method> checkedMethods = new ArrayList<>();
322322
for (Method method : multiMethods) {
323323
checkedMethods.add(checkProxy(method, bean));
324324
}
325325
for (KafkaListener classLevelListener : classLevelListeners) {
326-
MultiMethodKafkaListenerEndpoint<K, V> endpoint = new MultiMethodKafkaListenerEndpoint<K, V>(checkedMethods,
326+
MultiMethodKafkaListenerEndpoint<K, V> endpoint = new MultiMethodKafkaListenerEndpoint<>(checkedMethods,
327327
bean);
328328
endpoint.setBeanFactory(this.beanFactory);
329329
processListener(endpoint, classLevelListener, bean, bean.getClass(), beanName);
@@ -332,7 +332,7 @@ private void processMultiMethodListeners(Collection<KafkaListener> classLevelLis
332332

333333
protected void processKafkaListener(KafkaListener kafkaListener, Method method, Object bean, String beanName) {
334334
Method methodToUse = checkProxy(method, bean);
335-
MethodKafkaListenerEndpoint<K, V> endpoint = new MethodKafkaListenerEndpoint<K, V>();
335+
MethodKafkaListenerEndpoint<K, V> endpoint = new MethodKafkaListenerEndpoint<>();
336336
endpoint.setMethod(methodToUse);
337337
endpoint.setBeanFactory(this.beanFactory);
338338
String errorHandlerBeanName = resolveExpressionAsString(kafkaListener.errorHandler(), "errorHandler");
@@ -462,7 +462,7 @@ private Pattern resolvePattern(KafkaListener kafkaListener) {
462462
else if (resolved instanceof String) {
463463
pattern = Pattern.compile((String) resolved);
464464
}
465-
else {
465+
else if (resolved != null) {
466466
throw new IllegalStateException(
467467
"topicPattern must resolve to a Pattern or String, not " + resolved.getClass());
468468
}
@@ -603,16 +603,15 @@ private String resolveExpressionAsString(String value, String attribute) {
603603
if (resolved instanceof String) {
604604
return (String) resolved;
605605
}
606-
else {
606+
else if (resolved != null) {
607607
throw new IllegalStateException("The [" + attribute + "] must resolve to a String. "
608608
+ "Resolved to [" + resolved.getClass() + "] for [" + value + "]");
609609
}
610+
return null;
610611
}
611612

612613
private Object resolveExpression(String value) {
613-
String resolvedValue = resolve(value);
614-
615-
return this.resolver.evaluate(resolvedValue, this.expressionContext);
614+
return this.resolver.evaluate(resolve(value), this.expressionContext);
616615
}
617616

618617
/**

0 commit comments

Comments
 (0)