diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java index 15185bba6c..82015a5754 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java @@ -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 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 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); } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ParserStrategyUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/ParserStrategyUtils.java index 0aa2965b74..8decf18cfa 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ParserStrategyUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ParserStrategyUtils.java @@ -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 { 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 { 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); } } } diff --git a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java index 9f502e1f9e..8034c3a183 100644 --- a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java +++ b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java @@ -99,8 +99,9 @@ public class FormattingConversionService extends GenericConversionService @Override public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotationFormatterFactory) { Class 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> fieldTypes = annotationFormatterFactory.getFieldTypes(); for (Class fieldType : fieldTypes) { diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java index 1b5492bd84..9bbd3eed92 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java @@ -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; } diff --git a/spring-web/src/test/java/org/springframework/web/context/request/SessionScopeTests.java b/spring-web/src/test/java/org/springframework/web/context/request/SessionScopeTests.java index 16121ffc78..4bf119959d 100644 --- a/spring-web/src/test/java/org/springframework/web/context/request/SessionScopeTests.java +++ b/spring-web/src/test/java/org/springframework/web/context/request/SessionScopeTests.java @@ -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 { @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); } }