Use pattern matching for instanceof where appropriate

Closes gh-28181
This commit is contained in:
dreis2211 2022-06-20 19:20:03 +02:00 committed by Andy Wilkinson
parent b17eb9d6b5
commit f2101684b7
9 changed files with 18 additions and 19 deletions

View File

@ -186,11 +186,10 @@ public class DevToolsDataSourceAutoConfiguration {
return ConditionOutcome.noMatch(message.didNotFind("a single DataSourceProperties bean").atAll());
}
BeanDefinition dataSourceDefinition = context.getRegistry().getBeanDefinition(dataSourceBeanNames[0]);
if (dataSourceDefinition instanceof AnnotatedBeanDefinition
&& ((AnnotatedBeanDefinition) dataSourceDefinition).getFactoryMethodMetadata() != null
&& ((AnnotatedBeanDefinition) dataSourceDefinition).getFactoryMethodMetadata()
.getDeclaringClassName().startsWith(DataSourceAutoConfiguration.class.getPackage().getName()
+ ".DataSourceConfiguration$")) {
if (dataSourceDefinition instanceof AnnotatedBeanDefinition annotatedBeanDefinition
&& annotatedBeanDefinition.getFactoryMethodMetadata() != null
&& annotatedBeanDefinition.getFactoryMethodMetadata().getDeclaringClassName().startsWith(
DataSourceAutoConfiguration.class.getPackage().getName() + ".DataSourceConfiguration$")) {
return ConditionOutcome.match(message.foundExactly("auto-configured DataSource"));
}
return ConditionOutcome.noMatch(message.didNotFind("an auto-configured DataSource").atAll());

View File

@ -118,8 +118,8 @@ public class DevToolsR2dbcAutoConfiguration {
return ConditionOutcome.noMatch(message.didNotFind("a single ConnectionFactory bean").atAll());
}
BeanDefinition beanDefinition = context.getRegistry().getBeanDefinition(beanNames[0]);
if (beanDefinition instanceof AnnotatedBeanDefinition
&& isAutoConfigured((AnnotatedBeanDefinition) beanDefinition)) {
if (beanDefinition instanceof AnnotatedBeanDefinition annotatedBeanDefinition
&& isAutoConfigured(annotatedBeanDefinition)) {
return ConditionOutcome.match(message.foundExactly("auto-configured ConnectionFactory"));
}
return ConditionOutcome.noMatch(message.didNotFind("an auto-configured ConnectionFactory").atAll());

View File

@ -191,8 +191,8 @@ public class LocalDevToolsAutoConfiguration {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent || (event instanceof ClassPathChangedEvent
&& !((ClassPathChangedEvent) event).isRestartRequired())) {
if (event instanceof ContextRefreshedEvent || (event instanceof ClassPathChangedEvent classPathChangedEvent
&& !classPathChangedEvent.isRestartRequired())) {
this.liveReloadServer.triggerReload();
}
}

View File

@ -71,8 +71,8 @@ final class ConfigurationPropertiesBeanRegistrar {
}
private boolean containsBeanDefinition(BeanFactory beanFactory, String name) {
if (beanFactory instanceof ListableBeanFactory
&& ((ListableBeanFactory) beanFactory).containsBeanDefinition(name)) {
if (beanFactory instanceof ListableBeanFactory listableBeanFactory
&& listableBeanFactory.containsBeanDefinition(name)) {
return true;
}
if (beanFactory instanceof HierarchicalBeanFactory hierarchicalBeanFactory) {

View File

@ -89,7 +89,7 @@ abstract class IndexedElementsBinder<T> extends AggregateBinder<T> {
private void bindValue(Bindable<?> target, Collection<Object> collection, ResolvableType aggregateType,
ResolvableType elementType, Object value) {
if (value == null || value instanceof CharSequence && ((CharSequence) value).length() == 0) {
if (value == null || (value instanceof CharSequence charSequence && charSequence.isEmpty())) {
return;
}
Object aggregate = convert(value, aggregateType, target.getAnnotations());

View File

@ -97,7 +97,7 @@ class OriginTrackedYamlLoader extends YamlProcessor {
@Override
public Object getData() throws NoSuchElementException {
Object data = super.getData();
if (data instanceof CharSequence && ((CharSequence) data).length() == 0) {
if (data instanceof CharSequence charSequence && charSequence.isEmpty()) {
return null;
}
return data;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 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.
@ -213,8 +213,8 @@ public class SpringApplicationJsonEnvironmentPostProcessor implements Environmen
static JsonPropertyValue get(PropertySource<?> propertySource) {
for (String candidate : CANDIDATES) {
Object value = propertySource.getProperty(candidate);
if (value instanceof String && StringUtils.hasLength((String) value)) {
return new JsonPropertyValue(propertySource, candidate, (String) value);
if (value instanceof String string && StringUtils.hasLength(string)) {
return new JsonPropertyValue(propertySource, candidate, string);
}
}
return null;

View File

@ -81,8 +81,8 @@ class ServletComponentRegisteringPostProcessor implements BeanFactoryPostProcess
}
private boolean isRunningInEmbeddedWebServer() {
return this.applicationContext instanceof WebApplicationContext
&& ((WebApplicationContext) this.applicationContext).getServletContext() == null;
return this.applicationContext instanceof WebApplicationContext webApplicationContext
&& webApplicationContext.getServletContext() == null;
}
private ClassPathScanningCandidateComponentProvider createComponentProvider() {

View File

@ -107,7 +107,7 @@ class StaticResourceJars {
}
private void addUrlConnection(List<URL> urls, URL url, URLConnection connection) {
if (connection instanceof JarURLConnection && isResourcesJar((JarURLConnection) connection)) {
if (connection instanceof JarURLConnection jarURLConnection && isResourcesJar(jarURLConnection)) {
urls.add(url);
}
}