Apply "instanceof pattern matching" in spring-beans
This commit is contained in:
parent
2f3a9dbc68
commit
efaccd6356
|
|
@ -599,8 +599,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Object resolvedCachedArgument(@Nullable String beanName, @Nullable Object cachedArgument) {
|
private Object resolvedCachedArgument(@Nullable String beanName, @Nullable Object cachedArgument) {
|
||||||
if (cachedArgument instanceof DependencyDescriptor) {
|
if (cachedArgument instanceof DependencyDescriptor descriptor) {
|
||||||
DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument;
|
|
||||||
Assert.state(this.beanFactory != null, "No BeanFactory available");
|
Assert.state(this.beanFactory != null, "No BeanFactory available");
|
||||||
return this.beanFactory.resolveDependency(descriptor, beanName, null, null);
|
return this.beanFactory.resolveDependency(descriptor, beanName, null, null);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -121,8 +121,7 @@ public class ConstructorArgumentValues {
|
||||||
*/
|
*/
|
||||||
private void addOrMergeIndexedArgumentValue(Integer key, ValueHolder newValue) {
|
private void addOrMergeIndexedArgumentValue(Integer key, ValueHolder newValue) {
|
||||||
ValueHolder currentValue = this.indexedArgumentValues.get(key);
|
ValueHolder currentValue = this.indexedArgumentValues.get(key);
|
||||||
if (currentValue != null && newValue.getValue() instanceof Mergeable) {
|
if (currentValue != null && newValue.getValue() instanceof Mergeable mergeable) {
|
||||||
Mergeable mergeable = (Mergeable) newValue.getValue();
|
|
||||||
if (mergeable.isMergeEnabled()) {
|
if (mergeable.isMergeEnabled()) {
|
||||||
newValue.setValue(mergeable.merge(currentValue.getValue()));
|
newValue.setValue(mergeable.merge(currentValue.getValue()));
|
||||||
}
|
}
|
||||||
|
|
@ -230,8 +229,7 @@ public class ConstructorArgumentValues {
|
||||||
for (Iterator<ValueHolder> it = this.genericArgumentValues.iterator(); it.hasNext();) {
|
for (Iterator<ValueHolder> it = this.genericArgumentValues.iterator(); it.hasNext();) {
|
||||||
ValueHolder currentValue = it.next();
|
ValueHolder currentValue = it.next();
|
||||||
if (newValue.getName().equals(currentValue.getName())) {
|
if (newValue.getName().equals(currentValue.getName())) {
|
||||||
if (newValue.getValue() instanceof Mergeable) {
|
if (newValue.getValue() instanceof Mergeable mergeable) {
|
||||||
Mergeable mergeable = (Mergeable) newValue.getValue();
|
|
||||||
if (mergeable.isMergeEnabled()) {
|
if (mergeable.isMergeEnabled()) {
|
||||||
newValue.setValue(mergeable.merge(currentValue.getValue()));
|
newValue.setValue(mergeable.merge(currentValue.getValue()));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -101,8 +101,7 @@ public class CustomScopeConfigurer implements BeanFactoryPostProcessor, BeanClas
|
||||||
if (value instanceof Scope) {
|
if (value instanceof Scope) {
|
||||||
beanFactory.registerScope(scopeKey, (Scope) value);
|
beanFactory.registerScope(scopeKey, (Scope) value);
|
||||||
}
|
}
|
||||||
else if (value instanceof Class) {
|
else if (value instanceof Class<?> scopeClass) {
|
||||||
Class<?> scopeClass = (Class<?>) value;
|
|
||||||
Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class");
|
Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class");
|
||||||
beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass));
|
beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -462,8 +462,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
||||||
*/
|
*/
|
||||||
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
|
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
|
||||||
boolean hasClosureArgument = (args[args.length - 1] instanceof Closure);
|
boolean hasClosureArgument = (args[args.length - 1] instanceof Closure);
|
||||||
if (args[0] instanceof Class) {
|
if (args[0] instanceof Class<?> beanClass) {
|
||||||
Class<?> beanClass = (Class<?>) args[0];
|
|
||||||
if (hasClosureArgument) {
|
if (hasClosureArgument) {
|
||||||
if (args.length - 1 != 1) {
|
if (args.length - 1 != 1) {
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(
|
||||||
|
|
|
||||||
|
|
@ -194,9 +194,8 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// constructorArgs
|
// constructorArgs
|
||||||
else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
|
else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List<?> args) {
|
||||||
ConstructorArgumentValues cav = new ConstructorArgumentValues();
|
ConstructorArgumentValues cav = new ConstructorArgumentValues();
|
||||||
List<?> args = (List<?>) newValue;
|
|
||||||
for (Object arg : args) {
|
for (Object arg : args) {
|
||||||
cav.addGenericArgumentValue(arg);
|
cav.addGenericArgumentValue(arg);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.io.IOException;
|
||||||
import java.io.NotSerializableException;
|
import java.io.NotSerializableException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectStreamException;
|
import java.io.ObjectStreamException;
|
||||||
|
import java.io.Serial;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.ref.Reference;
|
import java.lang.ref.Reference;
|
||||||
|
|
@ -921,8 +922,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
|
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
|
||||||
if (isFactoryBean(beanName)) {
|
if (isFactoryBean(beanName)) {
|
||||||
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
|
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
|
||||||
if (bean instanceof FactoryBean) {
|
if (bean instanceof FactoryBean<?> factory) {
|
||||||
FactoryBean<?> factory = (FactoryBean<?>) bean;
|
|
||||||
boolean isEagerInit = (factory instanceof SmartFactoryBean &&
|
boolean isEagerInit = (factory instanceof SmartFactoryBean &&
|
||||||
((SmartFactoryBean<?>) factory).isEagerInit());
|
((SmartFactoryBean<?>) factory).isEagerInit());
|
||||||
if (isEagerInit) {
|
if (isEagerInit) {
|
||||||
|
|
@ -939,10 +939,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
// Trigger post-initialization callback for all applicable beans...
|
// Trigger post-initialization callback for all applicable beans...
|
||||||
for (String beanName : beanNames) {
|
for (String beanName : beanNames) {
|
||||||
Object singletonInstance = getSingleton(beanName);
|
Object singletonInstance = getSingleton(beanName);
|
||||||
if (singletonInstance instanceof SmartInitializingSingleton) {
|
if (singletonInstance instanceof SmartInitializingSingleton smartSingleton) {
|
||||||
StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize")
|
StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize")
|
||||||
.tag("beanName", beanName);
|
.tag("beanName", beanName);
|
||||||
SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
|
|
||||||
smartSingleton.afterSingletonsInstantiated();
|
smartSingleton.afterSingletonsInstantiated();
|
||||||
smartInitialize.end();
|
smartInitialize.end();
|
||||||
}
|
}
|
||||||
|
|
@ -1846,11 +1845,13 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
// Serialization support
|
// Serialization support
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
@Serial
|
||||||
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
|
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
|
||||||
throw new NotSerializableException("DefaultListableBeanFactory itself is not deserializable - " +
|
throw new NotSerializableException("DefaultListableBeanFactory itself is not deserializable - " +
|
||||||
"just a SerializedBeanFactoryReference is");
|
"just a SerializedBeanFactoryReference is");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Serial
|
||||||
protected Object writeReplace() throws ObjectStreamException {
|
protected Object writeReplace() throws ObjectStreamException {
|
||||||
if (this.serializationId != null) {
|
if (this.serializationId != null) {
|
||||||
return new SerializedBeanFactoryReference(this.serializationId);
|
return new SerializedBeanFactoryReference(this.serializationId);
|
||||||
|
|
|
||||||
|
|
@ -117,9 +117,8 @@ public class CustomCollectionEditor extends PropertyEditorSupport {
|
||||||
// Use the source value as-is, as it matches the target type.
|
// Use the source value as-is, as it matches the target type.
|
||||||
super.setValue(value);
|
super.setValue(value);
|
||||||
}
|
}
|
||||||
else if (value instanceof Collection) {
|
else if (value instanceof Collection<?> source) {
|
||||||
// Convert Collection elements.
|
// Convert Collection elements.
|
||||||
Collection<?> source = (Collection<?>) value;
|
|
||||||
Collection<Object> target = createCollection(this.collectionType, source.size());
|
Collection<Object> target = createCollection(this.collectionType, source.size());
|
||||||
for (Object elem : source) {
|
for (Object elem : source) {
|
||||||
target.add(convertElement(elem));
|
target.add(convertElement(elem));
|
||||||
|
|
|
||||||
|
|
@ -107,9 +107,8 @@ public class CustomMapEditor extends PropertyEditorSupport {
|
||||||
// Use the source value as-is, as it matches the target type.
|
// Use the source value as-is, as it matches the target type.
|
||||||
super.setValue(value);
|
super.setValue(value);
|
||||||
}
|
}
|
||||||
else if (value instanceof Map) {
|
else if (value instanceof Map<?, ?> source) {
|
||||||
// Convert Map elements.
|
// Convert Map elements.
|
||||||
Map<?, ?> source = (Map<?, ?>) value;
|
|
||||||
Map<Object, Object> target = createMap(this.mapType, source.size());
|
Map<Object, Object> target = createMap(this.mapType, source.size());
|
||||||
source.forEach((key, val) -> target.put(convertKey(key), convertValue(val)));
|
source.forEach((key, val) -> target.put(convertKey(key), convertValue(val)));
|
||||||
super.setValue(target);
|
super.setValue(target);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue