Apply "instanceof pattern matching" in spring-beans

This commit is contained in:
dramatist 2021-11-19 14:29:56 +08:00 committed by Juergen Hoeller
parent 2f3a9dbc68
commit efaccd6356
8 changed files with 13 additions and 20 deletions

View File

@ -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);
}

View File

@ -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<ValueHolder> 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()));
}

View File

@ -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));
}

View File

@ -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(

View File

@ -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);
}

View File

@ -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);

View File

@ -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<Object> target = createCollection(this.collectionType, source.size());
for (Object elem : source) {
target.add(convertElement(elem));

View File

@ -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<Object, Object> target = createMap(this.mapType, source.size());
source.forEach((key, val) -> target.put(convertKey(key), convertValue(val)));
super.setValue(target);