Fix more Sonar @NonNullApi violations

This commit is contained in:
Gary Russell
2018-10-25 17:45:37 -04:00
parent 2f2f6a8edb
commit 55e20cf82f
9 changed files with 91 additions and 44 deletions

View File

@@ -728,6 +728,7 @@ public class CachingConnectionFactory extends AbstractConnectionFactory
* return null, if there are no open idle, return the first closed idle so it can
* be reopened.
*/
@Nullable
private ChannelCachingConnectionProxy findIdleConnection() {
ChannelCachingConnectionProxy connection = null;
ChannelCachingConnectionProxy lastIdle = this.idleConnections.peekLast();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -78,6 +78,7 @@ public final class SimpleResourceHolder {
* @return a value bound to the current thread (usually the active
* resource object), or <code>null</code> if none
*/
@Nullable
public static Object get(Object key) {
Object value = doGet(key);
if (value != null && logger.isTraceEnabled()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
/**
* Convenient super class for application classes that need RabbitMQ access.
@@ -72,6 +73,7 @@ public class RabbitGatewaySupport implements InitializingBean {
/**
* @return The Rabbit ConnectionFactory used by the gateway.
*/
@Nullable
public final ConnectionFactory getConnectionFactory() {
return (this.rabbitOperations != null ? this.rabbitOperations.getConnectionFactory() : null);
}

View File

@@ -29,6 +29,7 @@ import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.lang.Nullable;
import com.rabbitmq.http.client.Client;
import com.rabbitmq.http.client.domain.BindingInfo;
@@ -230,7 +231,8 @@ public class RabbitManagementTemplate implements org.springframework.amqp.core.A
.collect(Collectors.toList());
}
private Queue convert(QueueInfo qi) {
@Nullable
private Queue convert(@Nullable QueueInfo qi) {
if (qi == null) {
return null;
}
@@ -244,7 +246,8 @@ public class RabbitManagementTemplate implements org.springframework.amqp.core.A
.collect(Collectors.toList());
}
private Exchange convert(ExchangeInfo ei) {
@Nullable
private Exchange convert(@Nullable ExchangeInfo ei) {
if (ei == null) {
return null;
}
@@ -280,7 +283,8 @@ public class RabbitManagementTemplate implements org.springframework.amqp.core.A
.collect(Collectors.toList());
}
private Binding convert(BindingInfo bi) {
@Nullable
private Binding convert(@Nullable BindingInfo bi) {
if (bi == null) {
return null;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.MessagingMessageConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.converter.MessageConversionException;
@@ -126,21 +127,22 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate<String>
@Override
public void convertAndSend(String exchange, String routingKey, Object payload,
Map<String, Object> headers) throws MessagingException {
@Nullable Map<String, Object> headers) throws MessagingException {
convertAndSend(exchange, routingKey, payload, headers, null);
}
@Override
public void convertAndSend(String exchange, String routingKey, Object payload,
MessagePostProcessor postProcessor) throws MessagingException {
@Nullable MessagePostProcessor postProcessor) throws MessagingException {
convertAndSend(exchange, routingKey, payload, null, postProcessor);
}
@Override
public void convertAndSend(String exchange, String routingKey, Object payload,
Map<String, Object> headers, MessagePostProcessor postProcessor) throws MessagingException {
@Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor)
throws MessagingException {
Message<?> message = doConvert(payload, headers, postProcessor);
send(exchange, routingKey, message);
@@ -162,22 +164,23 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate<String>
@Override
public <T> T convertSendAndReceive(String exchange, String routingKey, Object request,
Map<String, Object> headers, Class<T> targetClass) throws MessagingException {
@Nullable Map<String, Object> headers, Class<T> targetClass) throws MessagingException {
return convertSendAndReceive(exchange, routingKey, request, headers, targetClass, null);
}
@Override
public <T> T convertSendAndReceive(String exchange, String routingKey, Object request,
Class<T> targetClass, MessagePostProcessor requestPostProcessor) throws MessagingException {
Class<T> targetClass, @Nullable MessagePostProcessor requestPostProcessor) throws MessagingException {
return convertSendAndReceive(exchange, routingKey, request, null, targetClass, requestPostProcessor);
}
@SuppressWarnings("unchecked")
@Override
public <T> T convertSendAndReceive(String exchange, String routingKey, Object request, Map<String, Object> headers,
Class<T> targetClass, MessagePostProcessor requestPostProcessor) throws MessagingException {
public <T> T convertSendAndReceive(String exchange, String routingKey, Object request,
@Nullable Map<String, Object> headers,
Class<T> targetClass, @Nullable MessagePostProcessor requestPostProcessor) throws MessagingException {
Message<?> requestMessage = doConvert(request, headers, requestPostProcessor);
Message<?> replyMessage = sendAndReceive(exchange, routingKey, requestMessage);

View File

@@ -23,6 +23,7 @@ import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.lang.Nullable;
/**
* Rabbit specific methods for Amqp functionality.
@@ -68,8 +69,8 @@ public interface RabbitOperations extends AmqpTemplate {
* @return the result of the action method.
* @since 2.1
*/
<T> T invoke(OperationsCallback<T> action, com.rabbitmq.client.ConfirmCallback acks,
com.rabbitmq.client.ConfirmCallback nacks);
<T> T invoke(OperationsCallback<T> action, @Nullable com.rabbitmq.client.ConfirmCallback acks,
@Nullable com.rabbitmq.client.ConfirmCallback nacks);
/**
* Delegate to the underlying dedicated channel to wait for confirms. The connection
@@ -336,7 +337,9 @@ public interface RabbitOperations extends AmqpTemplate {
* @throws AmqpException if there is a problem
*/
default <T> T convertSendAndReceiveAsType(String exchange, String routingKey, Object message,
CorrelationData correlationData, ParameterizedTypeReference<T> responseType) throws AmqpException {
@Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType)
throws AmqpException {
return convertSendAndReceiveAsType(exchange, routingKey, message, null, correlationData, responseType);
}
@@ -399,7 +402,7 @@ public interface RabbitOperations extends AmqpTemplate {
* @throws AmqpException if there is a problem
*/
<T> T convertSendAndReceiveAsType(String exchange, String routingKey, Object message,
MessagePostProcessor messagePostProcessor, CorrelationData correlationData,
@Nullable MessagePostProcessor messagePostProcessor, CorrelationData correlationData,
ParameterizedTypeReference<T> responseType) throws AmqpException;
/**

View File

@@ -714,6 +714,7 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
* @since 1.5
*/
@Override
@Nullable
public Collection<String> expectedQueueNames() {
this.isListener = true;
Collection<String> replyQueue = null;
@@ -741,6 +742,7 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
* @return the collection of correlation data for which confirms have
* not been received or null if no such confirms exist.
*/
@Nullable
public Collection<CorrelationData> getUnconfirmed(long age) {
Set<CorrelationData> unconfirmed = new HashSet<>();
long cutoffTime = System.currentTimeMillis() - age;
@@ -935,8 +937,9 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
}
@Override
public void convertAndSend(String exchange, String routingKey, final Object object, CorrelationData correlationData)
throws AmqpException {
public void convertAndSend(String exchange, String routingKey, final Object object,
@Nullable CorrelationData correlationData) throws AmqpException {
send(exchange, routingKey, convertMessageIfNecessary(object), correlationData);
}
@@ -973,7 +976,8 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
@Override
public void convertAndSend(String exchange, String routingKey, final Object message,
final MessagePostProcessor messagePostProcessor, CorrelationData correlationData) throws AmqpException {
final MessagePostProcessor messagePostProcessor,
@Nullable CorrelationData correlationData) throws AmqpException {
Message messageToSend = convertMessageIfNecessary(message);
messageToSend = messagePostProcessor.postProcessMessage(messageToSend, correlationData);
send(exchange, routingKey, messageToSend, correlationData);
@@ -1159,6 +1163,7 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
}, obtainTargetConnectionFactory(this.receiveConnectionFactorySelectorExpression, queueName));
}
@Nullable
private Message receiveForReply(final String queueName, Channel channel) throws Exception {
boolean channelTransacted = isChannelTransacted();
boolean channelLocallyTransacted = isChannelLocallyTransacted(channel);
@@ -1199,6 +1204,7 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
return receiveMessage;
}
@Nullable
private Delivery consumeDelivery(Channel channel, String queueName, long timeoutMillis) throws Exception {
Delivery delivery = null;
RuntimeException exception = null;
@@ -1326,7 +1332,9 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
return sendAndReceive(message, null);
}
public Message sendAndReceive(final Message message, CorrelationData correlationData) throws AmqpException {
public Message sendAndReceive(final Message message, @Nullable CorrelationData correlationData)
throws AmqpException {
return doSendAndReceive(this.exchange, this.routingKey, message, correlationData);
}
@@ -1335,18 +1343,22 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
return sendAndReceive(routingKey, message, null);
}
public Message sendAndReceive(final String routingKey, final Message message, CorrelationData correlationData) throws AmqpException {
public Message sendAndReceive(final String routingKey, final Message message,
@Nullable CorrelationData correlationData) throws AmqpException {
return doSendAndReceive(this.exchange, routingKey, message, correlationData);
}
@Override
public Message sendAndReceive(final String exchange, final String routingKey, final Message message)
throws AmqpException {
return sendAndReceive(exchange, routingKey, message, null);
}
public Message sendAndReceive(final String exchange, final String routingKey, final Message message, CorrelationData correlationData)
throws AmqpException {
public Message sendAndReceive(final String exchange, final String routingKey, final Message message,
@Nullable CorrelationData correlationData) throws AmqpException {
return doSendAndReceive(exchange, routingKey, message, correlationData);
}
@@ -1356,7 +1368,9 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
}
@Override
public Object convertSendAndReceive(final Object message, CorrelationData correlationData) throws AmqpException {
public Object convertSendAndReceive(final Object message, @Nullable CorrelationData correlationData)
throws AmqpException {
return convertSendAndReceive(this.exchange, this.routingKey, message, null, correlationData);
}
@@ -1366,8 +1380,8 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
}
@Override
public Object convertSendAndReceive(final String routingKey, final Object message, CorrelationData correlationData)
throws AmqpException {
public Object convertSendAndReceive(final String routingKey, final Object message,
@Nullable CorrelationData correlationData) throws AmqpException {
return convertSendAndReceive(this.exchange, routingKey, message, null, correlationData);
}
@@ -1415,7 +1429,9 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
@Override
public Object convertSendAndReceive(final String exchange, final String routingKey, final Object message,
final MessagePostProcessor messagePostProcessor, final CorrelationData correlationData) throws AmqpException {
@Nullable final MessagePostProcessor messagePostProcessor,
@Nullable final CorrelationData correlationData) throws AmqpException {
Message replyMessage = convertSendAndReceiveRaw(exchange, routingKey, message, messagePostProcessor,
correlationData);
if (replyMessage == null) {
@@ -1431,8 +1447,9 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
}
@Override
public <T> T convertSendAndReceiveAsType(final Object message, CorrelationData correlationData,
public <T> T convertSendAndReceiveAsType(final Object message, @Nullable CorrelationData correlationData,
ParameterizedTypeReference<T> responseType) throws AmqpException {
return convertSendAndReceiveAsType(this.exchange, this.routingKey, message, null, correlationData,
responseType);
}
@@ -1440,44 +1457,54 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
@Override
public <T> T convertSendAndReceiveAsType(final String routingKey, final Object message,
ParameterizedTypeReference<T> responseType) throws AmqpException {
return convertSendAndReceiveAsType(routingKey, message, (CorrelationData) null, responseType);
}
@Override
public <T> T convertSendAndReceiveAsType(final String routingKey, final Object message,
CorrelationData correlationData, ParameterizedTypeReference<T> responseType) throws AmqpException {
@Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType)
throws AmqpException {
return convertSendAndReceiveAsType(this.exchange, routingKey, message, null, correlationData, responseType);
}
@Override
public <T> T convertSendAndReceiveAsType(final String exchange, final String routingKey, final Object message,
ParameterizedTypeReference<T> responseType) throws AmqpException {
return convertSendAndReceiveAsType(exchange, routingKey, message, (CorrelationData) null, responseType);
}
@Override
public <T> T convertSendAndReceiveAsType(final Object message, final MessagePostProcessor messagePostProcessor,
public <T> T convertSendAndReceiveAsType(final Object message,
@Nullable final MessagePostProcessor messagePostProcessor,
ParameterizedTypeReference<T> responseType) throws AmqpException {
return convertSendAndReceiveAsType(message, messagePostProcessor, null, responseType);
}
@Override
public <T> T convertSendAndReceiveAsType(final Object message, final MessagePostProcessor messagePostProcessor,
CorrelationData correlationData, ParameterizedTypeReference<T> responseType) throws AmqpException {
public <T> T convertSendAndReceiveAsType(final Object message,
@Nullable final MessagePostProcessor messagePostProcessor,
@Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType)
throws AmqpException {
return convertSendAndReceiveAsType(this.exchange, this.routingKey, message, messagePostProcessor,
correlationData, responseType);
}
@Override
public <T> T convertSendAndReceiveAsType(final String routingKey, final Object message,
final MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType)
throws AmqpException {
@Nullable final MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType)
throws AmqpException {
return convertSendAndReceiveAsType(routingKey, message, messagePostProcessor, null, responseType);
}
@Override
public <T> T convertSendAndReceiveAsType(final String routingKey, final Object message,
final MessagePostProcessor messagePostProcessor, CorrelationData correlationData,
@Nullable final MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData,
ParameterizedTypeReference<T> responseType) throws AmqpException {
return convertSendAndReceiveAsType(this.exchange, routingKey, message, messagePostProcessor, correlationData,
responseType);
@@ -1493,8 +1520,9 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
@Override
@SuppressWarnings("unchecked")
public <T> T convertSendAndReceiveAsType(final String exchange, final String routingKey, final Object message,
final MessagePostProcessor messagePostProcessor, final CorrelationData correlationData,
@Nullable final MessagePostProcessor messagePostProcessor, @Nullable final CorrelationData correlationData,
ParameterizedTypeReference<T> responseType) throws AmqpException {
Message replyMessage = convertSendAndReceiveRaw(exchange, routingKey, message, messagePostProcessor,
correlationData);
if (replyMessage == null) {
@@ -1516,7 +1544,9 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
* @since 1.6.6
*/
protected Message convertSendAndReceiveRaw(final String exchange, final String routingKey, final Object message,
final MessagePostProcessor messagePostProcessor, final CorrelationData correlationData) {
@Nullable final MessagePostProcessor messagePostProcessor,
@Nullable final CorrelationData correlationData) {
Message requestMessage = convertMessageIfNecessary(message);
if (messagePostProcessor != null) {
requestMessage = messagePostProcessor.postProcessMessage(requestMessage, correlationData);
@@ -1999,7 +2029,7 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
* @throws Exception If thrown by RabbitMQ API methods
*/
public void doSend(Channel channel, String exchange, String routingKey, Message message,
boolean mandatory, CorrelationData correlationData) throws Exception {
boolean mandatory, @Nullable CorrelationData correlationData) throws Exception {
if (exchange == null) {
// try to send to configured exchange
exchange = this.exchange;
@@ -2045,7 +2075,7 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
channel.basicPublish(exchange, routingKey, mandatory, convertedMessageProperties, message.getBody());
}
private void setupConfirm(Channel channel, Message message, CorrelationData correlationData) {
private void setupConfirm(Channel channel, Message message, @Nullable CorrelationData correlationData) {
if ((this.publisherConfirms || this.confirmCallback != null) && channel instanceof PublisherCallbackChannel) {
PublisherCallbackChannel publisherCallbackChannel = (PublisherCallbackChannel) channel;
correlationData = this.correlationDataPostProcessor != null
@@ -2359,7 +2389,7 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
return this.savedCorrelation;
}
public void setSavedCorrelation(String savedCorrelation) {
public void setSavedCorrelation(@Nullable String savedCorrelation) {
this.savedCorrelation = savedCorrelation;
}
@@ -2372,6 +2402,7 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
}
}
@Nullable
public Message get(long timeout, TimeUnit unit) throws InterruptedException {
try {
return this.future.get(timeout, unit);

View File

@@ -24,6 +24,7 @@ import org.springframework.amqp.core.Address;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import com.rabbitmq.client.Channel;
@@ -201,7 +202,7 @@ public class DirectReplyToMessageListenerContainer extends DirectMessageListener
* @param cancelConsumer true to cancel the consumer.
* @param message a message to be included in the cancel event if cancelConsumer is true.
*/
public void releaseConsumerFor(ChannelHolder channelHolder, boolean cancelConsumer, String message) {
public void releaseConsumerFor(ChannelHolder channelHolder, boolean cancelConsumer, @Nullable String message) {
synchronized (this.consumersMonitor) {
SimpleConsumer consumer = this.inUseConsumerChannels.get(channelHolder.getChannel());
if (consumer != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.amqp.rabbit.support;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.lang.Nullable;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Envelope;
@@ -30,7 +31,7 @@ import com.rabbitmq.client.Envelope;
*/
public interface MessagePropertiesConverter {
MessageProperties toMessageProperties(BasicProperties source, Envelope envelope, String charset);
MessageProperties toMessageProperties(BasicProperties source, @Nullable Envelope envelope, String charset);
BasicProperties fromMessageProperties(MessageProperties source, String charset);