Browse Source

Apply 'instanceof pattern matching'

pull/29557/head
Sam Brannen 2 years ago
parent
commit
43f8d9e084
  1. 14
      spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java
  2. 23
      spring-context/src/main/java/org/springframework/context/annotation/ParserStrategyUtils.java
  3. 5
      spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java
  4. 4
      spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java
  5. 6
      spring-web/src/test/java/org/springframework/web/context/request/SessionScopeTests.java

14
spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 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.
@ -527,8 +527,8 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe @@ -527,8 +527,8 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe
if (schedulerFactory == null) {
// Create local SchedulerFactory instance (typically a StdSchedulerFactory)
schedulerFactory = BeanUtils.instantiateClass(this.schedulerFactoryClass);
if (schedulerFactory instanceof StdSchedulerFactory) {
initSchedulerFactory((StdSchedulerFactory) schedulerFactory);
if (schedulerFactory instanceof StdSchedulerFactory stdSchedulerFactory) {
initSchedulerFactory(stdSchedulerFactory);
}
else if (this.configLocation != null || this.quartzProperties != null ||
this.taskExecutor != null || this.dataSource != null) {
@ -622,11 +622,11 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe @@ -622,11 +622,11 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe
this.jobFactory = new AdaptableJobFactory();
}
if (this.jobFactory != null) {
if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware) {
((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext);
if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware applicationContextAware) {
applicationContextAware.setApplicationContext(this.applicationContext);
}
if (this.jobFactory instanceof SchedulerContextAware) {
((SchedulerContextAware) this.jobFactory).setSchedulerContext(scheduler.getContext());
if (this.jobFactory instanceof SchedulerContextAware schedulerContextAware) {
schedulerContextAware.setSchedulerContext(scheduler.getContext());
}
scheduler.setJobFactory(this.jobFactory);
}

23
spring-context/src/main/java/org/springframework/context/annotation/ParserStrategyUtils.java

@ -1,5 +1,5 @@ @@ -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.
@ -60,8 +60,8 @@ abstract class ParserStrategyUtils { @@ -60,8 +60,8 @@ abstract class ParserStrategyUtils {
if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "Specified class is an interface");
}
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory cbf ?
cbf.getBeanClassLoader() : resourceLoader.getClassLoader());
T instance = (T) createInstance(clazz, environment, resourceLoader, registry, classLoader);
ParserStrategyUtils.invokeAwareMethods(instance, environment, resourceLoader, registry, classLoader);
return instance;
@ -122,17 +122,18 @@ abstract class ParserStrategyUtils { @@ -122,17 +122,18 @@ abstract class ParserStrategyUtils {
ResourceLoader resourceLoader, BeanDefinitionRegistry registry, @Nullable ClassLoader classLoader) {
if (parserStrategyBean instanceof Aware) {
if (parserStrategyBean instanceof BeanClassLoaderAware && classLoader != null) {
((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
if (parserStrategyBean instanceof BeanClassLoaderAware beanClassLoaderAware && classLoader != null) {
beanClassLoaderAware.setBeanClassLoader(classLoader);
}
if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
if (parserStrategyBean instanceof BeanFactoryAware beanFactoryAware &&
registry instanceof BeanFactory beanFactory) {
beanFactoryAware.setBeanFactory(beanFactory);
}
if (parserStrategyBean instanceof EnvironmentAware) {
((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
if (parserStrategyBean instanceof EnvironmentAware environmentAware) {
environmentAware.setEnvironment(environment);
}
if (parserStrategyBean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
if (parserStrategyBean instanceof ResourceLoaderAware resourceLoaderAware) {
resourceLoaderAware.setResourceLoader(resourceLoader);
}
}
}

5
spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java

@ -99,8 +99,9 @@ public class FormattingConversionService extends GenericConversionService @@ -99,8 +99,9 @@ public class FormattingConversionService extends GenericConversionService
@Override
public void addFormatterForFieldAnnotation(AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory) {
Class<? extends Annotation> annotationType = getAnnotationType(annotationFormatterFactory);
if (this.embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver);
if (this.embeddedValueResolver != null &&
annotationFormatterFactory instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
}
Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes();
for (Class<?> fieldType : fieldTypes) {

4
spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

@ -410,8 +410,8 @@ class StubWebApplicationContext implements WebApplicationContext { @@ -410,8 +410,8 @@ class StubWebApplicationContext implements WebApplicationContext {
@Override
public Object initializeBean(Object existingBean, String beanName) throws BeansException {
if (existingBean instanceof ApplicationContextAware) {
((ApplicationContextAware) existingBean).setApplicationContext(StubWebApplicationContext.this);
if (existingBean instanceof ApplicationContextAware applicationContextAware) {
applicationContextAware.setApplicationContext(StubWebApplicationContext.this);
}
return existingBean;
}

6
spring-web/src/test/java/org/springframework/web/context/request/SessionScopeTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 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.
@ -238,8 +238,8 @@ public class SessionScopeTests { @@ -238,8 +238,8 @@ public class SessionScopeTests {
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(null);
if (bean instanceof BeanNameAware beanNameAware) {
beanNameAware.setBeanName(null);
}
}

Loading…
Cancel
Save