Skip to content

Commit 24521d8

Browse files
garyrussellartembilan
authored andcommitted
Sonar Fixes
- remaining majors
1 parent 1134375 commit 24521d8

File tree

19 files changed

+175
-136
lines changed

19 files changed

+175
-136
lines changed

spring-amqp/src/main/java/org/springframework/amqp/remoting/client/AmqpProxyFactoryBean.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -36,6 +36,8 @@
3636
* This can be received and answered by an {@link AmqpInvokerServiceExporter}.
3737
*
3838
* @author David Bilge
39+
* @author Gary Russell
40+
*
3941
* @since 1.2
4042
* @see #setServiceInterface
4143
* @see AmqpClientInterceptor
@@ -56,7 +58,7 @@ public void afterPropertiesSet() {
5658
}
5759

5860
@Override
59-
public Object getObject() throws Exception {
61+
public Object getObject() {
6062
return this.serviceProxy;
6163
}
6264

spring-amqp/src/main/java/org/springframework/amqp/support/SimpleAmqpHeaderMapper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -50,8 +50,8 @@
5050
*/
5151
public class SimpleAmqpHeaderMapper extends AbstractHeaderMapper<MessageProperties> implements AmqpHeaderMapper {
5252

53-
@Override
54-
public void fromHeaders(MessageHeaders headers, MessageProperties amqpMessageProperties) {
53+
@Override // NOSONAR complexity - mostly null/empty tests
54+
public void fromHeaders(MessageHeaders headers, MessageProperties amqpMessageProperties) { // NOSONAR NCSS lines
5555
String appId = getHeaderIfAvailable(headers, AmqpHeaders.APP_ID, String.class);
5656
if (StringUtils.hasText(appId)) {
5757
amqpMessageProperties.setAppId(appId);
@@ -158,8 +158,8 @@ public void fromHeaders(MessageHeaders headers, MessageProperties amqpMessagePro
158158
}
159159
}
160160

161-
@Override
162-
public MessageHeaders toHeaders(MessageProperties amqpMessageProperties) {
161+
@Override // NOSONAR complexity - mostly null/empty tests
162+
public MessageHeaders toHeaders(MessageProperties amqpMessageProperties) { // NOSONAR NCSS lines
163163
Map<String, Object> headers = new HashMap<String, Object>();
164164
try {
165165
String appId = amqpMessageProperties.getAppId();

spring-amqp/src/main/java/org/springframework/amqp/support/converter/AbstractJackson2MessageConverter.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,24 @@
5151
public abstract class AbstractJackson2MessageConverter extends AbstractMessageConverter
5252
implements BeanClassLoaderAware, SmartMessageConverter {
5353

54-
protected final Log log = LogFactory.getLog(getClass());
54+
protected final Log log = LogFactory.getLog(getClass()); // NOSONAR protected
5555

5656
public static final String DEFAULT_CHARSET = "UTF-8";
5757

5858
private volatile String defaultCharset = DEFAULT_CHARSET;
5959

60-
@Nullable
61-
private ClassMapper classMapper = null;
60+
/**
61+
* The supported content type; only the subtype is checked, e.g. *&#47;json,
62+
* *&#47;xml.
63+
*/
64+
private final MimeType supportedContentType;
6265

63-
protected boolean typeMapperSet;
66+
protected final ObjectMapper objectMapper; // NOSONAR protected
6467

65-
private final MimeType contentType;
68+
@Nullable
69+
private ClassMapper classMapper = null;
6670

67-
protected final ObjectMapper objectMapper;
71+
protected boolean typeMapperSet; // NOSONAR - TODO private in 2.2
6872

6973
private ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
7074

@@ -73,7 +77,8 @@ public abstract class AbstractJackson2MessageConverter extends AbstractMessageCo
7377
/**
7478
* Construct with the provided {@link ObjectMapper} instance.
7579
* @param objectMapper the {@link ObjectMapper} to use.
76-
* @param contentType content type of the message
80+
* @param contentType supported content type when decoding messages, only the subtype
81+
* is checked, e.g. *&#47;json, *&#47;xml.
7782
* @param trustedPackages the trusted Java packages for deserialization
7883
* @see DefaultJackson2JavaTypeMapper#setTrustedPackages(String...)
7984
*/
@@ -83,14 +88,13 @@ protected AbstractJackson2MessageConverter(ObjectMapper objectMapper, MimeType c
8388
Assert.notNull(objectMapper, "'objectMapper' must not be null");
8489
Assert.notNull(contentType, "'contentType' must not be null");
8590
this.objectMapper = objectMapper;
86-
this.contentType = contentType;
91+
this.supportedContentType = contentType;
8792
((DefaultJackson2JavaTypeMapper) this.javaTypeMapper).setTrustedPackages(trustedPackages);
8893
}
8994

9095
@Nullable
9196
public ClassMapper getClassMapper() {
9297
return this.classMapper;
93-
9498
}
9599

96100
public void setClassMapper(ClassMapper classMapper) {
@@ -185,7 +189,7 @@ public Object fromMessage(Message message, @Nullable Object conversionHint) thro
185189
MessageProperties properties = message.getMessageProperties();
186190
if (properties != null) {
187191
String contentType = properties.getContentType();
188-
if (contentType != null && contentType.contains(this.contentType.getSubtype())) {
192+
if (contentType != null && contentType.contains(this.supportedContentType.getSubtype())) {
189193
String encoding = properties.getContentEncoding();
190194
if (encoding == null) {
191195
encoding = getDefaultCharset();
@@ -217,7 +221,7 @@ else if (getClassMapper() == null) {
217221
else {
218222
if (this.log.isWarnEnabled()) {
219223
this.log.warn("Could not convert incoming message with content-type ["
220-
+ contentType + "], '" + this.contentType.getSubtype() + "' keyword missing.");
224+
+ contentType + "], '" + this.supportedContentType.getSubtype() + "' keyword missing.");
221225
}
222226
}
223227
}
@@ -258,7 +262,7 @@ protected Message createMessage(Object objectToConvert, MessageProperties messag
258262
catch (IOException e) {
259263
throw new MessageConversionException("Failed to convert Message content", e);
260264
}
261-
messageProperties.setContentType(this.contentType.toString());
265+
messageProperties.setContentType(this.supportedContentType.toString());
262266
messageProperties.setContentEncoding(getDefaultCharset());
263267
messageProperties.setContentLength(bytes.length);
264268

spring-amqp/src/main/java/org/springframework/amqp/support/converter/AbstractMessageConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ public final Message toMessage(Object object, MessageProperties messagePropertie
5959
}
6060

6161
@Override
62-
public final Message toMessage(Object object, @Nullable MessageProperties messageProperties, @Nullable Type genericType)
62+
public final Message toMessage(Object object, @Nullable MessageProperties messagePropertiesArg,
63+
@Nullable Type genericType)
6364
throws MessageConversionException {
6465

66+
MessageProperties messageProperties = messagePropertiesArg;
6567
if (messageProperties == null) {
6668
messageProperties = new MessageProperties();
6769
}

spring-amqp/src/main/java/org/springframework/amqp/support/converter/DefaultClassMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public class DefaultClassMapper implements ClassMapper, InitializingBean {
6464

6565
private volatile Map<Class<?>, String> classIdMapping = new HashMap<>();
6666

67-
private volatile Class<?> defaultMapClass = LinkedHashMap.class;
67+
private volatile Class<?> defaultMapClass = LinkedHashMap.class; // NOSONAR concrete type
6868

69-
private volatile Class<?> defaultType = LinkedHashMap.class;
69+
private volatile Class<?> defaultType = LinkedHashMap.class; // NOSONAR concrete type
7070

7171
/**
7272
* The type returned by {@link #toClass(MessageProperties)} if no type information

spring-amqp/src/main/java/org/springframework/amqp/support/converter/DefaultJackson2JavaTypeMapper.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,7 @@ public JavaType toJavaType(MessageProperties properties) {
125125
String typeIdHeader = retrieveHeaderAsString(properties, getClassIdFieldName());
126126

127127
if (typeIdHeader != null) {
128-
129-
JavaType classType = getClassIdType(typeIdHeader);
130-
if (!classType.isContainerType() || classType.isArrayType()) {
131-
return classType;
132-
}
133-
134-
JavaType contentClassType = getClassIdType(retrieveHeader(properties, getContentClassIdFieldName()));
135-
if (classType.getKeyType() == null) {
136-
return TypeFactory.defaultInstance()
137-
.constructCollectionLikeType(classType.getRawClass(), contentClassType);
138-
}
139-
140-
JavaType keyClassType = getClassIdType(retrieveHeader(properties, getKeyClassIdFieldName()));
141-
return TypeFactory.defaultInstance()
142-
.constructMapLikeType(classType.getRawClass(), keyClassType, contentClassType);
128+
return fromTypeHeader(properties, typeIdHeader);
143129
}
144130

145131
if (hasInferredTypeHeader) {
@@ -149,6 +135,23 @@ public JavaType toJavaType(MessageProperties properties) {
149135
return TypeFactory.defaultInstance().constructType(Object.class);
150136
}
151137

138+
private JavaType fromTypeHeader(MessageProperties properties, String typeIdHeader) {
139+
JavaType classType = getClassIdType(typeIdHeader);
140+
if (!classType.isContainerType() || classType.isArrayType()) {
141+
return classType;
142+
}
143+
144+
JavaType contentClassType = getClassIdType(retrieveHeader(properties, getContentClassIdFieldName()));
145+
if (classType.getKeyType() == null) {
146+
return TypeFactory.defaultInstance()
147+
.constructCollectionLikeType(classType.getRawClass(), contentClassType);
148+
}
149+
150+
JavaType keyClassType = getClassIdType(retrieveHeader(properties, getKeyClassIdFieldName()));
151+
return TypeFactory.defaultInstance()
152+
.constructMapLikeType(classType.getRawClass(), keyClassType, contentClassType);
153+
}
154+
152155
private JavaType getClassIdType(String classId) {
153156
if (getIdClassMapping().containsKey(classId)) {
154157
return TypeFactory.defaultInstance().constructType(getIdClassMapping().get(classId));

spring-amqp/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -125,31 +125,11 @@ public Object fromMessage(Message message) throws MessageConversionException {
125125
if (properties != null) {
126126
String contentType = properties.getContentType();
127127
if (contentType != null && contentType.startsWith("text") && !this.ignoreContentType) {
128-
String encoding = properties.getContentEncoding();
129-
if (encoding == null) {
130-
encoding = this.defaultCharset;
131-
}
132-
try {
133-
content = new String(message.getBody(), encoding);
134-
}
135-
catch (UnsupportedEncodingException e) {
136-
throw new MessageConversionException("failed to convert text-based Message content", e);
137-
}
128+
content = asString(message, properties);
138129
}
139130
else if (contentType != null && contentType.equals(MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT)
140131
|| this.ignoreContentType) {
141-
try {
142-
ByteArrayInputStream inputStream = new ByteArrayInputStream(message.getBody());
143-
if (this.usingDefaultDeserializer) {
144-
content = deserialize(inputStream);
145-
}
146-
else {
147-
content = this.deserializer.deserialize(inputStream);
148-
}
149-
}
150-
catch (IOException e) {
151-
throw new MessageConversionException("Could not convert message body", e);
152-
}
132+
content = deserialize(message);
153133
}
154134
}
155135
if (content == null) {
@@ -158,6 +138,34 @@ else if (contentType != null && contentType.equals(MessageProperties.CONTENT_TYP
158138
return content;
159139
}
160140

141+
private Object deserialize(Message message) {
142+
try {
143+
ByteArrayInputStream inputStream = new ByteArrayInputStream(message.getBody());
144+
if (this.usingDefaultDeserializer) {
145+
return deserialize(inputStream);
146+
}
147+
else {
148+
return this.deserializer.deserialize(inputStream);
149+
}
150+
}
151+
catch (IOException e) {
152+
throw new MessageConversionException("Could not convert message body", e);
153+
}
154+
}
155+
156+
private Object asString(Message message, MessageProperties properties) {
157+
String encoding = properties.getContentEncoding();
158+
if (encoding == null) {
159+
encoding = this.defaultCharset;
160+
}
161+
try {
162+
return new String(message.getBody(), encoding);
163+
}
164+
catch (UnsupportedEncodingException e) {
165+
throw new MessageConversionException("failed to convert text-based Message content", e);
166+
}
167+
}
168+
161169
private Object deserialize(ByteArrayInputStream inputStream) throws IOException {
162170
try {
163171
ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream,

spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/AbstractDeflaterPostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2017 the original author or authors.
2+
* Copyright 2014-2018 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.
@@ -27,7 +27,7 @@
2727
*/
2828
public abstract class AbstractDeflaterPostProcessor extends AbstractCompressingPostProcessor {
2929

30-
protected int level = Deflater.BEST_SPEED;
30+
protected int level = Deflater.BEST_SPEED; // NOSONAR - TODO: add getter in 2.2
3131

3232
public AbstractDeflaterPostProcessor() {
3333
super();

spring-amqp/src/main/java/org/springframework/amqp/utils/MapBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Map<K, V> get() {
4242
}
4343

4444
@SuppressWarnings("unchecked")
45-
protected final B _this() {
45+
protected final B _this() { // NOSONAR underscore
4646
return (B) this;
4747
}
4848

spring-rabbit-test/src/main/java/org/springframework/amqp/rabbit/test/RabbitListenerTestHarness.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.commons.logging.LogFactory;
2929
import org.mockito.Mockito;
3030

31+
import org.springframework.amqp.AmqpException;
3132
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
3233
import org.springframework.amqp.rabbit.annotation.RabbitListener;
3334
import org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor;
@@ -87,7 +88,7 @@ protected void processListener(MethodRabbitListenerEndpoint endpoint, RabbitList
8788
this.listenerCapture.put(id, advice);
8889
}
8990
catch (Exception e) {
90-
logger.error("Failed to proxy @RabbitListener with id: " + id);
91+
throw new AmqpException("Failed to proxy @RabbitListener with id: " + id, e);
9192
}
9293
}
9394
}

0 commit comments

Comments
 (0)