Apply "instanceof pattern matching" in remainder of spring-beans module

See gh-30067
This commit is contained in:
Sam Brannen 2023-03-05 15:42:14 +01:00
parent e5d20a4f9d
commit d9500e60a1
3 changed files with 16 additions and 16 deletions

View File

@ -1224,8 +1224,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
if (supplier instanceof InstanceSupplier<?> instanceSupplier) { if (supplier instanceof InstanceSupplier<?> instanceSupplier) {
return instanceSupplier.get(RegisteredBean.of((ConfigurableListableBeanFactory) this, beanName)); return instanceSupplier.get(RegisteredBean.of((ConfigurableListableBeanFactory) this, beanName));
} }
if (supplier instanceof ThrowingSupplier<?> throwableSupplier) { if (supplier instanceof ThrowingSupplier<?> throwingSupplier) {
return throwableSupplier.getWithException(); return throwingSupplier.getWithException();
} }
return supplier.get(); return supplier.get();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -163,8 +163,8 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy {
catch (InvocationTargetException ex) { catch (InvocationTargetException ex) {
String msg = "Factory method '" + factoryMethod.getName() + "' threw exception with message: " + String msg = "Factory method '" + factoryMethod.getName() + "' threw exception with message: " +
ex.getTargetException().getMessage(); ex.getTargetException().getMessage();
if (bd.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory && if (bd.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory cbf &&
((ConfigurableBeanFactory) owner).isCurrentlyInCreation(bd.getFactoryBeanName())) { cbf.isCurrentlyInCreation(bd.getFactoryBeanName())) {
msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " + msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " +
"declaring the factory method as static for independence from its containing instance. " + msg; "declaring the factory method as static for independence from its containing instance. " + msg;
} }

View File

@ -127,9 +127,9 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
throw new BeanIsNotAFactoryException(beanName, bean.getClass()); throw new BeanIsNotAFactoryException(beanName, bean.getClass());
} }
if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) { if (bean instanceof FactoryBean<?> factoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
try { try {
Object exposedObject = ((FactoryBean<?>) bean).getObject(); Object exposedObject = factoryBean.getObject();
if (exposedObject == null) { if (exposedObject == null) {
throw new BeanCreationException(beanName, "FactoryBean exposed null object"); throw new BeanCreationException(beanName, "FactoryBean exposed null object");
} }
@ -205,8 +205,8 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException { public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
Object bean = getBean(name); Object bean = getBean(name);
// In case of FactoryBean, return singleton status of created object. // In case of FactoryBean, return singleton status of created object.
if (bean instanceof FactoryBean) { if (bean instanceof FactoryBean<?> factoryBean) {
return ((FactoryBean<?>) bean).isSingleton(); return factoryBean.isSingleton();
} }
return true; return true;
} }
@ -215,8 +215,8 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
public boolean isPrototype(String name) throws NoSuchBeanDefinitionException { public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
Object bean = getBean(name); Object bean = getBean(name);
// In case of FactoryBean, return prototype status of created object. // In case of FactoryBean, return prototype status of created object.
return ((bean instanceof SmartFactoryBean && ((SmartFactoryBean<?>) bean).isPrototype()) || return ((bean instanceof SmartFactoryBean<?> smartFactoryBean && smartFactoryBean.isPrototype()) ||
(bean instanceof FactoryBean && !((FactoryBean<?>) bean).isSingleton())); (bean instanceof FactoryBean<?> factoryBean && !factoryBean.isSingleton()));
} }
@Override @Override
@ -246,9 +246,9 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
"Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]"); "Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]");
} }
if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) { if (bean instanceof FactoryBean<?> factoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
// If it's a FactoryBean, we want to look at what it creates, not the factory class. // If it's a FactoryBean, we want to look at what it creates, not the factory class.
return ((FactoryBean<?>) bean).getObjectType(); return factoryBean.getObjectType();
} }
return bean.getClass(); return bean.getClass();
} }
@ -408,10 +408,10 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
String beanName = entry.getKey(); String beanName = entry.getKey();
Object beanInstance = entry.getValue(); Object beanInstance = entry.getValue();
// Is bean a FactoryBean? // Is bean a FactoryBean?
if (beanInstance instanceof FactoryBean<?> factory && !isFactoryType) { if (beanInstance instanceof FactoryBean<?> factoryBean && !isFactoryType) {
// Match object created by FactoryBean. // Match object created by FactoryBean.
Class<?> objectType = factory.getObjectType(); Class<?> objectType = factoryBean.getObjectType();
if ((includeNonSingletons || factory.isSingleton()) && if ((includeNonSingletons || factoryBean.isSingleton()) &&
objectType != null && (type == null || type.isAssignableFrom(objectType))) { objectType != null && (type == null || type.isAssignableFrom(objectType))) {
matches.put(beanName, getBean(beanName, type)); matches.put(beanName, getBean(beanName, type));
} }