Skip to content

Commit

Permalink
GH-1514: Pattern-Matched instanceof Where Possible
Browse files Browse the repository at this point in the history
Resolves #1514
  • Loading branch information
garyrussell authored and artembilan committed Oct 11, 2022
1 parent b1ae21c commit 8b4dd86
Show file tree
Hide file tree
Showing 29 changed files with 234 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ public void onMessage(Message message, Channel channel) {
MessageConverter messageConverter = this.template.getMessageConverter();
RabbitConverterFuture<Object> rabbitFuture = (RabbitConverterFuture<Object>) future;
Object converted = rabbitFuture.getReturnType() != null
&& messageConverter instanceof SmartMessageConverter
? ((SmartMessageConverter) messageConverter).fromMessage(message,
&& messageConverter instanceof SmartMessageConverter smart
? smart.fromMessage(message,
rabbitFuture.getReturnType())
: messageConverter.fromMessage(message);
rabbitFuture.complete(converted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ public void setMessageHandlerMethodFactory(MessageHandlerMethodFactory messageHa
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
if (beanFactory instanceof ConfigurableListableBeanFactory) {
this.resolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanExpressionResolver();
this.expressionContext = new BeanExpressionContext((ConfigurableListableBeanFactory) beanFactory, null);
if (beanFactory instanceof ConfigurableListableBeanFactory clbf) {
this.resolver = clbf.getBeanExpressionResolver();
this.expressionContext = new BeanExpressionContext(clbf, null);
}
}

Expand Down Expand Up @@ -265,9 +265,9 @@ MessageHandlerMethodFactory getMessageHandlerMethodFactory() {
public void afterSingletonsInstantiated() {
this.registrar.setBeanFactory(this.beanFactory);

if (this.beanFactory instanceof ListableBeanFactory) {
if (this.beanFactory instanceof ListableBeanFactory lbf) {
Map<String, RabbitListenerConfigurer> instances =
((ListableBeanFactory) this.beanFactory).getBeansOfType(RabbitListenerConfigurer.class);
lbf.getBeansOfType(RabbitListenerConfigurer.class);
for (RabbitListenerConfigurer configurer : instances.values()) {
configurer.configureRabbitListeners(this.registrar);
}
Expand Down Expand Up @@ -445,8 +445,8 @@ protected Collection<Declarable> processListener(MethodRabbitListenerEndpoint en
String group = rabbitListener.group();
if (StringUtils.hasText(group)) {
Object resolvedGroup = resolveExpression(group);
if (resolvedGroup instanceof String) {
endpoint.setGroup((String) resolvedGroup);
if (resolvedGroup instanceof String str) {
endpoint.setGroup(str);
}
}
String autoStartup = rabbitListener.autoStartup();
Expand Down Expand Up @@ -483,8 +483,8 @@ protected Collection<Declarable> processListener(MethodRabbitListenerEndpoint en

private void resolveErrorHandler(MethodRabbitListenerEndpoint endpoint, RabbitListener rabbitListener) {
Object errorHandler = resolveExpression(rabbitListener.errorHandler());
if (errorHandler instanceof RabbitListenerErrorHandler) {
endpoint.setErrorHandler((RabbitListenerErrorHandler) errorHandler);
if (errorHandler instanceof RabbitListenerErrorHandler rleh) {
endpoint.setErrorHandler(rleh);
}
else {
String errorHandlerBeanName = resolveExpressionAsString(rabbitListener.errorHandler(), "errorHandler");
Expand All @@ -499,11 +499,11 @@ private void resolveAckMode(MethodRabbitListenerEndpoint endpoint, RabbitListene
String ackModeAttr = rabbitListener.ackMode();
if (StringUtils.hasText(ackModeAttr)) {
Object ackMode = resolveExpression(ackModeAttr);
if (ackMode instanceof String) {
endpoint.setAckMode(AcknowledgeMode.valueOf((String) ackMode));
if (ackMode instanceof String str) {
endpoint.setAckMode(AcknowledgeMode.valueOf(str));
}
else if (ackMode instanceof AcknowledgeMode) {
endpoint.setAckMode((AcknowledgeMode) ackMode);
else if (ackMode instanceof AcknowledgeMode mode) {
endpoint.setAckMode(mode);
}
else {
Assert.isNull(ackMode, "ackMode must resolve to a String or AcknowledgeMode");
Expand All @@ -513,8 +513,8 @@ else if (ackMode instanceof AcknowledgeMode) {

private void resolveAdmin(MethodRabbitListenerEndpoint endpoint, RabbitListener rabbitListener, Object adminTarget) {
Object resolved = resolveExpression(rabbitListener.admin());
if (resolved instanceof AmqpAdmin) {
endpoint.setAdmin((AmqpAdmin) resolved);
if (resolved instanceof AmqpAdmin admin) {
endpoint.setAdmin(admin);
}
else {
String rabbitAdmin = resolveExpressionAsString(rabbitListener.admin(), "admin");
Expand All @@ -538,8 +538,8 @@ private RabbitListenerContainerFactory<?> resolveContainerFactory(RabbitListener

RabbitListenerContainerFactory<?> factory = null;
Object resolved = resolveExpression(rabbitListener.containerFactory());
if (resolved instanceof RabbitListenerContainerFactory) {
return (RabbitListenerContainerFactory<?>) resolved;
if (resolved instanceof RabbitListenerContainerFactory<?> rlcf) {
return rlcf;
}
String containerFactoryBeanName = resolveExpressionAsString(rabbitListener.containerFactory(),
"containerFactory");
Expand All @@ -561,8 +561,8 @@ private void resolveExecutor(MethodRabbitListenerEndpoint endpoint, RabbitListen
Object execTarget, String beanName) {

Object resolved = resolveExpression(rabbitListener.executor());
if (resolved instanceof TaskExecutor) {
endpoint.setTaskExecutor((TaskExecutor) resolved);
if (resolved instanceof TaskExecutor tex) {
endpoint.setTaskExecutor(tex);
}
else {
String execBeanName = resolveExpressionAsString(rabbitListener.executor(), "executor");
Expand All @@ -583,8 +583,8 @@ private void resolvePostProcessor(MethodRabbitListenerEndpoint endpoint, RabbitL
Object target, String beanName) {

Object resolved = resolveExpression(rabbitListener.replyPostProcessor());
if (resolved instanceof ReplyPostProcessor) {
endpoint.setReplyPostProcessor((ReplyPostProcessor) resolved);
if (resolved instanceof ReplyPostProcessor rpp) {
endpoint.setReplyPostProcessor(rpp);
}
else {
String ppBeanName = resolveExpressionAsString(rabbitListener.replyPostProcessor(), "replyPostProcessor");
Expand All @@ -605,8 +605,8 @@ private void resolveMessageConverter(MethodRabbitListenerEndpoint endpoint, Rabb
Object target, String beanName) {

Object resolved = resolveExpression(rabbitListener.messageConverter());
if (resolved instanceof MessageConverter) {
endpoint.setMessageConverter((MessageConverter) resolved);
if (resolved instanceof MessageConverter converter) {
endpoint.setMessageConverter(converter);
}
else {
String mcBeanName = resolveExpressionAsString(rabbitListener.messageConverter(), "messageConverter");
Expand Down Expand Up @@ -704,20 +704,20 @@ private void resolveAsStringOrQueue(Object resolvedValue, List<String> names, @N
String what) {

Object resolvedValueToUse = resolvedValue;
if (resolvedValue instanceof String[]) {
resolvedValueToUse = Arrays.asList((String[]) resolvedValue);
if (resolvedValue instanceof String[] strings) {
resolvedValueToUse = Arrays.asList(strings);
}
if (queues != null && resolvedValueToUse instanceof Queue) {
if (queues != null && resolvedValueToUse instanceof Queue q) {
if (!names.isEmpty()) {
// revert to the previous behavior of just using the name when there is mixture of String and Queue
names.add(((Queue) resolvedValueToUse).getName());
names.add(q.getName());
}
else {
queues.add((Queue) resolvedValueToUse);
queues.add(q);
}
}
else if (resolvedValueToUse instanceof String) {
names.add((String) resolvedValueToUse);
else if (resolvedValueToUse instanceof String str) {
names.add(str);
}
else if (resolvedValueToUse instanceof Iterable) {
for (Object object : (Iterable<Object>) resolvedValueToUse) {
Expand Down Expand Up @@ -858,8 +858,8 @@ private Map<String, Object> resolveArguments(Argument[] arguments) {
Object type = resolveExpression(arg.type());
Class<?> typeClass;
String typeName;
if (type instanceof Class) {
typeClass = (Class<?>) type;
if (type instanceof Class<?> clazz) {
typeClass = clazz;
typeName = typeClass.getName();
}
else {
Expand Down Expand Up @@ -924,12 +924,11 @@ private boolean resolveExpressionAsBoolean(String value) {

private boolean resolveExpressionAsBoolean(String value, boolean defaultValue) {
Object resolved = resolveExpression(value);
if (resolved instanceof Boolean) {
return (Boolean) resolved;
if (resolved instanceof Boolean bool) {
return bool;
}
else if (resolved instanceof String) {
final String s = (String) resolved;
return StringUtils.hasText(s) ? Boolean.parseBoolean(s) : defaultValue;
else if (resolved instanceof String str) {
return StringUtils.hasText(str) ? Boolean.parseBoolean(str) : defaultValue;
}
else {
return defaultValue;
Expand All @@ -938,8 +937,8 @@ else if (resolved instanceof String) {

protected String resolveExpressionAsString(String value, String attribute) {
Object resolved = resolveExpression(value);
if (resolved instanceof String) {
return (String) resolved;
if (resolved instanceof String str) {
return str;
}
else {
throw new IllegalStateException("The [" + attribute + "] must resolve to a String. "
Expand All @@ -952,8 +951,8 @@ private String resolveExpressionAsStringOrInteger(String value, String attribute
return null;
}
Object resolved = resolveExpression(value);
if (resolved instanceof String) {
return (String) resolved;
if (resolved instanceof String str) {
return str;
}
else if (resolved instanceof Integer) {
return resolved.toString();
Expand All @@ -977,8 +976,8 @@ private Object resolveExpression(String value) {
* @see ConfigurableBeanFactory#resolveEmbeddedValue
*/
private String resolve(String value) {
if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory) {
return ((ConfigurableBeanFactory) this.beanFactory).resolveEmbeddedValue(value);
if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory cbf) {
return cbf.resolveEmbeddedValue(value);
}
return value;
}
Expand Down Expand Up @@ -1086,8 +1085,8 @@ public Object resolveArgument(MethodParameter parameter, Message<?> message) thr
}

private boolean isOptional(Message<?> message, Type type) {
return (Optional.class.equals(type) || (type instanceof ParameterizedType
&& Optional.class.equals(((ParameterizedType) type).getRawType())))
return (Optional.class.equals(type) || (type instanceof ParameterizedType pType
&& Optional.class.equals(pType.getRawType())))
&& message.getPayload().equals(Optional.empty());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
Expand Down Expand Up @@ -104,11 +104,11 @@ private MethodInvocationRecoverer<?> createRecoverer() {
if (messageRecoverer == null) {
logger.warn("Message(s) dropped on recovery: " + arg, cause);
}
else if (arg instanceof Message) {
messageRecoverer.recover((Message) arg, cause);
else if (arg instanceof Message msg) {
messageRecoverer.recover(msg, cause);
}
else if (arg instanceof List && messageRecoverer instanceof MessageBatchRecoverer) {
((MessageBatchRecoverer) messageRecoverer).recover((List<Message>) arg, cause);
else if (arg instanceof List && messageRecoverer instanceof MessageBatchRecoverer recoverer) {
recoverer.recover((List<Message>) arg, cause);
}
// This is actually a normal outcome. It means the recovery was successful, but we don't want to consume
// any more messages until the acks and commits are sent for this (problematic) message...
Expand Down Expand Up @@ -137,8 +137,8 @@ private MethodArgumentsKeyGenerator createKeyGenerator() {
private Message argToMessage(Object[] args) {
Object arg = args[1];
Message message = null;
if (arg instanceof Message) {
message = (Message) arg;
if (arg instanceof Message msg) {
message = msg;
}
else if (arg instanceof List) {
message = ((List<Message>) arg).get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ protected final Connection createBareConnection() {
com.rabbitmq.client.Connection rabbitConnection = connect(connectionName);

Connection connection = new SimpleConnection(rabbitConnection, this.closeTimeout);
if (rabbitConnection instanceof AutorecoveringConnection) {
((AutorecoveringConnection) rabbitConnection).addRecoveryListener(new RecoveryListener() {
if (rabbitConnection instanceof AutorecoveringConnection auto) {
auto.addRecoveryListener(new RecoveryListener() {

@Override
public void handleRecoveryStarted(Recoverable recoverable) {
Expand All @@ -573,8 +573,8 @@ public void handleRecovery(Recoverable recoverable) {
if (this.logger.isInfoEnabled()) {
this.logger.info("Created new connection: " + connectionName + "/" + connection);
}
if (this.recoveryListener != null && rabbitConnection instanceof AutorecoveringConnection) {
((AutorecoveringConnection) rabbitConnection).addRecoveryListener(this.recoveryListener);
if (this.recoveryListener != null && rabbitConnection instanceof AutorecoveringConnection auto) {
auto.addRecoveryListener(this.recoveryListener);
}

if (this.applicationEventPublisher != null) {
Expand Down Expand Up @@ -717,8 +717,7 @@ private static class DefaultChannelCloseLogger implements ConditionalExceptionLo

@Override
public void log(Log logger, String message, Throwable t) {
if (t instanceof ShutdownSignalException) {
ShutdownSignalException cause = (ShutdownSignalException) t;
if (t instanceof ShutdownSignalException cause) {
if (RabbitUtils.isPassiveDeclarationChannelClose(cause)) {
if (logger.isDebugEnabled()) {
logger.debug(message + ": " + cause.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1291,8 +1291,8 @@ private void physicalClose(Object proxy) throws IOException, TimeoutException {
}
else {
this.target.close();
if (this.target instanceof AutorecoveringChannel) {
ClosingRecoveryListener.removeChannel((AutorecoveringChannel) this.target);
if (this.target instanceof AutorecoveringChannel auto) {
ClosingRecoveryListener.removeChannel(auto);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2022 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.
Expand Down Expand Up @@ -79,14 +79,13 @@ public void handleRecoveryStarted(Recoverable recoverable) {
*/
public static void addRecoveryListenerIfNecessary(Channel channel) {
AutorecoveringChannel autorecoveringChannel = null;
if (channel instanceof ChannelProxy) {
if (((ChannelProxy) channel).getTargetChannel() instanceof AutorecoveringChannel) {
autorecoveringChannel = (AutorecoveringChannel) ((ChannelProxy) channel)
.getTargetChannel();
if (channel instanceof ChannelProxy proxy) {
if (proxy.getTargetChannel() instanceof AutorecoveringChannel auto) {
autorecoveringChannel = auto;
}
}
else if (channel instanceof AutorecoveringChannel) {
autorecoveringChannel = (AutorecoveringChannel) channel;
else if (channel instanceof AutorecoveringChannel auto) {
autorecoveringChannel = auto;
}
if (autorecoveringChannel != null
&& hasListener.putIfAbsent(autorecoveringChannel, Boolean.TRUE) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ protected ConnectionFactory createConnectionFactory(String address, String node)
public void resetConnection() {
Exception lastException = null;
for (ConnectionFactory connectionFactory : this.nodeFactories.values()) {
if (connectionFactory instanceof DisposableBean) {
if (connectionFactory instanceof DisposableBean disposable) {
try {
((DisposableBean) connectionFactory).destroy();
disposable.destroy();
}
catch (Exception e) {
lastException = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ public Connection getConnection() {
@Override
public void close(int closeCode, String closeMessage) throws IOException, TimeoutException {
this.delegate.close(closeCode, closeMessage);
if (this.delegate instanceof AutorecoveringChannel) {
ClosingRecoveryListener.removeChannel((AutorecoveringChannel) this.delegate);
if (this.delegate instanceof AutorecoveringChannel auto) {
ClosingRecoveryListener.removeChannel(auto);
}
}

Expand Down
Loading

0 comments on commit 8b4dd86

Please sign in to comment.