Skip to content

Commit 77444ab

Browse files
committed
Fix Sonar Issues
1 parent 6e4c819 commit 77444ab

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
public class KafkaListenerAnnotationBeanPostProcessor<K, V>
145145
implements BeanPostProcessor, Ordered, ApplicationContextAware, InitializingBean, SmartInitializingSingleton {
146146

147+
private static final String UNCHECKED = "unchecked";
148+
147149
private static final String THE_LEFT = "The [";
148150

149151
private static final String RESOLVED_TO_LEFT = "Resolved to [";
@@ -643,7 +645,7 @@ private void resolveContentTypeConverter(MethodKafkaListenerEndpoint<?, ?> endpo
643645
}
644646
}
645647

646-
@SuppressWarnings({ "rawtypes", "unchecked" })
648+
@SuppressWarnings({ "rawtypes", UNCHECKED })
647649
private void resolveFilter(MethodKafkaListenerEndpoint<?, ?> endpoint, KafkaListener kafkaListener) {
648650
Object filter = resolveExpression(kafkaListener.filter());
649651
if (filter instanceof RecordFilterStrategy) {
@@ -701,7 +703,7 @@ protected String noBeanFoundMessage(Object target, String listenerBeanName, Stri
701703
+ requestedBeanName + "' was found in the application context";
702704
}
703705

704-
@SuppressWarnings("unchecked")
706+
@SuppressWarnings(UNCHECKED)
705707
private void resolveKafkaProperties(MethodKafkaListenerEndpoint<?, ?> endpoint, String[] propertyStrings) {
706708
if (propertyStrings.length > 0) {
707709
Properties properties = new Properties();
@@ -870,7 +872,7 @@ else if (relativeToCurrentValue instanceof Boolean) {
870872
return relativeToCurrent;
871873
}
872874

873-
@SuppressWarnings("unchecked")
875+
@SuppressWarnings(UNCHECKED)
874876
private void resolveAsString(Object resolvedValue, List<String> result) {
875877
if (resolvedValue instanceof String[]) {
876878
for (Object object : (String[]) resolvedValue) {
@@ -891,7 +893,7 @@ else if (resolvedValue instanceof Iterable) {
891893
}
892894
}
893895

894-
@SuppressWarnings("unchecked")
896+
@SuppressWarnings(UNCHECKED)
895897
private void resolvePartitionAsInteger(String topic, Object resolvedValue,
896898
List<TopicPartitionOffset> result, @Nullable Long offset, boolean isRelative, boolean checkDups) {
897899

spring-kafka/src/main/java/org/springframework/kafka/listener/KafkaMessageListenerContainer.java

+27-22
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ public class KafkaMessageListenerContainer<K, V> // NOSONAR line count
152152

153153
private static final String UNUSED = "unused";
154154

155+
private static final String UNCHECKED = "unchecked";
156+
157+
private static final String RAWTYPES = "rawtypes";
158+
155159
private static final int DEFAULT_ACK_TIME = 5000;
156160

157161
private static final Map<String, Object> CONSUMER_CONFIG_DEFAULTS = ConsumerConfig.configDef().defaultValues();
@@ -547,12 +551,6 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
547551

548552
private static final String ERROR_HANDLER_THREW_AN_EXCEPTION = "Error handler threw an exception";
549553

550-
private static final String UNCHECKED = "unchecked";
551-
552-
private static final String RAWTYPES = "rawtypes";
553-
554-
private static final String RAW_TYPES = RAWTYPES;
555-
556554
private final LogAccessor logger = KafkaMessageListenerContainer.this.logger; // NOSONAR hide
557555

558556
private final ContainerProperties containerProperties = getContainerProperties();
@@ -611,7 +609,7 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
611609

612610
private final PlatformTransactionManager transactionManager = this.containerProperties.getTransactionManager();
613611

614-
@SuppressWarnings(RAW_TYPES)
612+
@SuppressWarnings(RAWTYPES)
615613
private final KafkaAwareTransactionManager kafkaTxManager =
616614
this.transactionManager instanceof KafkaAwareTransactionManager
617615
? ((KafkaAwareTransactionManager) this.transactionManager) : null;
@@ -708,10 +706,10 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
708706

709707
private final Map<TopicPartition, Boolean> wasIdlePartition;
710708

711-
private final byte[] listenerinfo = getListenerInfo();
712-
713709
private final Header infoHeader = new RecordHeader(KafkaHeaders.LISTENER_INFO, this.listenerinfo);
714710

711+
private final byte[] listenerinfo = getListenerInfo();
712+
715713
private final Set<TopicPartition> pausedForNack = new HashSet<>();
716714

717715
private Map<TopicPartition, OffsetMetadata> definedPartitions;
@@ -1666,16 +1664,20 @@ private void pausePartitionsIfNecessary() {
16661664
}
16671665

16681666
private void resumePartitionsIfNecessary() {
1669-
List<TopicPartition> partitionsToResume = getAssignedPartitions()
1670-
.stream()
1671-
.filter(tp -> !isPartitionPauseRequested(tp)
1672-
&& this.pausedPartitions.contains(tp))
1673-
.collect(Collectors.toList());
1674-
if (partitionsToResume.size() > 0) {
1675-
this.consumer.resume(partitionsToResume);
1676-
this.pausedPartitions.removeAll(partitionsToResume);
1677-
this.logger.debug(() -> "Resumed consumption from " + partitionsToResume);
1678-
partitionsToResume.forEach(KafkaMessageListenerContainer.this::publishConsumerPartitionResumedEvent);
1667+
Collection<TopicPartition> assigned = getAssignedPartitions();
1668+
if (assigned != null) {
1669+
List<TopicPartition> partitionsToResume = assigned
1670+
.stream()
1671+
.filter(tp -> !isPartitionPauseRequested(tp)
1672+
&& this.pausedPartitions.contains(tp))
1673+
.collect(Collectors.toList());
1674+
if (partitionsToResume.size() > 0) {
1675+
this.consumer.resume(partitionsToResume);
1676+
this.pausedPartitions.removeAll(partitionsToResume);
1677+
this.logger.debug(() -> "Resumed consumption from " + partitionsToResume);
1678+
partitionsToResume
1679+
.forEach(KafkaMessageListenerContainer.this::publishConsumerPartitionResumedEvent);
1680+
}
16791681
}
16801682
}
16811683

@@ -1988,7 +1990,7 @@ private void invokeBatchListener(final ConsumerRecords<K, V> recordsArg) {
19881990
}
19891991
}
19901992

1991-
@SuppressWarnings(RAW_TYPES)
1993+
@SuppressWarnings(RAWTYPES)
19921994
private void invokeBatchListenerInTx(final ConsumerRecords<K, V> records,
19931995
@Nullable final List<ConsumerRecord<K, V>> recordList) {
19941996

@@ -2299,7 +2301,7 @@ private void invokeRecordListener(final ConsumerRecords<K, V> records) {
22992301
* Invoke the listener with each record in a separate transaction.
23002302
* @param records the records.
23012303
*/
2302-
@SuppressWarnings(RAW_TYPES) // NOSONAR complexity
2304+
@SuppressWarnings(RAWTYPES) // NOSONAR complexity
23032305
private void invokeRecordListenerInTx(final ConsumerRecords<K, V> records) {
23042306
Iterator<ConsumerRecord<K, V>> iterator = records.iterator();
23052307
while (iterator.hasNext()) {
@@ -2485,7 +2487,10 @@ private void pauseForNackSleep() {
24852487
this.nackWake = System.currentTimeMillis() + this.nackSleep;
24862488
this.nackSleep = -1;
24872489
Set<TopicPartition> alreadyPaused = this.consumer.paused();
2488-
this.pausedForNack.addAll(getAssignedPartitions());
2490+
Collection<TopicPartition> assigned = getAssignedPartitions();
2491+
if (assigned != null) {
2492+
this.pausedForNack.addAll(assigned);
2493+
}
24892494
this.pausedForNack.removeAll(alreadyPaused);
24902495
this.logger.debug(() -> "Pausing for nack sleep: " + ListenerConsumer.this.pausedForNack);
24912496
try {

spring-kafka/src/test/java/org/springframework/kafka/retrytopic/RetryTopicConfigurerTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ void shouldInstantiateIfNotInContainer() {
357357
}
358358

359359
@Test
360+
@SuppressWarnings("deprecation")
360361
void shouldLogConsumerRecordMessage() {
361362
RetryTopicConfigurer.LoggingDltListenerHandlerMethod method =
362363
new RetryTopicConfigurer.LoggingDltListenerHandlerMethod();

0 commit comments

Comments
 (0)