Use pattern matching for instanceof where appropriate

See gh-31475
This commit is contained in:
dreis2211
2022-06-20 18:14:16 +02:00
committed by Andy Wilkinson
parent a7b98e7312
commit 5db04da275
278 changed files with 1049 additions and 1126 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -91,8 +91,8 @@ class AbstractDependsOnBeanFactoryPostProcessorTests {
}
catch (NoSuchBeanDefinitionException ex) {
BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
if (parentBeanFactory instanceof ConfigurableListableBeanFactory) {
return getBeanDefinition(beanName, (ConfigurableListableBeanFactory) parentBeanFactory);
if (parentBeanFactory instanceof ConfigurableListableBeanFactory configurableListableBeanFactory) {
return getBeanDefinition(beanName, configurableListableBeanFactory);
}
throw ex;
}

View File

@@ -940,8 +940,8 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof CacheManager) {
this.cacheManagers.add((CacheManager) bean);
if (bean instanceof CacheManager cacheManager) {
this.cacheManagers.add(cacheManager);
return new SimpleCacheManager();
}
return bean;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -246,8 +246,8 @@ class RedisAutoConfigurationJedisTests {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
if (bean instanceof JedisConnectionFactory) {
connectionFactory = (JedisConnectionFactory) bean;
if (bean instanceof JedisConnectionFactory jedisConnectionFactory) {
connectionFactory = jedisConnectionFactory;
}
return bean;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -161,8 +161,8 @@ class HttpMessageConvertersTests {
private AllEncompassingFormHttpMessageConverter findFormConverter(Collection<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof AllEncompassingFormHttpMessageConverter) {
return (AllEncompassingFormHttpMessageConverter) converter;
if (converter instanceof AllEncompassingFormHttpMessageConverter allEncompassingConverter) {
return allEncompassingConverter;
}
}
return null;

View File

@@ -376,8 +376,8 @@ class ArtemisAutoConfigurationTests {
assertThat(transportConfig.getFactoryClassName()).isEqualTo(NettyConnectorFactory.class.getName());
assertThat(transportConfig.getParams().get("host")).isEqualTo(host);
Object transportConfigPort = transportConfig.getParams().get("port");
if (transportConfigPort instanceof String) {
transportConfigPort = Integer.parseInt((String) transportConfigPort);
if (transportConfigPort instanceof String portString) {
transportConfigPort = Integer.parseInt(portString);
}
assertThat(transportConfigPort).isEqualTo(port);
return transportConfig;

View File

@@ -364,8 +364,7 @@ class TomcatWebServerFactoryCustomizerTests {
Valve[] valves = server.getTomcat().getHost().getPipeline().getValves();
assertThat(valves).hasAtLeastOneElementOfType(ErrorReportValve.class);
for (Valve valve : valves) {
if (valve instanceof ErrorReportValve) {
ErrorReportValve errorReportValve = (ErrorReportValve) valve;
if (valve instanceof ErrorReportValve errorReportValve) {
assertThat(errorReportValve.isShowReport()).isFalse();
assertThat(errorReportValve.isShowServerInfo()).isFalse();
}

View File

@@ -249,9 +249,9 @@ class UndertowWebServerFactoryCustomizerTests {
ConfigurableUndertowWebServerFactory factory = mock(ConfigurableUndertowWebServerFactory.class);
willAnswer((invocation) -> {
Object argument = invocation.getArgument(0);
Arrays.stream((argument instanceof UndertowBuilderCustomizer)
? new UndertowBuilderCustomizer[] { (UndertowBuilderCustomizer) argument }
: (UndertowBuilderCustomizer[]) argument).forEach((customizer) -> customizer.customize(builder));
Arrays.stream((argument instanceof UndertowBuilderCustomizer undertowCustomizer)
? new UndertowBuilderCustomizer[] { undertowCustomizer } : (UndertowBuilderCustomizer[]) argument)
.forEach((customizer) -> customizer.customize(builder));
return null;
}).given(factory).addBuilderCustomizers(any());
return factory;

View File

@@ -427,8 +427,8 @@ class WebFluxAutoConfigurationTests {
Map<PathPattern, Object> handlerMap = getHandlerMap(context);
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceWebHandler) {
assertThat(((ResourceWebHandler) handler).getCacheControl()).usingRecursiveComparison()
if (handler instanceof ResourceWebHandler resourceWebHandler) {
assertThat(resourceWebHandler.getCacheControl()).usingRecursiveComparison()
.isEqualTo(CacheControl.maxAge(5, TimeUnit.SECONDS));
}
}
@@ -444,8 +444,8 @@ class WebFluxAutoConfigurationTests {
Map<PathPattern, Object> handlerMap = getHandlerMap(context);
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceWebHandler) {
assertThat(((ResourceWebHandler) handler).getCacheControl()).usingRecursiveComparison()
if (handler instanceof ResourceWebHandler resourceWebHandler) {
assertThat(resourceWebHandler.getCacheControl()).usingRecursiveComparison()
.isEqualTo(CacheControl.maxAge(5, TimeUnit.SECONDS).proxyRevalidate());
}
}
@@ -459,8 +459,8 @@ class WebFluxAutoConfigurationTests {
Map<PathPattern, Object> handlerMap = getHandlerMap(context);
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceWebHandler) {
assertThat(((ResourceWebHandler) handler).isUseLastModified()).isFalse();
if (handler instanceof ResourceWebHandler resourceWebHandler) {
assertThat(resourceWebHandler.isUseLastModified()).isFalse();
}
}
});
@@ -635,8 +635,8 @@ class WebFluxAutoConfigurationTests {
private Map<PathPattern, Object> getHandlerMap(ApplicationContext context) {
HandlerMapping mapping = context.getBean("resourceHandlerMapping", HandlerMapping.class);
if (mapping instanceof SimpleUrlHandlerMapping) {
return ((SimpleUrlHandlerMapping) mapping).getHandlerMap();
if (mapping instanceof SimpleUrlHandlerMapping simpleMapping) {
return simpleMapping.getHandlerMap();
}
return Collections.emptyMap();
}

View File

@@ -1021,16 +1021,16 @@ class WebMvcAutoConfigurationTests {
Map<String, Object> handlerMap = getHandlerMap(context.getBean("resourceHandlerMapping", HandlerMapping.class));
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceHttpRequestHandler) {
handlerConsumer.accept((ResourceHttpRequestHandler) handler);
if (handler instanceof ResourceHttpRequestHandler resourceHandler) {
handlerConsumer.accept(resourceHandler);
}
}
}
protected Map<String, List<Resource>> getResourceMappingLocations(ApplicationContext context) {
Object bean = context.getBean("resourceHandlerMapping");
if (bean instanceof HandlerMapping) {
return getMappingLocations(context, (HandlerMapping) bean);
if (bean instanceof HandlerMapping handlerMapping) {
return getMappingLocations(context, handlerMapping);
}
assertThat(bean.toString()).isEqualTo("null");
return Collections.emptyMap();
@@ -1066,8 +1066,8 @@ class WebMvcAutoConfigurationTests {
}
protected Map<String, Object> getHandlerMap(HandlerMapping mapping) {
if (mapping instanceof SimpleUrlHandlerMapping) {
return ((SimpleUrlHandlerMapping) mapping).getHandlerMap();
if (mapping instanceof SimpleUrlHandlerMapping handlerMapping) {
return handlerMapping.getHandlerMap();
}
return Collections.emptyMap();
}