Skip to content

Commit 2115cf6

Browse files
committed
Fix new Sonar smells
1 parent 0d45644 commit 2115cf6

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ protected void doStart() {
337337
publishConsumerFailedToStart();
338338
}
339339
}
340-
catch (@SuppressWarnings("unused") InterruptedException e) {
340+
catch (@SuppressWarnings(UNUSED) InterruptedException e) {
341341
Thread.currentThread().interrupt();
342342
}
343343
}
@@ -829,7 +829,7 @@ private MicrometerHolder obtainMicrometerHolder() {
829829
this.containerProperties.getMicrometerTags());
830830
}
831831
}
832-
catch (@SuppressWarnings("unused") IllegalStateException ex) {
832+
catch (@SuppressWarnings(UNUSED) IllegalStateException ex) {
833833
// NOSONAR - no micrometer or meter registry
834834
}
835835
return holder;
@@ -1508,7 +1508,7 @@ private void handleNack(final ConsumerRecords<K, V> records, final ConsumerRecor
15081508
try {
15091509
Thread.sleep(this.nackSleep);
15101510
}
1511-
catch (@SuppressWarnings("unused") InterruptedException e) {
1511+
catch (@SuppressWarnings(UNUSED) InterruptedException e) {
15121512
Thread.currentThread().interrupt();
15131513
}
15141514
this.nackSleep = -1;

spring-kafka/src/main/java/org/springframework/kafka/support/DefaultKafkaHeaderMapper.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -271,38 +271,8 @@ public void toHeaders(Headers source, final Map<String, Object> headers) {
271271
source.forEach(header -> {
272272
if (!(header.key().equals(JSON_TYPES))) {
273273
if (jsonTypes != null && jsonTypes.containsKey(header.key())) {
274-
Class<?> type = Object.class;
275274
String requestedType = jsonTypes.get(header.key());
276-
boolean trusted = false;
277-
try {
278-
trusted = trusted(requestedType);
279-
if (trusted) {
280-
type = ClassUtils.forName(requestedType, null);
281-
}
282-
}
283-
catch (Exception e) {
284-
logger.error(e, () -> "Could not load class for header: " + header.key());
285-
}
286-
if (String.class.equals(type) && header.value().length > 0 && header.value()[0] != '"') {
287-
headers.put(header.key(), new String(header.value(), getCharset()));
288-
}
289-
else {
290-
if (trusted) {
291-
try {
292-
Object value = decodeValue(header, type);
293-
headers.put(header.key(), value);
294-
}
295-
catch (IOException e) {
296-
logger.error(e, () ->
297-
"Could not decode json type: " + new String(header.value()) + " for key: "
298-
+ header.key());
299-
headers.put(header.key(), header.value());
300-
}
301-
}
302-
else {
303-
headers.put(header.key(), new NonTrustedHeaderType(header.value(), requestedType));
304-
}
305-
}
275+
populateJsonValueHeader(header, requestedType, headers);
306276
}
307277
else {
308278
headers.put(header.key(), headerValueToAddIn(header));
@@ -311,6 +281,40 @@ public void toHeaders(Headers source, final Map<String, Object> headers) {
311281
});
312282
}
313283

284+
private void populateJsonValueHeader(Header header, String requestedType, Map<String, Object> headers) {
285+
Class<?> type = Object.class;
286+
boolean trusted = false;
287+
try {
288+
trusted = trusted(requestedType);
289+
if (trusted) {
290+
type = ClassUtils.forName(requestedType, null);
291+
}
292+
}
293+
catch (Exception e) {
294+
logger.error(e, () -> "Could not load class for header: " + header.key());
295+
}
296+
if (String.class.equals(type) && header.value().length > 0 && header.value()[0] != '"') {
297+
headers.put(header.key(), new String(header.value(), getCharset()));
298+
}
299+
else {
300+
if (trusted) {
301+
try {
302+
Object value = decodeValue(header, type);
303+
headers.put(header.key(), value);
304+
}
305+
catch (IOException e) {
306+
logger.error(e, () ->
307+
"Could not decode json type: " + new String(header.value()) + " for key: "
308+
+ header.key());
309+
headers.put(header.key(), header.value());
310+
}
311+
}
312+
else {
313+
headers.put(header.key(), new NonTrustedHeaderType(header.value(), requestedType));
314+
}
315+
}
316+
}
317+
314318
private Object decodeValue(Header h, Class<?> type) throws IOException, LinkageError {
315319
ObjectMapper headerObjectMapper = getObjectMapper();
316320
Object value = headerObjectMapper.readValue(h.value(), type);

0 commit comments

Comments
 (0)