Skip to content

Commit d32027d

Browse files
committed
Apply 'instanceof pattern matching'
1 parent a832c98 commit d32027d

11 files changed

+81
-77
lines changed

spring-context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ public class BeanExpressionContextAccessor implements PropertyAccessor {
3636

3737
@Override
3838
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
39-
return (target instanceof BeanExpressionContext && ((BeanExpressionContext) target).containsObject(name));
39+
return (target instanceof BeanExpressionContext bec && bec.containsObject(name));
4040
}
4141

4242
@Override

spring-context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,7 @@ public Class<?>[] getSpecificTargetClasses() {
4141

4242
@Override
4343
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
44-
return (target instanceof BeanFactory && ((BeanFactory) target).containsBean(name));
44+
return (target instanceof BeanFactory beanFactory && beanFactory.containsBean(name));
4545
}
4646

4747
@Override

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
395395

396396
// Decorate event as an ApplicationEvent if necessary
397397
ApplicationEvent applicationEvent;
398-
if (event instanceof ApplicationEvent) {
399-
applicationEvent = (ApplicationEvent) event;
398+
if (event instanceof ApplicationEvent applEvent) {
399+
applicationEvent = applEvent;
400400
}
401401
else {
402402
applicationEvent = new PayloadApplicationEvent<>(this, event, eventType);
@@ -415,8 +415,8 @@ protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
415415

416416
// Publish event via parent context as well...
417417
if (this.parent != null) {
418-
if (this.parent instanceof AbstractApplicationContext) {
419-
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
418+
if (this.parent instanceof AbstractApplicationContext abstractApplicationContext) {
419+
abstractApplicationContext.publishEvent(event, eventType);
420420
}
421421
else {
422422
this.parent.publishEvent(event);
@@ -497,8 +497,8 @@ public void setParent(@Nullable ApplicationContext parent) {
497497
this.parent = parent;
498498
if (parent != null) {
499499
Environment parentEnvironment = parent.getEnvironment();
500-
if (parentEnvironment instanceof ConfigurableEnvironment) {
501-
getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
500+
if (parentEnvironment instanceof ConfigurableEnvironment configurableEnvironment) {
501+
getEnvironment().merge(configurableEnvironment);
502502
}
503503
}
504504
}
@@ -770,12 +770,11 @@ protected void initMessageSource() {
770770
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
771771
this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
772772
// Make MessageSource aware of parent MessageSource.
773-
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource hms) {
774-
if (hms.getParentMessageSource() == null) {
775-
// Only set parent context as parent MessageSource if no parent MessageSource
776-
// registered already.
777-
hms.setParentMessageSource(getInternalParentMessageSource());
778-
}
773+
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource hms &&
774+
hms.getParentMessageSource() == null) {
775+
// Only set parent context as parent MessageSource if no parent MessageSource
776+
// registered already.
777+
hms.setParentMessageSource(getInternalParentMessageSource());
779778
}
780779
if (logger.isTraceEnabled()) {
781780
logger.trace("Using MessageSource [" + this.messageSource + "]");
@@ -1350,8 +1349,8 @@ public boolean containsLocalBean(String name) {
13501349
*/
13511350
@Nullable
13521351
protected BeanFactory getInternalParentBeanFactory() {
1353-
return (getParent() instanceof ConfigurableApplicationContext ?
1354-
((ConfigurableApplicationContext) getParent()).getBeanFactory() : getParent());
1352+
return (getParent() instanceof ConfigurableApplicationContext cac ?
1353+
cac.getBeanFactory() : getParent());
13551354
}
13561355

13571356

@@ -1393,8 +1392,8 @@ private MessageSource getMessageSource() throws IllegalStateException {
13931392
*/
13941393
@Nullable
13951394
protected MessageSource getInternalParentMessageSource() {
1396-
return (getParent() instanceof AbstractApplicationContext ?
1397-
((AbstractApplicationContext) getParent()).messageSource : getParent());
1395+
return (getParent() instanceof AbstractApplicationContext abstractApplicationContext ?
1396+
abstractApplicationContext.messageSource : getParent());
13981397
}
13991398

14001399

spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -255,10 +255,10 @@ protected String getMessageInternal(@Nullable String code, @Nullable Object[] ar
255255
protected String getMessageFromParent(String code, @Nullable Object[] args, Locale locale) {
256256
MessageSource parent = getParentMessageSource();
257257
if (parent != null) {
258-
if (parent instanceof AbstractMessageSource) {
258+
if (parent instanceof AbstractMessageSource abstractMessageSource) {
259259
// Call internal method to avoid getting the default code back
260260
// in case of "useCodeAsDefaultMessage" being activated.
261-
return ((AbstractMessageSource) parent).getMessageInternal(code, args, locale);
261+
return abstractMessageSource.getMessageInternal(code, args, locale);
262262
}
263263
else {
264264
// Check parent MessageSource, returning null if not found there.
@@ -287,8 +287,8 @@ protected String getDefaultMessage(MessageSourceResolvable resolvable, Locale lo
287287
String defaultMessage = resolvable.getDefaultMessage();
288288
String[] codes = resolvable.getCodes();
289289
if (defaultMessage != null) {
290-
if (resolvable instanceof DefaultMessageSourceResolvable &&
291-
!((DefaultMessageSourceResolvable) resolvable).shouldRenderDefaultMessage()) {
290+
if (resolvable instanceof DefaultMessageSourceResolvable defaultMessageSourceResolvable &&
291+
!defaultMessageSourceResolvable.shouldRenderDefaultMessage()) {
292292
// Given default message does not contain any argument placeholders
293293
// (and isn't escaped for alwaysUseMessageFormat either) -> return as-is.
294294
return defaultMessage;
@@ -336,8 +336,8 @@ protected Object[] resolveArguments(@Nullable Object[] args, Locale locale) {
336336
}
337337
List<Object> resolvedArgs = new ArrayList<>(args.length);
338338
for (Object arg : args) {
339-
if (arg instanceof MessageSourceResolvable) {
340-
resolvedArgs.add(getMessage((MessageSourceResolvable) arg, locale));
339+
if (arg instanceof MessageSourceResolvable messageSourceResolvable) {
340+
resolvedArgs.add(getMessage(messageSourceResolvable, locale));
341341
}
342342
else {
343343
resolvedArgs.add(arg);

spring-context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.context.support;
1818

1919
import org.springframework.beans.BeansException;
20+
import org.springframework.beans.factory.Aware;
2021
import org.springframework.beans.factory.config.BeanPostProcessor;
2122
import org.springframework.beans.factory.config.EmbeddedValueResolver;
2223
import org.springframework.context.ApplicationContextAware;
@@ -47,6 +48,7 @@
4748
* @author Juergen Hoeller
4849
* @author Costin Leau
4950
* @author Chris Beams
51+
* @author Sam Brannen
5052
* @since 10.10.2003
5153
* @see org.springframework.context.EnvironmentAware
5254
* @see org.springframework.context.EmbeddedValueResolverAware
@@ -87,26 +89,28 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
8789
}
8890

8991
private void invokeAwareInterfaces(Object bean) {
90-
if (bean instanceof EnvironmentAware) {
91-
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
92-
}
93-
if (bean instanceof EmbeddedValueResolverAware) {
94-
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
95-
}
96-
if (bean instanceof ResourceLoaderAware) {
97-
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
98-
}
99-
if (bean instanceof ApplicationEventPublisherAware) {
100-
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
101-
}
102-
if (bean instanceof MessageSourceAware) {
103-
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
104-
}
105-
if (bean instanceof ApplicationStartupAware) {
106-
((ApplicationStartupAware) bean).setApplicationStartup(this.applicationContext.getApplicationStartup());
107-
}
108-
if (bean instanceof ApplicationContextAware) {
109-
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
92+
if (bean instanceof Aware) {
93+
if (bean instanceof EnvironmentAware environmentAware) {
94+
environmentAware.setEnvironment(this.applicationContext.getEnvironment());
95+
}
96+
if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
97+
embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
98+
}
99+
if (bean instanceof ResourceLoaderAware resourceLoaderAware) {
100+
resourceLoaderAware.setResourceLoader(this.applicationContext);
101+
}
102+
if (bean instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {
103+
applicationEventPublisherAware.setApplicationEventPublisher(this.applicationContext);
104+
}
105+
if (bean instanceof MessageSourceAware messageSourceAware) {
106+
messageSourceAware.setMessageSource(this.applicationContext);
107+
}
108+
if (bean instanceof ApplicationStartupAware applicationStartupAware) {
109+
applicationStartupAware.setApplicationStartup(this.applicationContext.getApplicationStartup());
110+
}
111+
if (bean instanceof ApplicationContextAware applicationContextAware) {
112+
applicationContextAware.setApplicationContext(this.applicationContext);
113+
}
110114
}
111115
}
112116

spring-context/src/main/java/org/springframework/context/support/ApplicationListenerDetector.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,12 +71,12 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
7171

7272
@Override
7373
public Object postProcessAfterInitialization(Object bean, String beanName) {
74-
if (bean instanceof ApplicationListener) {
74+
if (bean instanceof ApplicationListener<?> applicationListener) {
7575
// potentially not detected as a listener by getBeanNamesForType retrieval
7676
Boolean flag = this.singletonNames.get(beanName);
7777
if (Boolean.TRUE.equals(flag)) {
7878
// singleton bean (top-level or inner): register on the fly
79-
this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);
79+
this.applicationContext.addApplicationListener(applicationListener);
8080
}
8181
else if (Boolean.FALSE.equals(flag)) {
8282
if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
@@ -94,10 +94,10 @@ else if (Boolean.FALSE.equals(flag)) {
9494

9595
@Override
9696
public void postProcessBeforeDestruction(Object bean, String beanName) {
97-
if (bean instanceof ApplicationListener) {
97+
if (bean instanceof ApplicationListener<?> applicationListener) {
9898
try {
9999
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
100-
multicaster.removeApplicationListener((ApplicationListener<?>) bean);
100+
multicaster.removeApplicationListener(applicationListener);
101101
multicaster.removeApplicationListenerBean(beanName);
102102
}
103103
catch (IllegalStateException ex) {
@@ -114,8 +114,9 @@ public boolean requiresDestruction(Object bean) {
114114

115115
@Override
116116
public boolean equals(@Nullable Object other) {
117-
return (this == other || (other instanceof ApplicationListenerDetector &&
118-
this.applicationContext == ((ApplicationListenerDetector) other).applicationContext));
117+
return (this == other ||
118+
(other instanceof ApplicationListenerDetector applicationListenerDectector &&
119+
this.applicationContext == applicationListenerDectector.applicationContext));
119120
}
120121

121122
@Override

spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -74,11 +74,11 @@ public void setTimeoutPerShutdownPhase(long timeoutPerShutdownPhase) {
7474

7575
@Override
7676
public void setBeanFactory(BeanFactory beanFactory) {
77-
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
77+
if (!(beanFactory instanceof ConfigurableListableBeanFactory clbf)) {
7878
throw new IllegalArgumentException(
7979
"DefaultLifecycleProcessor requires a ConfigurableListableBeanFactory: " + beanFactory);
8080
}
81-
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
81+
this.beanFactory = clbf;
8282
}
8383

8484
private ConfigurableListableBeanFactory getBeanFactory() {
@@ -143,7 +143,7 @@ private void startBeans(boolean autoStartupOnly) {
143143
Map<Integer, LifecycleGroup> phases = new TreeMap<>();
144144

145145
lifecycleBeans.forEach((beanName, bean) -> {
146-
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
146+
if (!autoStartupOnly || (bean instanceof SmartLifecycle smartLifecycle && smartLifecycle.isAutoStartup())) {
147147
int phase = getPhase(bean);
148148
phases.computeIfAbsent(
149149
phase,
@@ -170,7 +170,7 @@ private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String bea
170170
doStart(lifecycleBeans, dependency, autoStartupOnly);
171171
}
172172
if (!bean.isRunning() &&
173-
(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
173+
(!autoStartupOnly || !(bean instanceof SmartLifecycle smartLifecycle) || smartLifecycle.isAutoStartup())) {
174174
if (logger.isTraceEnabled()) {
175175
logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
176176
}
@@ -225,13 +225,13 @@ private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final Strin
225225
}
226226
try {
227227
if (bean.isRunning()) {
228-
if (bean instanceof SmartLifecycle) {
228+
if (bean instanceof SmartLifecycle smartLifecycle) {
229229
if (logger.isTraceEnabled()) {
230230
logger.trace("Asking bean '" + beanName + "' of type [" +
231231
bean.getClass().getName() + "] to stop");
232232
}
233233
countDownBeanNames.add(beanName);
234-
((SmartLifecycle) bean).stop(() -> {
234+
smartLifecycle.stop(() -> {
235235
latch.countDown();
236236
countDownBeanNames.remove(beanName);
237237
if (logger.isDebugEnabled()) {
@@ -283,8 +283,8 @@ protected Map<String, Lifecycle> getLifecycleBeans() {
283283
(!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
284284
matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
285285
Object bean = beanFactory.getBean(beanNameToCheck);
286-
if (bean != this && bean instanceof Lifecycle) {
287-
beans.put(beanNameToRegister, (Lifecycle) bean);
286+
if (bean != this && bean instanceof Lifecycle lifecycle) {
287+
beans.put(beanNameToRegister, lifecycle);
288288
}
289289
}
290290
}
@@ -306,7 +306,7 @@ private boolean matchesBeanType(Class<?> targetType, String beanName, BeanFactor
306306
* @see SmartLifecycle
307307
*/
308308
protected int getPhase(Lifecycle bean) {
309-
return (bean instanceof Phased ? ((Phased) bean).getPhase() : 0);
309+
return (bean instanceof Phased phased ? phased.getPhase() : 0);
310310
}
311311

312312

spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ public Resource getResource(String location) {
272272
*/
273273
@Override
274274
public Resource[] getResources(String locationPattern) throws IOException {
275-
if (this.resourceLoader instanceof ResourcePatternResolver) {
276-
return ((ResourcePatternResolver) this.resourceLoader).getResources(locationPattern);
275+
if (this.resourceLoader instanceof ResourcePatternResolver resourcePatternResolver) {
276+
return resourcePatternResolver.getResources(locationPattern);
277277
}
278278
return super.getResources(locationPattern);
279279
}

spring-context/src/main/java/org/springframework/context/support/GenericGroovyApplicationContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -242,8 +242,8 @@ public Object invokeMethod(String name, Object args) {
242242

243243
@Override
244244
public void setProperty(String property, Object newValue) {
245-
if (newValue instanceof BeanDefinition) {
246-
registerBeanDefinition(property, (BeanDefinition) newValue);
245+
if (newValue instanceof BeanDefinition beanDefinition) {
246+
registerBeanDefinition(property, beanDefinition);
247247
}
248248
else {
249249
this.metaClass.setProperty(this, property, newValue);

spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ private static void sortPostProcessors(List<?> postProcessors, ConfigurableLista
323323
return;
324324
}
325325
Comparator<Object> comparatorToUse = null;
326-
if (beanFactory instanceof DefaultListableBeanFactory) {
327-
comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
326+
if (beanFactory instanceof DefaultListableBeanFactory dlbf) {
327+
comparatorToUse = dlbf.getDependencyComparator();
328328
}
329329
if (comparatorToUse == null) {
330330
comparatorToUse = OrderComparator.INSTANCE;
@@ -366,9 +366,9 @@ private static void invokeBeanFactoryPostProcessors(
366366
private static void registerBeanPostProcessors(
367367
ConfigurableListableBeanFactory beanFactory, List<? extends BeanPostProcessor> postProcessors) {
368368

369-
if (beanFactory instanceof AbstractBeanFactory) {
369+
if (beanFactory instanceof AbstractBeanFactory abstractBeanFactory) {
370370
// Bulk addition is more efficient against our CopyOnWriteArrayList there
371-
((AbstractBeanFactory) beanFactory).addBeanPostProcessors(postProcessors);
371+
abstractBeanFactory.addBeanPostProcessors(postProcessors);
372372
}
373373
else {
374374
for (BeanPostProcessor postProcessor : postProcessors) {

0 commit comments

Comments
 (0)