Browse Source

Apply "instanceof pattern matching" in additional core classes

pull/27986/head
Sam Brannen 3 years ago
parent
commit
f64cc08b62
  1. 4
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/AnnotatedGenericBeanDefinition.java
  2. 15
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java
  3. 104
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
  4. 84
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
  5. 14
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

4
spring-beans/src/main/java/org/springframework/beans/factory/annotation/AnnotatedGenericBeanDefinition.java

@ -70,8 +70,8 @@ public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implem @@ -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());

15
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java

@ -390,13 +390,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess @@ -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 @@ -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 @@ -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);
}
/**

104
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

@ -107,6 +107,7 @@ import org.springframework.util.StringValueResolver; @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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.
* <p>The default implementation register the prototype as currently in creation.
* <p>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 @@ -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<String> 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 @@ -1159,8 +1159,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
if (curVal instanceof String) {
this.prototypesCurrentlyInCreation.remove();
}
else if (curVal instanceof Set) {
Set<String> beanNameSet = (Set<String>) 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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;
}

84
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.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.
@ -54,7 +54,6 @@ import org.springframework.beans.factory.BeanFactoryAware; @@ -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 @@ -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 @@ -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<T> parentProvider = parent.getBeanProvider(requiredType);
@ -665,8 +664,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -1383,7 +1377,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
Class<?> type = descriptor.getDependencyType();
if (descriptor instanceof StreamDependencyDescriptor) {
if (descriptor instanceof StreamDependencyDescriptor streamDependencyDescriptor) {
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
if (autowiredBeanNames != null) {
autowiredBeanNames.addAll(matchingBeans.keySet());
@ -1391,7 +1385,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1391,7 +1385,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
Stream<Object> stream = matchingBeans.keySet().stream()
.map(name -> descriptor.resolveCandidate(name, type, this))
.filter(bean -> !(bean instanceof NullBean));
if (((StreamDependencyDescriptor) descriptor).isOrdered()) {
if (streamDependencyDescriptor.isOrdered()) {
stream = stream.sorted(adaptOrderComparator(matchingBeans));
}
return stream;
@ -1416,10 +1410,10 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1416,10 +1410,10 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
Object result = converter.convertIfNecessary(matchingBeans.values(), resolvedArrayType);
if (result instanceof Object[]) {
if (result instanceof Object[] array) {
Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
if (comparator != null) {
Arrays.sort((Object[]) result, comparator);
Arrays.sort(array, comparator);
}
}
return result;
@ -1439,12 +1433,10 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1439,12 +1433,10 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
Object result = converter.convertIfNecessary(matchingBeans.values(), type);
if (result instanceof List) {
if (((List<?>) result).size() > 1) {
Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
if (comparator != null) {
((List<?>) result).sort(comparator);
}
if (result instanceof List<?> list && list.size() > 1) {
Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
if (comparator != null) {
list.sort(comparator);
}
}
return result;
@ -1486,8 +1478,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1486,8 +1478,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
@Nullable
private Comparator<Object> adaptDependencyComparator(Map<String, ?> matchingBeans) {
Comparator<Object> comparator = getDependencyComparator();
if (comparator instanceof OrderComparator) {
return ((OrderComparator) comparator).withSourceProvider(
if (comparator instanceof OrderComparator orderComparator) {
return orderComparator.withSourceProvider(
createFactoryAwareOrderSourceProvider(matchingBeans));
}
else {
@ -1497,8 +1489,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1497,8 +1489,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
private Comparator<Object> adaptOrderComparator(Map<String, ?> matchingBeans) {
Comparator<Object> dependencyComparator = getDependencyComparator();
OrderComparator comparator = (dependencyComparator instanceof OrderComparator ?
(OrderComparator) dependencyComparator : OrderComparator.INSTANCE);
OrderComparator comparator = (dependencyComparator instanceof OrderComparator orderComparator ?
orderComparator : OrderComparator.INSTANCE);
return comparator.withSourceProvider(createFactoryAwareOrderSourceProvider(matchingBeans));
}
@ -1581,8 +1573,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1581,8 +1573,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
candidates.put(candidateName, beanInstance);
}
}
else if (containsSingleton(candidateName) || (descriptor instanceof StreamDependencyDescriptor &&
((StreamDependencyDescriptor) descriptor).isOrdered())) {
else if (containsSingleton(candidateName) || (descriptor instanceof StreamDependencyDescriptor streamDescriptor &&
streamDescriptor.isOrdered())) {
Object beanInstance = descriptor.resolveCandidate(candidateName, requiredType, this);
candidates.put(candidateName, (beanInstance instanceof NullBean ? null : beanInstance));
}
@ -1711,9 +1703,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1711,9 +1703,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (containsBeanDefinition(transformedBeanName)) {
return getMergedLocalBeanDefinition(transformedBeanName).isPrimary();
}
BeanFactory parent = getParentBeanFactory();
return (parent instanceof DefaultListableBeanFactory &&
((DefaultListableBeanFactory) parent).isPrimary(transformedBeanName, beanInstance));
return (getParentBeanFactory() instanceof DefaultListableBeanFactory parent &&
parent.isPrimary(transformedBeanName, beanInstance));
}
/**
@ -1731,8 +1722,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1731,8 +1722,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
@Nullable
protected Integer getPriority(Object beanInstance) {
Comparator<Object> comparator = getDependencyComparator();
if (comparator instanceof OrderComparator) {
return ((OrderComparator) comparator).getPriority(beanInstance);
if (comparator instanceof OrderComparator orderComparator) {
return orderComparator.getPriority(beanInstance);
}
return null;
}
@ -1796,9 +1787,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1796,9 +1787,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
}
BeanFactory parent = getParentBeanFactory();
if (parent instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) parent).checkBeanNotOfRequiredType(type, descriptor);
if (getParentBeanFactory() instanceof DefaultListableBeanFactory parent) {
parent.checkBeanNotOfRequiredType(type, descriptor);
}
}
@ -1820,7 +1810,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1820,7 +1810,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
};
Object result = doResolveDependency(descriptorToUse, beanName, null, null);
return (result instanceof Optional ? (Optional<?>) result : Optional.ofNullable(result));
return (result instanceof Optional<?> optional ? optional : Optional.ofNullable(result));
}

14
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.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.
@ -223,8 +223,8 @@ class ConfigurationClassBeanDefinitionReader { @@ -223,8 +223,8 @@ class ConfigurationClassBeanDefinitionReader {
if (metadata.isStatic()) {
// static @Bean method
if (configClass.getMetadata() instanceof StandardAnnotationMetadata) {
beanDef.setBeanClass(((StandardAnnotationMetadata) configClass.getMetadata()).getIntrospectedClass());
if (configClass.getMetadata() instanceof StandardAnnotationMetadata sam) {
beanDef.setBeanClass(sam.getIntrospectedClass());
}
else {
beanDef.setBeanClassName(configClass.getMetadata().getClassName());
@ -237,8 +237,8 @@ class ConfigurationClassBeanDefinitionReader { @@ -237,8 +237,8 @@ class ConfigurationClassBeanDefinitionReader {
beanDef.setUniqueFactoryMethodName(methodName);
}
if (metadata instanceof StandardMethodMetadata) {
beanDef.setResolvedFactoryMethod(((StandardMethodMetadata) metadata).getIntrospectedMethod());
if (metadata instanceof StandardMethodMetadata sam) {
beanDef.setResolvedFactoryMethod(sam.getIntrospectedMethod());
}
beanDef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
@ -322,8 +322,8 @@ class ConfigurationClassBeanDefinitionReader { @@ -322,8 +322,8 @@ class ConfigurationClassBeanDefinitionReader {
// At this point, it's a top-level override (probably XML), just having been parsed
// before configuration class processing kicks in...
if (this.registry instanceof DefaultListableBeanFactory &&
!((DefaultListableBeanFactory) this.registry).isAllowBeanDefinitionOverriding()) {
if (this.registry instanceof DefaultListableBeanFactory dlbf &&
!dlbf.isAllowBeanDefinitionOverriding()) {
throw new BeanDefinitionStoreException(beanMethod.getConfigurationClass().getResource().getDescription(),
beanName, "@Bean definition illegally overridden by existing bean definition: " + existingBeanDef);
}

Loading…
Cancel
Save