Skip to content

Commit

Permalink
Apply "instanceof pattern matching" in additional core classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jan 26, 2022
1 parent e7b9d61 commit f64cc08
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 121 deletions.
Expand Up @@ -70,8 +70,8 @@ public AnnotatedGenericBeanDefinition(Class<?> beanClass) {
*/
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());
Expand Down
Expand Up @@ -390,13 +390,7 @@ public void setBeanClassName(@Nullable String beanClassName) {
@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);
}

/**
Expand Down Expand Up @@ -433,11 +427,11 @@ public Class<?> getBeanClass() throws IllegalStateException {
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;
}

/**
Expand Down Expand Up @@ -1102,8 +1096,7 @@ public void setOriginatingBeanDefinition(BeanDefinition originatingBd) {
@Override
@Nullable
public BeanDefinition getOriginatingBeanDefinition() {
return (this.resource instanceof BeanDefinitionResource ?
((BeanDefinitionResource) this.resource).getBeanDefinition() : null);
return (this.resource instanceof BeanDefinitionResource bdr ? bdr.getBeanDefinition() : null);
}

/**
Expand Down
Expand Up @@ -107,6 +107,7 @@
* @author Costin Leau
* @author Chris Beams
* @author Phillip Webb
* @author Sam Brannen
* @since 15 April 2001
* @see #getBeanDefinition
* @see #createBean
Expand Down Expand Up @@ -270,9 +271,8 @@ protected <T> T doGetBean(
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.
Expand Down Expand Up @@ -428,8 +428,8 @@ public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {

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);
Expand Down Expand Up @@ -486,7 +486,7 @@ public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
}
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 {
Expand Down Expand Up @@ -522,9 +522,9 @@ protected boolean isTypeMatch(String name, ResolvableType typeToMatch, boolean a
// 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 {
Expand Down Expand Up @@ -673,8 +673,8 @@ public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuch
// 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();
Expand Down Expand Up @@ -962,26 +962,26 @@ public List<BeanPostProcessor> getBeanPostProcessors() {
* @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;
}

/**
Expand Down Expand Up @@ -1085,8 +1085,8 @@ public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
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);
Expand All @@ -1100,9 +1100,9 @@ public boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException {
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));
}
Expand All @@ -1120,12 +1120,12 @@ public boolean isActuallyInCreation(String beanName) {
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
*/
Expand All @@ -1135,9 +1135,9 @@ protected void beforePrototypeCreation(String beanName) {
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);
}
Expand All @@ -1159,8 +1159,7 @@ protected void afterPrototypeCreation(String beanName) {
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();
Expand Down Expand Up @@ -1253,8 +1252,8 @@ protected void initBeanWrapper(BeanWrapper bw) {
* @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) {
Expand All @@ -1263,8 +1262,7 @@ protected void registerCustomEditors(PropertyEditorRegistry registry) {
}
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()) {
Expand Down Expand Up @@ -1345,8 +1343,8 @@ protected RootBeanDefinition getMergedBeanDefinition(
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);
Expand All @@ -1361,9 +1359,8 @@ protected RootBeanDefinition getMergedBeanDefinition(
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,
Expand Down Expand Up @@ -1524,11 +1521,11 @@ private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToM
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 {
Expand Down Expand Up @@ -1685,11 +1682,11 @@ else if (mbd.isLazyInit()) {
*/
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;
}
Expand Down Expand Up @@ -1789,7 +1786,7 @@ protected Object getObjectForBeanInstance(
// 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;
}

Expand All @@ -1802,13 +1799,12 @@ protected Object getObjectForBeanInstance(
}
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;
}
Expand Down

0 comments on commit f64cc08

Please sign in to comment.