diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index f11dcce9a33..2ded080cd5d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -599,8 +599,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA */ @Nullable private Object resolvedCachedArgument(@Nullable String beanName, @Nullable Object cachedArgument) { - if (cachedArgument instanceof DependencyDescriptor) { - DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument; + if (cachedArgument instanceof DependencyDescriptor descriptor) { Assert.state(this.beanFactory != null, "No BeanFactory available"); return this.beanFactory.resolveDependency(descriptor, beanName, null, null); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java index a90e5709453..c7f1d24dfa1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java @@ -121,8 +121,7 @@ public class ConstructorArgumentValues { */ private void addOrMergeIndexedArgumentValue(Integer key, ValueHolder newValue) { ValueHolder currentValue = this.indexedArgumentValues.get(key); - if (currentValue != null && newValue.getValue() instanceof Mergeable) { - Mergeable mergeable = (Mergeable) newValue.getValue(); + if (currentValue != null && newValue.getValue() instanceof Mergeable mergeable) { if (mergeable.isMergeEnabled()) { newValue.setValue(mergeable.merge(currentValue.getValue())); } @@ -230,8 +229,7 @@ public class ConstructorArgumentValues { for (Iterator it = this.genericArgumentValues.iterator(); it.hasNext();) { ValueHolder currentValue = it.next(); if (newValue.getName().equals(currentValue.getName())) { - if (newValue.getValue() instanceof Mergeable) { - Mergeable mergeable = (Mergeable) newValue.getValue(); + if (newValue.getValue() instanceof Mergeable mergeable) { if (mergeable.isMergeEnabled()) { newValue.setValue(mergeable.merge(currentValue.getValue())); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/CustomScopeConfigurer.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/CustomScopeConfigurer.java index f292cc9852f..92702225d42 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/CustomScopeConfigurer.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/CustomScopeConfigurer.java @@ -101,8 +101,7 @@ public class CustomScopeConfigurer implements BeanFactoryPostProcessor, BeanClas if (value instanceof Scope) { beanFactory.registerScope(scopeKey, (Scope) value); } - else if (value instanceof Class) { - Class scopeClass = (Class) value; + else if (value instanceof Class scopeClass) { Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class"); beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java index 56dd8580e9f..be6dac24a4b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java @@ -462,8 +462,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp */ private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) { boolean hasClosureArgument = (args[args.length - 1] instanceof Closure); - if (args[0] instanceof Class) { - Class beanClass = (Class) args[0]; + if (args[0] instanceof Class beanClass) { if (hasClosureArgument) { if (args.length - 1 != 1) { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper( diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java b/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java index d1aeff7d0bc..322c41429f2 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java @@ -194,9 +194,8 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport { } } // constructorArgs - else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) { + else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List args) { ConstructorArgumentValues cav = new ConstructorArgumentValues(); - List args = (List) newValue; for (Object arg : args) { cav.addGenericArgumentValue(arg); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 99561a063b1..f84f724174e 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.NotSerializableException; import java.io.ObjectInputStream; import java.io.ObjectStreamException; +import java.io.Serial; import java.io.Serializable; import java.lang.annotation.Annotation; import java.lang.ref.Reference; @@ -921,8 +922,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); - if (bean instanceof FactoryBean) { - FactoryBean factory = (FactoryBean) bean; + if (bean instanceof FactoryBean factory) { boolean isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean) factory).isEagerInit()); if (isEagerInit) { @@ -939,10 +939,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto // Trigger post-initialization callback for all applicable beans... for (String beanName : beanNames) { Object singletonInstance = getSingleton(beanName); - if (singletonInstance instanceof SmartInitializingSingleton) { + if (singletonInstance instanceof SmartInitializingSingleton smartSingleton) { StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize") .tag("beanName", beanName); - SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance; smartSingleton.afterSingletonsInstantiated(); smartInitialize.end(); } @@ -1846,11 +1845,13 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto // Serialization support //--------------------------------------------------------------------- + @Serial private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { throw new NotSerializableException("DefaultListableBeanFactory itself is not deserializable - " + "just a SerializedBeanFactoryReference is"); } + @Serial protected Object writeReplace() throws ObjectStreamException { if (this.serializationId != null) { return new SerializedBeanFactoryReference(this.serializationId); diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomCollectionEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomCollectionEditor.java index bcf39977df2..898adb52ecc 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomCollectionEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomCollectionEditor.java @@ -117,9 +117,8 @@ public class CustomCollectionEditor extends PropertyEditorSupport { // Use the source value as-is, as it matches the target type. super.setValue(value); } - else if (value instanceof Collection) { + else if (value instanceof Collection source) { // Convert Collection elements. - Collection source = (Collection) value; Collection target = createCollection(this.collectionType, source.size()); for (Object elem : source) { target.add(convertElement(elem)); diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomMapEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomMapEditor.java index a1e5ebd119b..d421a8e25c0 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomMapEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomMapEditor.java @@ -107,9 +107,8 @@ public class CustomMapEditor extends PropertyEditorSupport { // Use the source value as-is, as it matches the target type. super.setValue(value); } - else if (value instanceof Map) { + else if (value instanceof Map source) { // Convert Map elements. - Map source = (Map) value; Map target = createMap(this.mapType, source.size()); source.forEach((key, val) -> target.put(convertKey(key), convertValue(val))); super.setValue(target);