Apply 'instanceof pattern matching'
This commit is contained in:
parent
a832c98ced
commit
d32027df92
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -36,7 +36,7 @@ public class BeanExpressionContextAccessor implements PropertyAccessor {
|
|||
|
||||
@Override
|
||||
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
|
||||
return (target instanceof BeanExpressionContext && ((BeanExpressionContext) target).containsObject(name));
|
||||
return (target instanceof BeanExpressionContext bec && bec.containsObject(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -41,7 +41,7 @@ public class BeanFactoryAccessor implements PropertyAccessor {
|
|||
|
||||
@Override
|
||||
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
|
||||
return (target instanceof BeanFactory && ((BeanFactory) target).containsBean(name));
|
||||
return (target instanceof BeanFactory beanFactory && beanFactory.containsBean(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -395,8 +395,8 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
|||
|
||||
// Decorate event as an ApplicationEvent if necessary
|
||||
ApplicationEvent applicationEvent;
|
||||
if (event instanceof ApplicationEvent) {
|
||||
applicationEvent = (ApplicationEvent) event;
|
||||
if (event instanceof ApplicationEvent applEvent) {
|
||||
applicationEvent = applEvent;
|
||||
}
|
||||
else {
|
||||
applicationEvent = new PayloadApplicationEvent<>(this, event, eventType);
|
||||
|
|
@ -415,8 +415,8 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
|||
|
||||
// Publish event via parent context as well...
|
||||
if (this.parent != null) {
|
||||
if (this.parent instanceof AbstractApplicationContext) {
|
||||
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
|
||||
if (this.parent instanceof AbstractApplicationContext abstractApplicationContext) {
|
||||
abstractApplicationContext.publishEvent(event, eventType);
|
||||
}
|
||||
else {
|
||||
this.parent.publishEvent(event);
|
||||
|
|
@ -497,8 +497,8 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
|||
this.parent = parent;
|
||||
if (parent != null) {
|
||||
Environment parentEnvironment = parent.getEnvironment();
|
||||
if (parentEnvironment instanceof ConfigurableEnvironment) {
|
||||
getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
|
||||
if (parentEnvironment instanceof ConfigurableEnvironment configurableEnvironment) {
|
||||
getEnvironment().merge(configurableEnvironment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -770,12 +770,11 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
|||
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
|
||||
this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
|
||||
// Make MessageSource aware of parent MessageSource.
|
||||
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource hms) {
|
||||
if (hms.getParentMessageSource() == null) {
|
||||
// Only set parent context as parent MessageSource if no parent MessageSource
|
||||
// registered already.
|
||||
hms.setParentMessageSource(getInternalParentMessageSource());
|
||||
}
|
||||
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource hms &&
|
||||
hms.getParentMessageSource() == null) {
|
||||
// Only set parent context as parent MessageSource if no parent MessageSource
|
||||
// registered already.
|
||||
hms.setParentMessageSource(getInternalParentMessageSource());
|
||||
}
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Using MessageSource [" + this.messageSource + "]");
|
||||
|
|
@ -1350,8 +1349,8 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
|||
*/
|
||||
@Nullable
|
||||
protected BeanFactory getInternalParentBeanFactory() {
|
||||
return (getParent() instanceof ConfigurableApplicationContext ?
|
||||
((ConfigurableApplicationContext) getParent()).getBeanFactory() : getParent());
|
||||
return (getParent() instanceof ConfigurableApplicationContext cac ?
|
||||
cac.getBeanFactory() : getParent());
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1393,8 +1392,8 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
|||
*/
|
||||
@Nullable
|
||||
protected MessageSource getInternalParentMessageSource() {
|
||||
return (getParent() instanceof AbstractApplicationContext ?
|
||||
((AbstractApplicationContext) getParent()).messageSource : getParent());
|
||||
return (getParent() instanceof AbstractApplicationContext abstractApplicationContext ?
|
||||
abstractApplicationContext.messageSource : getParent());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -255,10 +255,10 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
|
|||
protected String getMessageFromParent(String code, @Nullable Object[] args, Locale locale) {
|
||||
MessageSource parent = getParentMessageSource();
|
||||
if (parent != null) {
|
||||
if (parent instanceof AbstractMessageSource) {
|
||||
if (parent instanceof AbstractMessageSource abstractMessageSource) {
|
||||
// Call internal method to avoid getting the default code back
|
||||
// in case of "useCodeAsDefaultMessage" being activated.
|
||||
return ((AbstractMessageSource) parent).getMessageInternal(code, args, locale);
|
||||
return abstractMessageSource.getMessageInternal(code, args, locale);
|
||||
}
|
||||
else {
|
||||
// Check parent MessageSource, returning null if not found there.
|
||||
|
|
@ -287,8 +287,8 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
|
|||
String defaultMessage = resolvable.getDefaultMessage();
|
||||
String[] codes = resolvable.getCodes();
|
||||
if (defaultMessage != null) {
|
||||
if (resolvable instanceof DefaultMessageSourceResolvable &&
|
||||
!((DefaultMessageSourceResolvable) resolvable).shouldRenderDefaultMessage()) {
|
||||
if (resolvable instanceof DefaultMessageSourceResolvable defaultMessageSourceResolvable &&
|
||||
!defaultMessageSourceResolvable.shouldRenderDefaultMessage()) {
|
||||
// Given default message does not contain any argument placeholders
|
||||
// (and isn't escaped for alwaysUseMessageFormat either) -> return as-is.
|
||||
return defaultMessage;
|
||||
|
|
@ -336,8 +336,8 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
|
|||
}
|
||||
List<Object> resolvedArgs = new ArrayList<>(args.length);
|
||||
for (Object arg : args) {
|
||||
if (arg instanceof MessageSourceResolvable) {
|
||||
resolvedArgs.add(getMessage((MessageSourceResolvable) arg, locale));
|
||||
if (arg instanceof MessageSourceResolvable messageSourceResolvable) {
|
||||
resolvedArgs.add(getMessage(messageSourceResolvable, locale));
|
||||
}
|
||||
else {
|
||||
resolvedArgs.add(arg);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.context.support;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.Aware;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.EmbeddedValueResolver;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
|
@ -47,6 +48,7 @@ import org.springframework.util.StringValueResolver;
|
|||
* @author Juergen Hoeller
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
* @author Sam Brannen
|
||||
* @since 10.10.2003
|
||||
* @see org.springframework.context.EnvironmentAware
|
||||
* @see org.springframework.context.EmbeddedValueResolverAware
|
||||
|
|
@ -87,26 +89,28 @@ class ApplicationContextAwareProcessor implements BeanPostProcessor {
|
|||
}
|
||||
|
||||
private void invokeAwareInterfaces(Object bean) {
|
||||
if (bean instanceof EnvironmentAware) {
|
||||
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
|
||||
}
|
||||
if (bean instanceof EmbeddedValueResolverAware) {
|
||||
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
|
||||
}
|
||||
if (bean instanceof ResourceLoaderAware) {
|
||||
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
|
||||
}
|
||||
if (bean instanceof ApplicationEventPublisherAware) {
|
||||
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
|
||||
}
|
||||
if (bean instanceof MessageSourceAware) {
|
||||
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
|
||||
}
|
||||
if (bean instanceof ApplicationStartupAware) {
|
||||
((ApplicationStartupAware) bean).setApplicationStartup(this.applicationContext.getApplicationStartup());
|
||||
}
|
||||
if (bean instanceof ApplicationContextAware) {
|
||||
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
|
||||
if (bean instanceof Aware) {
|
||||
if (bean instanceof EnvironmentAware environmentAware) {
|
||||
environmentAware.setEnvironment(this.applicationContext.getEnvironment());
|
||||
}
|
||||
if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
|
||||
embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
|
||||
}
|
||||
if (bean instanceof ResourceLoaderAware resourceLoaderAware) {
|
||||
resourceLoaderAware.setResourceLoader(this.applicationContext);
|
||||
}
|
||||
if (bean instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {
|
||||
applicationEventPublisherAware.setApplicationEventPublisher(this.applicationContext);
|
||||
}
|
||||
if (bean instanceof MessageSourceAware messageSourceAware) {
|
||||
messageSourceAware.setMessageSource(this.applicationContext);
|
||||
}
|
||||
if (bean instanceof ApplicationStartupAware applicationStartupAware) {
|
||||
applicationStartupAware.setApplicationStartup(this.applicationContext.getApplicationStartup());
|
||||
}
|
||||
if (bean instanceof ApplicationContextAware applicationContextAware) {
|
||||
applicationContextAware.setApplicationContext(this.applicationContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -71,12 +71,12 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
|
|||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) {
|
||||
if (bean instanceof ApplicationListener) {
|
||||
if (bean instanceof ApplicationListener<?> applicationListener) {
|
||||
// potentially not detected as a listener by getBeanNamesForType retrieval
|
||||
Boolean flag = this.singletonNames.get(beanName);
|
||||
if (Boolean.TRUE.equals(flag)) {
|
||||
// singleton bean (top-level or inner): register on the fly
|
||||
this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);
|
||||
this.applicationContext.addApplicationListener(applicationListener);
|
||||
}
|
||||
else if (Boolean.FALSE.equals(flag)) {
|
||||
if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
|
||||
|
|
@ -94,10 +94,10 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
|
|||
|
||||
@Override
|
||||
public void postProcessBeforeDestruction(Object bean, String beanName) {
|
||||
if (bean instanceof ApplicationListener) {
|
||||
if (bean instanceof ApplicationListener<?> applicationListener) {
|
||||
try {
|
||||
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
|
||||
multicaster.removeApplicationListener((ApplicationListener<?>) bean);
|
||||
multicaster.removeApplicationListener(applicationListener);
|
||||
multicaster.removeApplicationListenerBean(beanName);
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
|
|
@ -114,8 +114,9 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
|
|||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof ApplicationListenerDetector &&
|
||||
this.applicationContext == ((ApplicationListenerDetector) other).applicationContext));
|
||||
return (this == other ||
|
||||
(other instanceof ApplicationListenerDetector applicationListenerDectector &&
|
||||
this.applicationContext == applicationListenerDectector.applicationContext));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -74,11 +74,11 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
|
|||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
|
||||
if (!(beanFactory instanceof ConfigurableListableBeanFactory clbf)) {
|
||||
throw new IllegalArgumentException(
|
||||
"DefaultLifecycleProcessor requires a ConfigurableListableBeanFactory: " + beanFactory);
|
||||
}
|
||||
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
|
||||
this.beanFactory = clbf;
|
||||
}
|
||||
|
||||
private ConfigurableListableBeanFactory getBeanFactory() {
|
||||
|
|
@ -143,7 +143,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
|
|||
Map<Integer, LifecycleGroup> phases = new TreeMap<>();
|
||||
|
||||
lifecycleBeans.forEach((beanName, bean) -> {
|
||||
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
|
||||
if (!autoStartupOnly || (bean instanceof SmartLifecycle smartLifecycle && smartLifecycle.isAutoStartup())) {
|
||||
int phase = getPhase(bean);
|
||||
phases.computeIfAbsent(
|
||||
phase,
|
||||
|
|
@ -170,7 +170,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
|
|||
doStart(lifecycleBeans, dependency, autoStartupOnly);
|
||||
}
|
||||
if (!bean.isRunning() &&
|
||||
(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
|
||||
(!autoStartupOnly || !(bean instanceof SmartLifecycle smartLifecycle) || smartLifecycle.isAutoStartup())) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
|
||||
}
|
||||
|
|
@ -225,13 +225,13 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
|
|||
}
|
||||
try {
|
||||
if (bean.isRunning()) {
|
||||
if (bean instanceof SmartLifecycle) {
|
||||
if (bean instanceof SmartLifecycle smartLifecycle) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Asking bean '" + beanName + "' of type [" +
|
||||
bean.getClass().getName() + "] to stop");
|
||||
}
|
||||
countDownBeanNames.add(beanName);
|
||||
((SmartLifecycle) bean).stop(() -> {
|
||||
smartLifecycle.stop(() -> {
|
||||
latch.countDown();
|
||||
countDownBeanNames.remove(beanName);
|
||||
if (logger.isDebugEnabled()) {
|
||||
|
|
@ -283,8 +283,8 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
|
|||
(!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
|
||||
matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
|
||||
Object bean = beanFactory.getBean(beanNameToCheck);
|
||||
if (bean != this && bean instanceof Lifecycle) {
|
||||
beans.put(beanNameToRegister, (Lifecycle) bean);
|
||||
if (bean != this && bean instanceof Lifecycle lifecycle) {
|
||||
beans.put(beanNameToRegister, lifecycle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -306,7 +306,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
|
|||
* @see SmartLifecycle
|
||||
*/
|
||||
protected int getPhase(Lifecycle bean) {
|
||||
return (bean instanceof Phased ? ((Phased) bean).getPhase() : 0);
|
||||
return (bean instanceof Phased phased ? phased.getPhase() : 0);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -272,8 +272,8 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
|
|||
*/
|
||||
@Override
|
||||
public Resource[] getResources(String locationPattern) throws IOException {
|
||||
if (this.resourceLoader instanceof ResourcePatternResolver) {
|
||||
return ((ResourcePatternResolver) this.resourceLoader).getResources(locationPattern);
|
||||
if (this.resourceLoader instanceof ResourcePatternResolver resourcePatternResolver) {
|
||||
return resourcePatternResolver.getResources(locationPattern);
|
||||
}
|
||||
return super.getResources(locationPattern);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -242,8 +242,8 @@ public class GenericGroovyApplicationContext extends GenericApplicationContext i
|
|||
|
||||
@Override
|
||||
public void setProperty(String property, Object newValue) {
|
||||
if (newValue instanceof BeanDefinition) {
|
||||
registerBeanDefinition(property, (BeanDefinition) newValue);
|
||||
if (newValue instanceof BeanDefinition beanDefinition) {
|
||||
registerBeanDefinition(property, beanDefinition);
|
||||
}
|
||||
else {
|
||||
this.metaClass.setProperty(this, property, newValue);
|
||||
|
|
|
|||
|
|
@ -323,8 +323,8 @@ final class PostProcessorRegistrationDelegate {
|
|||
return;
|
||||
}
|
||||
Comparator<Object> comparatorToUse = null;
|
||||
if (beanFactory instanceof DefaultListableBeanFactory) {
|
||||
comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
|
||||
if (beanFactory instanceof DefaultListableBeanFactory dlbf) {
|
||||
comparatorToUse = dlbf.getDependencyComparator();
|
||||
}
|
||||
if (comparatorToUse == null) {
|
||||
comparatorToUse = OrderComparator.INSTANCE;
|
||||
|
|
@ -366,9 +366,9 @@ final class PostProcessorRegistrationDelegate {
|
|||
private static void registerBeanPostProcessors(
|
||||
ConfigurableListableBeanFactory beanFactory, List<? extends BeanPostProcessor> postProcessors) {
|
||||
|
||||
if (beanFactory instanceof AbstractBeanFactory) {
|
||||
if (beanFactory instanceof AbstractBeanFactory abstractBeanFactory) {
|
||||
// Bulk addition is more efficient against our CopyOnWriteArrayList there
|
||||
((AbstractBeanFactory) beanFactory).addBeanPostProcessors(postProcessors);
|
||||
abstractBeanFactory.addBeanPostProcessors(postProcessors);
|
||||
}
|
||||
else {
|
||||
for (BeanPostProcessor postProcessor : postProcessors) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -534,8 +534,8 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased
|
|||
*/
|
||||
public void clearCacheIncludingAncestors() {
|
||||
clearCache();
|
||||
if (getParentMessageSource() instanceof ReloadableResourceBundleMessageSource) {
|
||||
((ReloadableResourceBundleMessageSource) getParentMessageSource()).clearCacheIncludingAncestors();
|
||||
if (getParentMessageSource() instanceof ReloadableResourceBundleMessageSource reloadableMsgSrc) {
|
||||
reloadableMsgSrc.clearCacheIncludingAncestors();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue