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()); return ConditionOutcome.noMatch(message.didNotFind("a single DataSourceProperties bean").atAll());
} }
BeanDefinition dataSourceDefinition = context.getRegistry().getBeanDefinition(dataSourceBeanNames[0]); BeanDefinition dataSourceDefinition = context.getRegistry().getBeanDefinition(dataSourceBeanNames[0]);
if (dataSourceDefinition instanceof AnnotatedBeanDefinition if (dataSourceDefinition instanceof AnnotatedBeanDefinition annotatedBeanDefinition
&& ((AnnotatedBeanDefinition) dataSourceDefinition).getFactoryMethodMetadata() != null && annotatedBeanDefinition.getFactoryMethodMetadata() != null
&& ((AnnotatedBeanDefinition) dataSourceDefinition).getFactoryMethodMetadata() && annotatedBeanDefinition.getFactoryMethodMetadata().getDeclaringClassName().startsWith(
.getDeclaringClassName().startsWith(DataSourceAutoConfiguration.class.getPackage().getName() DataSourceAutoConfiguration.class.getPackage().getName() + ".DataSourceConfiguration$")) {
+ ".DataSourceConfiguration$")) {
return ConditionOutcome.match(message.foundExactly("auto-configured DataSource")); return ConditionOutcome.match(message.foundExactly("auto-configured DataSource"));
} }
return ConditionOutcome.noMatch(message.didNotFind("an auto-configured DataSource").atAll()); 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()); return ConditionOutcome.noMatch(message.didNotFind("a single ConnectionFactory bean").atAll());
} }
BeanDefinition beanDefinition = context.getRegistry().getBeanDefinition(beanNames[0]); BeanDefinition beanDefinition = context.getRegistry().getBeanDefinition(beanNames[0]);
if (beanDefinition instanceof AnnotatedBeanDefinition if (beanDefinition instanceof AnnotatedBeanDefinition annotatedBeanDefinition
&& isAutoConfigured((AnnotatedBeanDefinition) beanDefinition)) { && isAutoConfigured(annotatedBeanDefinition)) {
return ConditionOutcome.match(message.foundExactly("auto-configured ConnectionFactory")); return ConditionOutcome.match(message.foundExactly("auto-configured ConnectionFactory"));
} }
return ConditionOutcome.noMatch(message.didNotFind("an auto-configured ConnectionFactory").atAll()); return ConditionOutcome.noMatch(message.didNotFind("an auto-configured ConnectionFactory").atAll());

View File

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

View File

@ -71,8 +71,8 @@ final class ConfigurationPropertiesBeanRegistrar {
} }
private boolean containsBeanDefinition(BeanFactory beanFactory, String name) { private boolean containsBeanDefinition(BeanFactory beanFactory, String name) {
if (beanFactory instanceof ListableBeanFactory if (beanFactory instanceof ListableBeanFactory listableBeanFactory
&& ((ListableBeanFactory) beanFactory).containsBeanDefinition(name)) { && listableBeanFactory.containsBeanDefinition(name)) {
return true; return true;
} }
if (beanFactory instanceof HierarchicalBeanFactory hierarchicalBeanFactory) { 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, private void bindValue(Bindable<?> target, Collection<Object> collection, ResolvableType aggregateType,
ResolvableType elementType, Object value) { ResolvableType elementType, Object value) {
if (value == null || value instanceof CharSequence && ((CharSequence) value).length() == 0) { if (value == null || (value instanceof CharSequence charSequence && charSequence.isEmpty())) {
return; return;
} }
Object aggregate = convert(value, aggregateType, target.getAnnotations()); Object aggregate = convert(value, aggregateType, target.getAnnotations());

View File

@ -97,7 +97,7 @@ class OriginTrackedYamlLoader extends YamlProcessor {
@Override @Override
public Object getData() throws NoSuchElementException { public Object getData() throws NoSuchElementException {
Object data = super.getData(); Object data = super.getData();
if (data instanceof CharSequence && ((CharSequence) data).length() == 0) { if (data instanceof CharSequence charSequence && charSequence.isEmpty()) {
return null; return null;
} }
return data; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { static JsonPropertyValue get(PropertySource<?> propertySource) {
for (String candidate : CANDIDATES) { for (String candidate : CANDIDATES) {
Object value = propertySource.getProperty(candidate); Object value = propertySource.getProperty(candidate);
if (value instanceof String && StringUtils.hasLength((String) value)) { if (value instanceof String string && StringUtils.hasLength(string)) {
return new JsonPropertyValue(propertySource, candidate, (String) value); return new JsonPropertyValue(propertySource, candidate, string);
} }
} }
return null; return null;

View File

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

View File

@ -107,7 +107,7 @@ class StaticResourceJars {
} }
private void addUrlConnection(List<URL> urls, URL url, URLConnection connection) { 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); urls.add(url);
} }
} }