Skip to content

Commit 43f8d9e

Browse files
committed
Apply 'instanceof pattern matching'
1 parent 7816c9e commit 43f8d9e

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -527,8 +527,8 @@ private SchedulerFactory prepareSchedulerFactory() throws SchedulerException, IO
527527
if (schedulerFactory == null) {
528528
// Create local SchedulerFactory instance (typically a StdSchedulerFactory)
529529
schedulerFactory = BeanUtils.instantiateClass(this.schedulerFactoryClass);
530-
if (schedulerFactory instanceof StdSchedulerFactory) {
531-
initSchedulerFactory((StdSchedulerFactory) schedulerFactory);
530+
if (schedulerFactory instanceof StdSchedulerFactory stdSchedulerFactory) {
531+
initSchedulerFactory(stdSchedulerFactory);
532532
}
533533
else if (this.configLocation != null || this.quartzProperties != null ||
534534
this.taskExecutor != null || this.dataSource != null) {
@@ -622,11 +622,11 @@ private Scheduler prepareScheduler(SchedulerFactory schedulerFactory) throws Sch
622622
this.jobFactory = new AdaptableJobFactory();
623623
}
624624
if (this.jobFactory != null) {
625-
if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware) {
626-
((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext);
625+
if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware applicationContextAware) {
626+
applicationContextAware.setApplicationContext(this.applicationContext);
627627
}
628-
if (this.jobFactory instanceof SchedulerContextAware) {
629-
((SchedulerContextAware) this.jobFactory).setSchedulerContext(scheduler.getContext());
628+
if (this.jobFactory instanceof SchedulerContextAware schedulerContextAware) {
629+
schedulerContextAware.setSchedulerContext(scheduler.getContext());
630630
}
631631
scheduler.setJobFactory(this.jobFactory);
632632
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,8 +60,8 @@ static <T> T instantiateClass(Class<?> clazz, Class<T> assignableTo, Environment
6060
if (clazz.isInterface()) {
6161
throw new BeanInstantiationException(clazz, "Specified class is an interface");
6262
}
63-
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
64-
((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
63+
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory cbf ?
64+
cbf.getBeanClassLoader() : resourceLoader.getClassLoader());
6565
T instance = (T) createInstance(clazz, environment, resourceLoader, registry, classLoader);
6666
ParserStrategyUtils.invokeAwareMethods(instance, environment, resourceLoader, registry, classLoader);
6767
return instance;
@@ -122,17 +122,18 @@ private static void invokeAwareMethods(Object parserStrategyBean, Environment en
122122
ResourceLoader resourceLoader, BeanDefinitionRegistry registry, @Nullable ClassLoader classLoader) {
123123

124124
if (parserStrategyBean instanceof Aware) {
125-
if (parserStrategyBean instanceof BeanClassLoaderAware && classLoader != null) {
126-
((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
125+
if (parserStrategyBean instanceof BeanClassLoaderAware beanClassLoaderAware && classLoader != null) {
126+
beanClassLoaderAware.setBeanClassLoader(classLoader);
127127
}
128-
if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
129-
((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
128+
if (parserStrategyBean instanceof BeanFactoryAware beanFactoryAware &&
129+
registry instanceof BeanFactory beanFactory) {
130+
beanFactoryAware.setBeanFactory(beanFactory);
130131
}
131-
if (parserStrategyBean instanceof EnvironmentAware) {
132-
((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
132+
if (parserStrategyBean instanceof EnvironmentAware environmentAware) {
133+
environmentAware.setEnvironment(environment);
133134
}
134-
if (parserStrategyBean instanceof ResourceLoaderAware) {
135-
((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
135+
if (parserStrategyBean instanceof ResourceLoaderAware resourceLoaderAware) {
136+
resourceLoaderAware.setResourceLoader(resourceLoader);
136137
}
137138
}
138139
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Par
9999
@Override
100100
public void addFormatterForFieldAnnotation(AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory) {
101101
Class<? extends Annotation> annotationType = getAnnotationType(annotationFormatterFactory);
102-
if (this.embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware) {
103-
((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver);
102+
if (this.embeddedValueResolver != null &&
103+
annotationFormatterFactory instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
104+
embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
104105
}
105106
Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes();
106107
for (Class<?> fieldType : fieldTypes) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ private class StubBeanFactory extends StaticListableBeanFactory implements Autow
410410

411411
@Override
412412
public Object initializeBean(Object existingBean, String beanName) throws BeansException {
413-
if (existingBean instanceof ApplicationContextAware) {
414-
((ApplicationContextAware) existingBean).setApplicationContext(StubWebApplicationContext.this);
413+
if (existingBean instanceof ApplicationContextAware applicationContextAware) {
414+
applicationContextAware.setApplicationContext(StubWebApplicationContext.this);
415415
}
416416
return existingBean;
417417
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -238,8 +238,8 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
238238

239239
@Override
240240
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
241-
if (bean instanceof BeanNameAware) {
242-
((BeanNameAware) bean).setBeanName(null);
241+
if (bean instanceof BeanNameAware beanNameAware) {
242+
beanNameAware.setBeanName(null);
243243
}
244244
}
245245

0 commit comments

Comments
 (0)