Skip to content

Commit efaccd6

Browse files
dramatistjhoeller
authored andcommitted
Apply "instanceof pattern matching" in spring-beans
1 parent 2f3a9db commit efaccd6

File tree

8 files changed

+13
-20
lines changed

8 files changed

+13
-20
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,7 @@ private void registerDependentBeans(@Nullable String beanName, Set<String> autow
599599
*/
600600
@Nullable
601601
private Object resolvedCachedArgument(@Nullable String beanName, @Nullable Object cachedArgument) {
602-
if (cachedArgument instanceof DependencyDescriptor) {
603-
DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument;
602+
if (cachedArgument instanceof DependencyDescriptor descriptor) {
604603
Assert.state(this.beanFactory != null, "No BeanFactory available");
605604
return this.beanFactory.resolveDependency(descriptor, beanName, null, null);
606605
}

spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public void addIndexedArgumentValue(int index, ValueHolder newValue) {
121121
*/
122122
private void addOrMergeIndexedArgumentValue(Integer key, ValueHolder newValue) {
123123
ValueHolder currentValue = this.indexedArgumentValues.get(key);
124-
if (currentValue != null && newValue.getValue() instanceof Mergeable) {
125-
Mergeable mergeable = (Mergeable) newValue.getValue();
124+
if (currentValue != null && newValue.getValue() instanceof Mergeable mergeable) {
126125
if (mergeable.isMergeEnabled()) {
127126
newValue.setValue(mergeable.merge(currentValue.getValue()));
128127
}
@@ -230,8 +229,7 @@ private void addOrMergeGenericArgumentValue(ValueHolder newValue) {
230229
for (Iterator<ValueHolder> it = this.genericArgumentValues.iterator(); it.hasNext();) {
231230
ValueHolder currentValue = it.next();
232231
if (newValue.getName().equals(currentValue.getName())) {
233-
if (newValue.getValue() instanceof Mergeable) {
234-
Mergeable mergeable = (Mergeable) newValue.getValue();
232+
if (newValue.getValue() instanceof Mergeable mergeable) {
235233
if (mergeable.isMergeEnabled()) {
236234
newValue.setValue(mergeable.merge(currentValue.getValue()));
237235
}

spring-beans/src/main/java/org/springframework/beans/factory/config/CustomScopeConfigurer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
101101
if (value instanceof Scope) {
102102
beanFactory.registerScope(scopeKey, (Scope) value);
103103
}
104-
else if (value instanceof Class) {
105-
Class<?> scopeClass = (Class<?>) value;
104+
else if (value instanceof Class<?> scopeClass) {
106105
Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class");
107106
beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass));
108107
}

spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,7 @@ protected GroovyBeanDefinitionReader invokeBeanDefiningClosure(Closure<?> callab
462462
*/
463463
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
464464
boolean hasClosureArgument = (args[args.length - 1] instanceof Closure);
465-
if (args[0] instanceof Class) {
466-
Class<?> beanClass = (Class<?>) args[0];
465+
if (args[0] instanceof Class<?> beanClass) {
467466
if (hasClosureArgument) {
468467
if (args.length - 1 != 1) {
469468
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(

spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ else if (Boolean.TRUE.equals(newValue)) {
194194
}
195195
}
196196
// constructorArgs
197-
else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
197+
else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List<?> args) {
198198
ConstructorArgumentValues cav = new ConstructorArgumentValues();
199-
List<?> args = (List<?>) newValue;
200199
for (Object arg : args) {
201200
cav.addGenericArgumentValue(arg);
202201
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.NotSerializableException;
2121
import java.io.ObjectInputStream;
2222
import java.io.ObjectStreamException;
23+
import java.io.Serial;
2324
import java.io.Serializable;
2425
import java.lang.annotation.Annotation;
2526
import java.lang.ref.Reference;
@@ -921,8 +922,7 @@ public void preInstantiateSingletons() throws BeansException {
921922
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
922923
if (isFactoryBean(beanName)) {
923924
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
924-
if (bean instanceof FactoryBean) {
925-
FactoryBean<?> factory = (FactoryBean<?>) bean;
925+
if (bean instanceof FactoryBean<?> factory) {
926926
boolean isEagerInit = (factory instanceof SmartFactoryBean &&
927927
((SmartFactoryBean<?>) factory).isEagerInit());
928928
if (isEagerInit) {
@@ -939,10 +939,9 @@ public void preInstantiateSingletons() throws BeansException {
939939
// Trigger post-initialization callback for all applicable beans...
940940
for (String beanName : beanNames) {
941941
Object singletonInstance = getSingleton(beanName);
942-
if (singletonInstance instanceof SmartInitializingSingleton) {
942+
if (singletonInstance instanceof SmartInitializingSingleton smartSingleton) {
943943
StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize")
944944
.tag("beanName", beanName);
945-
SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
946945
smartSingleton.afterSingletonsInstantiated();
947946
smartInitialize.end();
948947
}
@@ -1846,11 +1845,13 @@ public String toString() {
18461845
// Serialization support
18471846
//---------------------------------------------------------------------
18481847

1848+
@Serial
18491849
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
18501850
throw new NotSerializableException("DefaultListableBeanFactory itself is not deserializable - " +
18511851
"just a SerializedBeanFactoryReference is");
18521852
}
18531853

1854+
@Serial
18541855
protected Object writeReplace() throws ObjectStreamException {
18551856
if (this.serializationId != null) {
18561857
return new SerializedBeanFactoryReference(this.serializationId);

spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomCollectionEditor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ else if (value == null || (this.collectionType.isInstance(value) && !alwaysCreat
117117
// Use the source value as-is, as it matches the target type.
118118
super.setValue(value);
119119
}
120-
else if (value instanceof Collection) {
120+
else if (value instanceof Collection<?> source) {
121121
// Convert Collection elements.
122-
Collection<?> source = (Collection<?>) value;
123122
Collection<Object> target = createCollection(this.collectionType, source.size());
124123
for (Object elem : source) {
125124
target.add(convertElement(elem));

spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomMapEditor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ else if (value == null || (this.mapType.isInstance(value) && !alwaysCreateNewMap
107107
// Use the source value as-is, as it matches the target type.
108108
super.setValue(value);
109109
}
110-
else if (value instanceof Map) {
110+
else if (value instanceof Map<?, ?> source) {
111111
// Convert Map elements.
112-
Map<?, ?> source = (Map<?, ?>) value;
113112
Map<Object, Object> target = createMap(this.mapType, source.size());
114113
source.forEach((key, val) -> target.put(convertKey(key), convertValue(val)));
115114
super.setValue(target);

0 commit comments

Comments
 (0)