diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AnnotatedGenericBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AnnotatedGenericBeanDefinition.java
index c0cf85a172f..b8cb9070636 100644
--- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AnnotatedGenericBeanDefinition.java
+++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AnnotatedGenericBeanDefinition.java
@@ -70,8 +70,8 @@ public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implem
*/
public AnnotatedGenericBeanDefinition(AnnotationMetadata metadata) {
Assert.notNull(metadata, "AnnotationMetadata must not be null");
- if (metadata instanceof StandardAnnotationMetadata) {
- setBeanClass(((StandardAnnotationMetadata) metadata).getIntrospectedClass());
+ if (metadata instanceof StandardAnnotationMetadata sam) {
+ setBeanClass(sam.getIntrospectedClass());
}
else {
setBeanClassName(metadata.getClassName());
diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java
index 09bbf79d194..847574dbd2e 100644
--- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java
+++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java
@@ -390,13 +390,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
@Override
@Nullable
public String getBeanClassName() {
- Object beanClassObject = this.beanClass;
- if (beanClassObject instanceof Class) {
- return ((Class>) beanClassObject).getName();
- }
- else {
- return (String) beanClassObject;
- }
+ return (this.beanClass instanceof Class> clazz ? clazz.getName() : (String) this.beanClass);
}
/**
@@ -433,11 +427,11 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
if (beanClassObject == null) {
throw new IllegalStateException("No bean class specified on bean definition");
}
- if (!(beanClassObject instanceof Class)) {
+ if (!(beanClassObject instanceof Class> clazz)) {
throw new IllegalStateException(
"Bean class name [" + beanClassObject + "] has not been resolved into an actual Class");
}
- return (Class>) beanClassObject;
+ return clazz;
}
/**
@@ -1102,8 +1096,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
@Override
@Nullable
public BeanDefinition getOriginatingBeanDefinition() {
- return (this.resource instanceof BeanDefinitionResource ?
- ((BeanDefinitionResource) this.resource).getBeanDefinition() : null);
+ return (this.resource instanceof BeanDefinitionResource bdr ? bdr.getBeanDefinition() : null);
}
/**
diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
index d003326cc32..c99741dcef6 100644
--- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
+++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
@@ -107,6 +107,7 @@ import org.springframework.util.StringValueResolver;
* @author Costin Leau
* @author Chris Beams
* @author Phillip Webb
+ * @author Sam Brannen
* @since 15 April 2001
* @see #getBeanDefinition
* @see #createBean
@@ -270,9 +271,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
String nameToLookup = originalBeanName(name);
- if (parentBeanFactory instanceof AbstractBeanFactory) {
- return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
- nameToLookup, requiredType, args, typeCheckOnly);
+ if (parentBeanFactory instanceof AbstractBeanFactory abf) {
+ return abf.doGetBean(nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
// Delegation to parent with explicit args.
@@ -428,8 +428,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
Object beanInstance = getSingleton(beanName, false);
if (beanInstance != null) {
- if (beanInstance instanceof FactoryBean) {
- return (BeanFactoryUtils.isFactoryDereference(name) || ((FactoryBean>) beanInstance).isSingleton());
+ if (beanInstance instanceof FactoryBean> factoryBean) {
+ return (BeanFactoryUtils.isFactoryDereference(name) || factoryBean.isSingleton());
}
else {
return !BeanFactoryUtils.isFactoryDereference(name);
@@ -486,7 +486,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
}
if (isFactoryBean(beanName, mbd)) {
FactoryBean> fb = (FactoryBean>) getBean(FACTORY_BEAN_PREFIX + beanName);
- return ((fb instanceof SmartFactoryBean && ((SmartFactoryBean>) fb).isPrototype()) ||
+ return ((fb instanceof SmartFactoryBean> smartFactoryBean && smartFactoryBean.isPrototype()) ||
!fb.isSingleton());
}
else {
@@ -522,9 +522,9 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
// Check manually registered singletons.
Object beanInstance = getSingleton(beanName, false);
if (beanInstance != null && beanInstance.getClass() != NullBean.class) {
- if (beanInstance instanceof FactoryBean) {
+ if (beanInstance instanceof FactoryBean> factoryBean) {
if (!isFactoryDereference) {
- Class> type = getTypeForFactoryBean((FactoryBean>) beanInstance);
+ Class> type = getTypeForFactoryBean(factoryBean);
return (type != null && typeToMatch.isAssignableFrom(type));
}
else {
@@ -673,8 +673,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
// Check manually registered singletons.
Object beanInstance = getSingleton(beanName, false);
if (beanInstance != null && beanInstance.getClass() != NullBean.class) {
- if (beanInstance instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
- return getTypeForFactoryBean((FactoryBean>) beanInstance);
+ if (beanInstance instanceof FactoryBean> factoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
+ return getTypeForFactoryBean(factoryBean);
}
else {
return beanInstance.getClass();
@@ -962,26 +962,26 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @since 5.3
*/
BeanPostProcessorCache getBeanPostProcessorCache() {
- BeanPostProcessorCache bpCache = this.beanPostProcessorCache;
- if (bpCache == null) {
- bpCache = new BeanPostProcessorCache();
- for (BeanPostProcessor bp : this.beanPostProcessors) {
- if (bp instanceof InstantiationAwareBeanPostProcessor) {
- bpCache.instantiationAware.add((InstantiationAwareBeanPostProcessor) bp);
- if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
- bpCache.smartInstantiationAware.add((SmartInstantiationAwareBeanPostProcessor) bp);
+ BeanPostProcessorCache bppCache = this.beanPostProcessorCache;
+ if (bppCache == null) {
+ bppCache = new BeanPostProcessorCache();
+ for (BeanPostProcessor bpp : this.beanPostProcessors) {
+ if (bpp instanceof InstantiationAwareBeanPostProcessor instantiationAwareBpp) {
+ bppCache.instantiationAware.add(instantiationAwareBpp);
+ if (bpp instanceof SmartInstantiationAwareBeanPostProcessor smartInstantiationAwareBpp) {
+ bppCache.smartInstantiationAware.add(smartInstantiationAwareBpp);
}
}
- if (bp instanceof DestructionAwareBeanPostProcessor) {
- bpCache.destructionAware.add((DestructionAwareBeanPostProcessor) bp);
+ if (bpp instanceof DestructionAwareBeanPostProcessor destructionAwareBpp) {
+ bppCache.destructionAware.add(destructionAwareBpp);
}
- if (bp instanceof MergedBeanDefinitionPostProcessor) {
- bpCache.mergedDefinition.add((MergedBeanDefinitionPostProcessor) bp);
+ if (bpp instanceof MergedBeanDefinitionPostProcessor mergedBeanDefBpp) {
+ bppCache.mergedDefinition.add(mergedBeanDefBpp);
}
}
- this.beanPostProcessorCache = bpCache;
+ this.beanPostProcessorCache = bppCache;
}
- return bpCache;
+ return bppCache;
}
/**
@@ -1085,8 +1085,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
public BeanDefinition getMergedBeanDefinition(String name) throws BeansException {
String beanName = transformedBeanName(name);
// Efficiently check whether bean definition exists in this factory.
- if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
- return ((ConfigurableBeanFactory) getParentBeanFactory()).getMergedBeanDefinition(beanName);
+ if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory parent) {
+ return parent.getMergedBeanDefinition(beanName);
}
// Resolve merged bean definition locally.
return getMergedLocalBeanDefinition(beanName);
@@ -1100,9 +1100,9 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
return (beanInstance instanceof FactoryBean);
}
// No singleton instance found -> check bean definition.
- if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
+ if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory cbf) {
// No bean definition found in this factory -> delegate to parent.
- return ((ConfigurableBeanFactory) getParentBeanFactory()).isFactoryBean(name);
+ return cbf.isFactoryBean(name);
}
return isFactoryBean(beanName, getMergedLocalBeanDefinition(beanName));
}
@@ -1120,12 +1120,12 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
protected boolean isPrototypeCurrentlyInCreation(String beanName) {
Object curVal = this.prototypesCurrentlyInCreation.get();
return (curVal != null &&
- (curVal.equals(beanName) || (curVal instanceof Set && ((Set>) curVal).contains(beanName))));
+ (curVal.equals(beanName) || (curVal instanceof Set> set && set.contains(beanName))));
}
/**
* Callback before prototype creation.
- *
The default implementation register the prototype as currently in creation.
+ *
The default implementation registers the prototype as currently in creation.
* @param beanName the name of the prototype about to be created
* @see #isPrototypeCurrentlyInCreation
*/
@@ -1135,9 +1135,9 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
if (curVal == null) {
this.prototypesCurrentlyInCreation.set(beanName);
}
- else if (curVal instanceof String) {
+ else if (curVal instanceof String strValue) {
Set beanNameSet = new HashSet<>(2);
- beanNameSet.add((String) curVal);
+ beanNameSet.add(strValue);
beanNameSet.add(beanName);
this.prototypesCurrentlyInCreation.set(beanNameSet);
}
@@ -1159,8 +1159,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
if (curVal instanceof String) {
this.prototypesCurrentlyInCreation.remove();
}
- else if (curVal instanceof Set) {
- Set beanNameSet = (Set) curVal;
+ else if (curVal instanceof Set> beanNameSet) {
beanNameSet.remove(beanName);
if (beanNameSet.isEmpty()) {
this.prototypesCurrentlyInCreation.remove();
@@ -1253,8 +1252,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @param registry the PropertyEditorRegistry to initialize
*/
protected void registerCustomEditors(PropertyEditorRegistry registry) {
- if (registry instanceof PropertyEditorRegistrySupport) {
- ((PropertyEditorRegistrySupport) registry).useConfigValueEditors();
+ if (registry instanceof PropertyEditorRegistrySupport registrySupport) {
+ registrySupport.useConfigValueEditors();
}
if (!this.propertyEditorRegistrars.isEmpty()) {
for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
@@ -1263,8 +1262,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
}
catch (BeanCreationException ex) {
Throwable rootCause = ex.getMostSpecificCause();
- if (rootCause instanceof BeanCurrentlyInCreationException) {
- BeanCreationException bce = (BeanCreationException) rootCause;
+ if (rootCause instanceof BeanCurrentlyInCreationException bce) {
String bceBeanName = bce.getBeanName();
if (bceBeanName != null && isCurrentlyInCreation(bceBeanName)) {
if (logger.isDebugEnabled()) {
@@ -1345,8 +1343,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
previous = mbd;
if (bd.getParentName() == null) {
// Use copy of given root bean definition.
- if (bd instanceof RootBeanDefinition) {
- mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
+ if (bd instanceof RootBeanDefinition rootBeanDef) {
+ mbd = rootBeanDef.cloneBeanDefinition();
}
else {
mbd = new RootBeanDefinition(bd);
@@ -1361,9 +1359,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
pbd = getMergedBeanDefinition(parentBeanName);
}
else {
- BeanFactory parent = getParentBeanFactory();
- if (parent instanceof ConfigurableBeanFactory) {
- pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName);
+ if (getParentBeanFactory() instanceof ConfigurableBeanFactory parent) {
+ pbd = parent.getMergedBeanDefinition(parentBeanName);
}
else {
throw new NoSuchBeanDefinitionException(parentBeanName,
@@ -1524,11 +1521,11 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
Object evaluated = evaluateBeanDefinitionString(className, mbd);
if (!className.equals(evaluated)) {
// A dynamically resolved expression, supported as of 4.2...
- if (evaluated instanceof Class) {
- return (Class>) evaluated;
+ if (evaluated instanceof Class> clazz) {
+ return clazz;
}
- else if (evaluated instanceof String) {
- className = (String) evaluated;
+ else if (evaluated instanceof String str) {
+ className = str;
freshResolve = true;
}
else {
@@ -1685,11 +1682,11 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
*/
ResolvableType getTypeForFactoryBeanFromAttributes(AttributeAccessor attributes) {
Object attribute = attributes.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
- if (attribute instanceof ResolvableType) {
- return (ResolvableType) attribute;
+ if (attribute instanceof ResolvableType resolvableType) {
+ return resolvableType;
}
- if (attribute instanceof Class) {
- return ResolvableType.forClass((Class>) attribute);
+ if (attribute instanceof Class> clazz) {
+ return ResolvableType.forClass(clazz);
}
return ResolvableType.NONE;
}
@@ -1789,7 +1786,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
// Now we have the bean instance, which may be a normal bean or a FactoryBean.
// If it's a FactoryBean, we use it to create a bean instance, unless the
// caller actually wants a reference to the factory.
- if (!(beanInstance instanceof FactoryBean)) {
+ if (!(beanInstance instanceof FactoryBean> factoryBean)) {
return beanInstance;
}
@@ -1802,13 +1799,12 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
}
if (object == null) {
// Return bean instance from factory.
- FactoryBean> factory = (FactoryBean>) beanInstance;
// Caches object obtained from FactoryBean if it is a singleton.
if (mbd == null && containsBeanDefinition(beanName)) {
mbd = getMergedLocalBeanDefinition(beanName);
}
boolean synthetic = (mbd != null && mbd.isSynthetic());
- object = getObjectFromFactoryBean(factory, beanName, !synthetic);
+ object = getObjectFromFactoryBean(factoryBean, beanName, !synthetic);
}
return object;
}
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 f84f724174e..4186000c303 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
@@ -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.
@@ -54,7 +54,6 @@ import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.CannotLoadBeanClassException;
-import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
@@ -294,8 +293,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
*/
public void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver) {
Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
- if (autowireCandidateResolver instanceof BeanFactoryAware) {
- ((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
+ if (autowireCandidateResolver instanceof BeanFactoryAware beanFactoryAware) {
+ beanFactoryAware.setBeanFactory(this);
}
this.autowireCandidateResolver = autowireCandidateResolver;
}
@@ -486,8 +485,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return namedBean.getBeanInstance();
}
BeanFactory parent = getParentBeanFactory();
- if (parent instanceof DefaultListableBeanFactory) {
- return ((DefaultListableBeanFactory) parent).resolveBean(requiredType, args, nonUniqueAsNull);
+ if (parent instanceof DefaultListableBeanFactory dlfb) {
+ return dlfb.resolveBean(requiredType, args, nonUniqueAsNull);
}
else if (parent != null) {
ObjectProvider parentProvider = parent.getBeanProvider(requiredType);
@@ -665,8 +664,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
catch (BeanCreationException ex) {
Throwable rootCause = ex.getMostSpecificCause();
- if (rootCause instanceof BeanCurrentlyInCreationException) {
- BeanCreationException bce = (BeanCreationException) rootCause;
+ if (rootCause instanceof BeanCurrentlyInCreationException bce) {
String exBeanName = bce.getBeanName();
if (exBeanName != null && isCurrentlyInCreation(exBeanName)) {
if (logger.isTraceEnabled()) {
@@ -815,13 +813,13 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
BeanFactory parent = getParentBeanFactory();
- if (parent instanceof DefaultListableBeanFactory) {
+ if (parent instanceof DefaultListableBeanFactory dlfb) {
// No bean definition found in this factory -> delegate to parent.
- return ((DefaultListableBeanFactory) parent).isAutowireCandidate(beanName, descriptor, resolver);
+ return dlfb.isAutowireCandidate(beanName, descriptor, resolver);
}
- else if (parent instanceof ConfigurableListableBeanFactory) {
+ else if (parent instanceof ConfigurableListableBeanFactory clfb) {
// If no DefaultListableBeanFactory, can't pass the resolver along.
- return ((ConfigurableListableBeanFactory) parent).isAutowireCandidate(beanName, descriptor);
+ return clfb.isAutowireCandidate(beanName, descriptor);
}
else {
return true;
@@ -922,12 +920,8 @@ 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> factory) {
- boolean isEagerInit = (factory instanceof SmartFactoryBean &&
- ((SmartFactoryBean>) factory).isEagerInit());
- if (isEagerInit) {
- getBean(beanName);
- }
+ if (bean instanceof SmartFactoryBean> smartFactoryBean && smartFactoryBean.isEagerInit()) {
+ getBean(beanName);
}
}
else {
@@ -960,9 +954,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(beanDefinition, "BeanDefinition must not be null");
- if (beanDefinition instanceof AbstractBeanDefinition) {
+ if (beanDefinition instanceof AbstractBeanDefinition abd) {
try {
- ((AbstractBeanDefinition) beanDefinition).validate();
+ abd.validate();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
@@ -1185,8 +1179,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return namedBean;
}
BeanFactory parent = getParentBeanFactory();
- if (parent instanceof AutowireCapableBeanFactory) {
- return ((AutowireCapableBeanFactory) parent).resolveNamedBean(requiredType);
+ if (parent instanceof AutowireCapableBeanFactory acbf) {
+ return acbf.resolveNamedBean(requiredType);
}
throw new NoSuchBeanDefinitionException(requiredType);
}
@@ -1383,7 +1377,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
Class> type = descriptor.getDependencyType();
- if (descriptor instanceof StreamDependencyDescriptor) {
+ if (descriptor instanceof StreamDependencyDescriptor streamDependencyDescriptor) {
Map matchingBeans = findAutowireCandidates(beanName, type, descriptor);
if (autowiredBeanNames != null) {
autowiredBeanNames.addAll(matchingBeans.keySet());
@@ -1391,7 +1385,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
Stream