diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java b/spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java index e891b58a240..49bf01e26d1 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -88,22 +88,22 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria private Collection parseCacheAnnotations( DefaultCacheConfig cachingConfig, AnnotatedElement ae, boolean localOnly) { - Collection anns = (localOnly ? + Collection annotations = (localOnly ? AnnotatedElementUtils.getAllMergedAnnotations(ae, CACHE_OPERATION_ANNOTATIONS) : AnnotatedElementUtils.findAllMergedAnnotations(ae, CACHE_OPERATION_ANNOTATIONS)); - if (anns.isEmpty()) { + if (annotations.isEmpty()) { return null; } - final Collection ops = new ArrayList<>(1); - anns.stream().filter(ann -> ann instanceof Cacheable).forEach( - ann -> ops.add(parseCacheableAnnotation(ae, cachingConfig, (Cacheable) ann))); - anns.stream().filter(ann -> ann instanceof CacheEvict).forEach( - ann -> ops.add(parseEvictAnnotation(ae, cachingConfig, (CacheEvict) ann))); - anns.stream().filter(ann -> ann instanceof CachePut).forEach( - ann -> ops.add(parsePutAnnotation(ae, cachingConfig, (CachePut) ann))); - anns.stream().filter(ann -> ann instanceof Caching).forEach( - ann -> parseCachingAnnotation(ae, cachingConfig, (Caching) ann, ops)); + Collection ops = new ArrayList<>(1); + annotations.stream().filter(Cacheable.class::isInstance).map(Cacheable.class::cast).forEach( + cacheable -> ops.add(parseCacheableAnnotation(ae, cachingConfig, cacheable))); + annotations.stream().filter(CacheEvict.class::isInstance).map(CacheEvict.class::cast).forEach( + cacheEvict -> ops.add(parseEvictAnnotation(ae, cachingConfig, cacheEvict))); + annotations.stream().filter(CachePut.class::isInstance).map(CachePut.class::cast).forEach( + cachePut -> ops.add(parsePutAnnotation(ae, cachingConfig, cachePut))); + annotations.stream().filter(Caching.class::isInstance).map(Caching.class::cast).forEach( + caching -> parseCachingAnnotation(ae, cachingConfig, caching, ops)); return ops; } diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index f29dfe3395f..572d3295db2 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -772,11 +772,11 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker protected boolean canPutToCache(@Nullable Object value) { String unless = ""; - if (this.metadata.operation instanceof CacheableOperation) { - unless = ((CacheableOperation) this.metadata.operation).getUnless(); + if (this.metadata.operation instanceof CacheableOperation cacheableOperation) { + unless = cacheableOperation.getUnless(); } - else if (this.metadata.operation instanceof CachePutOperation) { - unless = ((CachePutOperation) this.metadata.operation).getUnless(); + else if (this.metadata.operation instanceof CachePutOperation cachePutOperation) { + unless = cachePutOperation.getUnless(); } if (StringUtils.hasText(unless)) { EvaluationContext evaluationContext = createEvaluationContext(value); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java index bc5ad2a4282..0203485ce4b 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -235,11 +235,11 @@ public abstract class AnnotationConfigUtils { @Nullable private static DefaultListableBeanFactory unwrapDefaultListableBeanFactory(BeanDefinitionRegistry registry) { - if (registry instanceof DefaultListableBeanFactory) { - return (DefaultListableBeanFactory) registry; + if (registry instanceof DefaultListableBeanFactory dlbf) { + return dlbf; } - else if (registry instanceof GenericApplicationContext) { - return ((GenericApplicationContext) registry).getDefaultListableBeanFactory(); + else if (registry instanceof GenericApplicationContext gac) { + return gac.getDefaultListableBeanFactory(); } else { return null; diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index 94995830b97..0a1f3667b05 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -370,8 +370,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo // Detect any custom bean name generation strategy supplied through the enclosing application context SingletonBeanRegistry sbr = null; - if (registry instanceof SingletonBeanRegistry) { - sbr = (SingletonBeanRegistry) registry; + if (registry instanceof SingletonBeanRegistry _sbr) { + sbr = _sbr; if (!this.localBeanNameGeneratorSet) { BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton( AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java index b957179e548..acfd6f6c3bb 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -101,15 +101,15 @@ public abstract class ConfigurationClassUtils { } AnnotationMetadata metadata; - if (beanDef instanceof AnnotatedBeanDefinition && - className.equals(((AnnotatedBeanDefinition) beanDef).getMetadata().getClassName())) { + if (beanDef instanceof AnnotatedBeanDefinition annotatedBd && + className.equals(annotatedBd.getMetadata().getClassName())) { // Can reuse the pre-parsed metadata from the given BeanDefinition... - metadata = ((AnnotatedBeanDefinition) beanDef).getMetadata(); + metadata = annotatedBd.getMetadata(); } - else if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) beanDef).hasBeanClass()) { + else if (beanDef instanceof AbstractBeanDefinition abstractBd && abstractBd.hasBeanClass()) { // Check already loaded Class if present... // since we possibly can't even load the class file for this Class. - Class beanClass = ((AbstractBeanDefinition) beanDef).getBeanClass(); + Class beanClass = abstractBd.getBeanClass(); if (BeanFactoryPostProcessor.class.isAssignableFrom(beanClass) || BeanPostProcessor.class.isAssignableFrom(beanClass) || AopInfrastructureBean.class.isAssignableFrom(beanClass) || diff --git a/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java b/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java index 16a1640fd12..1fea6e198da 100644 --- a/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -184,8 +184,8 @@ public class EventListenerMethodProcessor Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName)); ApplicationListener applicationListener = factory.createApplicationListener(beanName, targetType, methodToUse); - if (applicationListener instanceof ApplicationListenerMethodAdapter) { - ((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator); + if (applicationListener instanceof ApplicationListenerMethodAdapter alma) { + alma.init(context, this.evaluator); } context.addApplicationListener(applicationListener); break; diff --git a/spring-context/src/main/java/org/springframework/context/event/GenericApplicationListenerAdapter.java b/spring-context/src/main/java/org/springframework/context/event/GenericApplicationListenerAdapter.java index b48352f0b6a..a0ac143fa30 100644 --- a/spring-context/src/main/java/org/springframework/context/event/GenericApplicationListenerAdapter.java +++ b/spring-context/src/main/java/org/springframework/context/event/GenericApplicationListenerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -67,12 +67,12 @@ public class GenericApplicationListenerAdapter implements GenericApplicationList @Override @SuppressWarnings("unchecked") public boolean supportsEventType(ResolvableType eventType) { - if (this.delegate instanceof GenericApplicationListener) { - return ((GenericApplicationListener) this.delegate).supportsEventType(eventType); + if (this.delegate instanceof GenericApplicationListener gal) { + return gal.supportsEventType(eventType); } - else if (this.delegate instanceof SmartApplicationListener) { + else if (this.delegate instanceof SmartApplicationListener sal) { Class eventClass = (Class) eventType.resolve(); - return (eventClass != null && ((SmartApplicationListener) this.delegate).supportsEventType(eventClass)); + return (eventClass != null && sal.supportsEventType(eventClass)); } else { return (this.declaredEventType == null || this.declaredEventType.isAssignableFrom(eventType)); @@ -81,19 +81,17 @@ public class GenericApplicationListenerAdapter implements GenericApplicationList @Override public boolean supportsSourceType(@Nullable Class sourceType) { - return !(this.delegate instanceof SmartApplicationListener) || - ((SmartApplicationListener) this.delegate).supportsSourceType(sourceType); + return (!(this.delegate instanceof SmartApplicationListener sal) || sal.supportsSourceType(sourceType)); } @Override public int getOrder() { - return (this.delegate instanceof Ordered ? ((Ordered) this.delegate).getOrder() : Ordered.LOWEST_PRECEDENCE); + return (this.delegate instanceof Ordered ordered ? ordered.getOrder() : Ordered.LOWEST_PRECEDENCE); } @Override public String getListenerId() { - return (this.delegate instanceof SmartApplicationListener ? - ((SmartApplicationListener) this.delegate).getListenerId() : ""); + return (this.delegate instanceof SmartApplicationListener sal ? sal.getListenerId() : ""); } diff --git a/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java b/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java index a62b2dc839d..c84e201d2f2 100644 --- a/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java +++ b/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java @@ -178,8 +178,8 @@ public class SimpleApplicationEventMulticaster extends AbstractApplicationEventM catch (ClassCastException ex) { String msg = ex.getMessage(); if (msg == null || matchesClassCastMessage(msg, event.getClass()) || - (event instanceof PayloadApplicationEvent && - matchesClassCastMessage(msg, ((PayloadApplicationEvent) event).getPayload().getClass()))) { + (event instanceof PayloadApplicationEvent payloadEvent && + matchesClassCastMessage(msg, payloadEvent.getPayload().getClass()))) { // Possibly a lambda-defined listener which we could not resolve the generic event type for // -> let's suppress the exception. Log loggerToUse = this.lazyLogger; diff --git a/spring-context/src/main/java/org/springframework/context/event/SourceFilteringListener.java b/spring-context/src/main/java/org/springframework/context/event/SourceFilteringListener.java index a2593444ea7..7170931aaa0 100644 --- a/spring-context/src/main/java/org/springframework/context/event/SourceFilteringListener.java +++ b/spring-context/src/main/java/org/springframework/context/event/SourceFilteringListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -51,8 +51,8 @@ public class SourceFilteringListener implements GenericApplicationListener { */ public SourceFilteringListener(Object source, ApplicationListener delegate) { this.source = source; - this.delegate = (delegate instanceof GenericApplicationListener ? - (GenericApplicationListener) delegate : new GenericApplicationListenerAdapter(delegate)); + this.delegate = (delegate instanceof GenericApplicationListener gal ? gal : + new GenericApplicationListenerAdapter(delegate)); } /** diff --git a/spring-context/src/main/java/org/springframework/context/i18n/LocaleContextHolder.java b/spring-context/src/main/java/org/springframework/context/i18n/LocaleContextHolder.java index a9a05a29108..97a2a2f1ec4 100644 --- a/spring-context/src/main/java/org/springframework/context/i18n/LocaleContextHolder.java +++ b/spring-context/src/main/java/org/springframework/context/i18n/LocaleContextHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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. @@ -152,8 +152,8 @@ public final class LocaleContextHolder { */ public static void setLocale(@Nullable Locale locale, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); - TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? - ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); + TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext timeZoneAware ? + timeZoneAware.getTimeZone() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } @@ -324,8 +324,8 @@ public final class LocaleContextHolder { * @see java.util.TimeZone#getDefault() */ public static TimeZone getTimeZone(@Nullable LocaleContext localeContext) { - if (localeContext instanceof TimeZoneAwareLocaleContext) { - TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); + if (localeContext instanceof TimeZoneAwareLocaleContext timeZoneAware) { + TimeZone timeZone = timeZoneAware.getTimeZone(); if (timeZone != null) { return timeZone; } diff --git a/spring-context/src/main/java/org/springframework/context/weaving/DefaultContextLoadTimeWeaver.java b/spring-context/src/main/java/org/springframework/context/weaving/DefaultContextLoadTimeWeaver.java index 7c558c70890..9a815627fb0 100644 --- a/spring-context/src/main/java/org/springframework/context/weaving/DefaultContextLoadTimeWeaver.java +++ b/spring-context/src/main/java/org/springframework/context/weaving/DefaultContextLoadTimeWeaver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -128,12 +128,12 @@ public class DefaultContextLoadTimeWeaver implements LoadTimeWeaver, BeanClassLo @Override public void destroy() { - if (this.loadTimeWeaver instanceof InstrumentationLoadTimeWeaver) { + if (this.loadTimeWeaver instanceof InstrumentationLoadTimeWeaver iltw) { if (logger.isDebugEnabled()) { logger.debug("Removing all registered transformers for class loader: " + this.loadTimeWeaver.getInstrumentableClassLoader().getClass().getName()); } - ((InstrumentationLoadTimeWeaver) this.loadTimeWeaver).removeTransformers(); + iltw.removeTransformers(); } } diff --git a/spring-context/src/main/java/org/springframework/context/weaving/LoadTimeWeaverAwareProcessor.java b/spring-context/src/main/java/org/springframework/context/weaving/LoadTimeWeaverAwareProcessor.java index 7094ebdb639..bb045de9a68 100644 --- a/spring-context/src/main/java/org/springframework/context/weaving/LoadTimeWeaverAwareProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/weaving/LoadTimeWeaverAwareProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2023 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. @@ -92,7 +92,7 @@ public class LoadTimeWeaverAwareProcessor implements BeanPostProcessor, BeanFact @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - if (bean instanceof LoadTimeWeaverAware) { + if (bean instanceof LoadTimeWeaverAware loadTimeWeaverAware) { LoadTimeWeaver ltw = this.loadTimeWeaver; if (ltw == null) { Assert.state(this.beanFactory != null, @@ -100,7 +100,7 @@ public class LoadTimeWeaverAwareProcessor implements BeanPostProcessor, BeanFact ltw = this.beanFactory.getBean( ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, LoadTimeWeaver.class); } - ((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw); + loadTimeWeaverAware.setLoadTimeWeaver(ltw); } return bean; } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java index f64cdcb0539..9e2f2668ec9 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -96,8 +96,8 @@ public class DateTimeContext { } else { LocaleContext localeContext = LocaleContextHolder.getLocaleContext(); - if (localeContext instanceof TimeZoneAwareLocaleContext) { - TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); + if (localeContext instanceof TimeZoneAwareLocaleContext timeZoneAware) { + TimeZone timeZone = timeZoneAware.getTimeZone(); if (timeZone != null) { formatter = formatter.withZone(timeZone.toZoneId()); } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java index 0295c4af291..1d4f04e55c2 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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. @@ -77,8 +77,8 @@ final class DateTimeConverters { } private static ZonedDateTime calendarToZonedDateTime(Calendar source) { - if (source instanceof GregorianCalendar) { - return ((GregorianCalendar) source).toZonedDateTime(); + if (source instanceof GregorianCalendar gc) { + return gc.toZonedDateTime(); } else { return ZonedDateTime.ofInstant(Instant.ofEpochMilli(source.getTimeInMillis()), diff --git a/spring-context/src/main/java/org/springframework/format/number/PercentStyleFormatter.java b/spring-context/src/main/java/org/springframework/format/number/PercentStyleFormatter.java index 0d8fe6788af..95c2db45dba 100644 --- a/spring-context/src/main/java/org/springframework/format/number/PercentStyleFormatter.java +++ b/spring-context/src/main/java/org/springframework/format/number/PercentStyleFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2023 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. @@ -37,8 +37,8 @@ public class PercentStyleFormatter extends AbstractNumberFormatter { @Override protected NumberFormat getNumberFormat(Locale locale) { NumberFormat format = NumberFormat.getPercentInstance(locale); - if (format instanceof DecimalFormat) { - ((DecimalFormat) format).setParseBigDecimal(true); + if (format instanceof DecimalFormat decimalFormat) { + decimalFormat.setParseBigDecimal(true); } return format; } diff --git a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java index b4e8ad7f729..e69d4bdc3a2 100644 --- a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java +++ b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -117,9 +117,8 @@ public class FormattingConversionService extends GenericConversionService private static Class getFieldType(T instance, Class genericInterface) { Class fieldType = GenericTypeResolver.resolveTypeArgument(instance.getClass(), genericInterface); - if (fieldType == null && instance instanceof DecoratingProxy) { - fieldType = GenericTypeResolver.resolveTypeArgument( - ((DecoratingProxy) instance).getDecoratedClass(), genericInterface); + if (fieldType == null && instance instanceof DecoratingProxy decoratingProxy) { + fieldType = GenericTypeResolver.resolveTypeArgument(decoratingProxy.getDecoratedClass(), genericInterface); } Assert.notNull(fieldType, () -> "Unable to extract the parameterized field type from " + ClassUtils.getShortName(genericInterface) + " [" + instance.getClass().getName() + diff --git a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java index 5644f8012b0..4ac53b00911 100644 --- a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java +++ b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2023 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. @@ -145,12 +145,12 @@ public class FormattingConversionServiceFactoryBean private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { - for (Object formatter : this.formatters) { - if (formatter instanceof Formatter) { - conversionService.addFormatter((Formatter) formatter); + for (Object candidate : this.formatters) { + if (candidate instanceof Formatter formatter) { + conversionService.addFormatter(formatter); } - else if (formatter instanceof AnnotationFormatterFactory) { - conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory) formatter); + else if (candidate instanceof AnnotationFormatterFactory factory) { + conversionService.addFormatterForFieldAnnotation(factory); } else { throw new IllegalArgumentException( diff --git a/spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java b/spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java index 1da77e14d4a..3b5b18e3734 100644 --- a/spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java +++ b/spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -441,11 +441,11 @@ public class MBeanClientInterceptor catch (RuntimeOperationsException ex) { // This one is only thrown by the JMX 1.2 RI, not by the JDK 1.5 JMX code. RuntimeException rex = ex.getTargetException(); - if (rex instanceof RuntimeMBeanException) { - throw ((RuntimeMBeanException) rex).getTargetException(); + if (rex instanceof RuntimeMBeanException runtimeMBeanException) { + throw runtimeMBeanException.getTargetException(); } - else if (rex instanceof RuntimeErrorException) { - throw ((RuntimeErrorException) rex).getTargetError(); + else if (rex instanceof RuntimeErrorException runtimeErrorException) { + throw runtimeErrorException.getTargetError(); } else { throw rex; diff --git a/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java b/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java index 13c5259a59f..f4ce6bc207d 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java @@ -402,8 +402,8 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo */ @Override public void setBeanFactory(BeanFactory beanFactory) { - if (beanFactory instanceof ListableBeanFactory) { - this.beanFactory = (ListableBeanFactory) beanFactory; + if (beanFactory instanceof ListableBeanFactory lbf) { + this.beanFactory = lbf; } else { logger.debug("MBeanExporter not running in a ListableBeanFactory: autodetection of MBeans not available."); @@ -543,8 +543,8 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo } // Allow the assembler a chance to vote for bean inclusion. if ((mode == AUTODETECT_ASSEMBLER || mode == AUTODETECT_ALL) && - this.assembler instanceof AutodetectCapableMBeanInfoAssembler) { - autodetect(this.beans, ((AutodetectCapableMBeanInfoAssembler) this.assembler)::includeBean); + this.assembler instanceof AutodetectCapableMBeanInfoAssembler autodetectCapableAssembler) { + autodetect(this.beans, autodetectCapableAssembler::includeBean); } } @@ -561,8 +561,8 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo * @see org.springframework.beans.factory.config.BeanDefinition#isLazyInit */ protected boolean isBeanDefinitionLazyInit(ListableBeanFactory beanFactory, String beanName) { - return (beanFactory instanceof ConfigurableListableBeanFactory && beanFactory.containsBeanDefinition(beanName) && - ((ConfigurableListableBeanFactory) beanFactory).getBeanDefinition(beanName).isLazyInit()); + return (beanFactory instanceof ConfigurableListableBeanFactory clbf && beanFactory.containsBeanDefinition(beanName) && + clbf.getBeanDefinition(beanName).isLazyInit()); } /** @@ -748,8 +748,8 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo * if the retrieved {@code ObjectName} is malformed */ protected ObjectName getObjectName(Object bean, @Nullable String beanKey) throws MalformedObjectNameException { - if (bean instanceof SelfNaming) { - return ((SelfNaming) bean).getObjectName(); + if (bean instanceof SelfNaming selfNaming) { + return selfNaming.getObjectName(); } else { return this.namingStrategy.getObjectName(bean, beanKey); @@ -869,8 +869,8 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo Assert.state(this.beanFactory != null, "No BeanFactory set"); Set beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount()); Collections.addAll(beanNames, this.beanFactory.getBeanDefinitionNames()); - if (this.beanFactory instanceof ConfigurableBeanFactory) { - Collections.addAll(beanNames, ((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()); + if (this.beanFactory instanceof ConfigurableBeanFactory cbf) { + Collections.addAll(beanNames, cbf.getSingletonNames()); } for (String beanName : beanNames) { @@ -925,8 +925,8 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo * Return whether the specified bean definition should be considered as abstract. */ private boolean isBeanDefinitionAbstract(ListableBeanFactory beanFactory, String beanName) { - return (beanFactory instanceof ConfigurableListableBeanFactory && beanFactory.containsBeanDefinition(beanName) && - ((ConfigurableListableBeanFactory) beanFactory).getBeanDefinition(beanName).isAbstract()); + return (beanFactory instanceof ConfigurableListableBeanFactory clbf && beanFactory.containsBeanDefinition(beanName) && + clbf.getBeanDefinition(beanName).isAbstract()); } @@ -941,9 +941,8 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo private void injectNotificationPublisherIfNecessary( Object managedResource, @Nullable ModelMBean modelMBean, @Nullable ObjectName objectName) { - if (managedResource instanceof NotificationPublisherAware && modelMBean != null && objectName != null) { - ((NotificationPublisherAware) managedResource).setNotificationPublisher( - new ModelMBeanNotificationPublisher(modelMBean, objectName, managedResource)); + if (managedResource instanceof NotificationPublisherAware npa && modelMBean != null && objectName != null) { + npa.setNotificationPublisher(new ModelMBeanNotificationPublisher(modelMBean, objectName, managedResource)); } } diff --git a/spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java b/spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java index 9f9f6381ca4..975b40be7c5 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -66,8 +66,8 @@ public class AnnotationJmxAttributeSource implements JmxAttributeSource, BeanFac @Override public void setBeanFactory(BeanFactory beanFactory) { - if (beanFactory instanceof ConfigurableBeanFactory) { - this.embeddedValueResolver = new EmbeddedValueResolver((ConfigurableBeanFactory) beanFactory); + if (beanFactory instanceof ConfigurableBeanFactory cbf) { + this.embeddedValueResolver = new EmbeddedValueResolver(cbf); } } @@ -92,8 +92,8 @@ public class AnnotationJmxAttributeSource implements JmxAttributeSource, BeanFac map.forEach((attrName, attrValue) -> { if (!"value".equals(attrName)) { Object value = attrValue; - if (this.embeddedValueResolver != null && value instanceof String) { - value = this.embeddedValueResolver.resolveStringValue((String) value); + if (this.embeddedValueResolver != null && value instanceof String text) { + value = this.embeddedValueResolver.resolveStringValue(text); } list.add(new PropertyValue(attrName, value)); } diff --git a/spring-context/src/main/java/org/springframework/jmx/export/notification/ModelMBeanNotificationPublisher.java b/spring-context/src/main/java/org/springframework/jmx/export/notification/ModelMBeanNotificationPublisher.java index 6728204e1e0..8f41cdd3973 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/notification/ModelMBeanNotificationPublisher.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/notification/ModelMBeanNotificationPublisher.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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. @@ -90,8 +90,8 @@ public class ModelMBeanNotificationPublisher implements NotificationPublisher { Assert.notNull(notification, "Notification must not be null"); replaceNotificationSourceIfNecessary(notification); try { - if (notification instanceof AttributeChangeNotification) { - this.modelMBean.sendAttributeChangeNotification((AttributeChangeNotification) notification); + if (notification instanceof AttributeChangeNotification acn) { + this.modelMBean.sendAttributeChangeNotification(acn); } else { this.modelMBean.sendNotification(notification); diff --git a/spring-context/src/main/java/org/springframework/jmx/support/ObjectNameManager.java b/spring-context/src/main/java/org/springframework/jmx/support/ObjectNameManager.java index a3941b16a06..9f79280418a 100644 --- a/spring-context/src/main/java/org/springframework/jmx/support/ObjectNameManager.java +++ b/spring-context/src/main/java/org/springframework/jmx/support/ObjectNameManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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,23 +36,22 @@ public final class ObjectNameManager { /** - * Retrieve the {@code ObjectName} instance corresponding to the supplied name. - * @param objectName the {@code ObjectName} in {@code ObjectName} or - * {@code String} format + * Retrieve the {@link ObjectName} instance corresponding to the supplied name. + * @param name the name in {@code ObjectName} or {@code String} format * @return the {@code ObjectName} instance * @throws MalformedObjectNameException in case of an invalid object name specification * @see ObjectName#ObjectName(String) * @see ObjectName#getInstance(String) */ - public static ObjectName getInstance(Object objectName) throws MalformedObjectNameException { - if (objectName instanceof ObjectName) { - return (ObjectName) objectName; + public static ObjectName getInstance(Object name) throws MalformedObjectNameException { + if (name instanceof ObjectName objectName) { + return objectName; } - if (!(objectName instanceof String)) { + if (!(name instanceof String text)) { throw new MalformedObjectNameException("Invalid ObjectName value type [" + - objectName.getClass().getName() + "]: only ObjectName and String supported."); + name.getClass().getName() + "]: only ObjectName and String supported."); } - return getInstance((String) objectName); + return getInstance(text); } /** diff --git a/spring-context/src/main/java/org/springframework/jndi/JndiObjectFactoryBean.java b/spring-context/src/main/java/org/springframework/jndi/JndiObjectFactoryBean.java index 9f1b49f8ee0..a237e6c731e 100644 --- a/spring-context/src/main/java/org/springframework/jndi/JndiObjectFactoryBean.java +++ b/spring-context/src/main/java/org/springframework/jndi/JndiObjectFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 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. @@ -178,10 +178,10 @@ public class JndiObjectFactoryBean extends JndiObjectLocator @Override public void setBeanFactory(BeanFactory beanFactory) { - if (beanFactory instanceof ConfigurableBeanFactory) { + if (beanFactory instanceof ConfigurableBeanFactory cbf) { // Just optional - for getting a specifically configured TypeConverter if needed. // We'll simply fall back to a SimpleTypeConverter if no specific one available. - this.beanFactory = (ConfigurableBeanFactory) beanFactory; + this.beanFactory = cbf; } } diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java index 12d92efc4d5..6cad78b46dd 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -139,8 +139,8 @@ public class AsyncAnnotationAdvisor extends AbstractPointcutAdvisor implements B */ @Override public void setBeanFactory(BeanFactory beanFactory) { - if (this.advice instanceof BeanFactoryAware) { - ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory); + if (this.advice instanceof BeanFactoryAware beanFactoryAware) { + beanFactoryAware.setBeanFactory(beanFactory); } } diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncResult.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncResult.java index fb72172b405..784a3e3f1ca 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncResult.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncResult.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -94,8 +94,7 @@ public class AsyncResult implements ListenableFuture { @Nullable public V get() throws ExecutionException { if (this.executionException != null) { - throw (this.executionException instanceof ExecutionException ? - (ExecutionException) this.executionException : + throw (this.executionException instanceof ExecutionException execEx ? execEx : new ExecutionException(this.executionException)); } return this.value; diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java index 5b3426f6cfa..14272d35e77 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -244,9 +244,8 @@ public class ScheduledAnnotationBeanPostProcessor this.registrar.setScheduler(this.scheduler); } - if (this.beanFactory instanceof ListableBeanFactory) { - Map beans = - ((ListableBeanFactory) this.beanFactory).getBeansOfType(SchedulingConfigurer.class); + if (this.beanFactory instanceof ListableBeanFactory lbf) { + Map beans = lbf.getBeansOfType(SchedulingConfigurer.class); List configurers = new ArrayList<>(beans.values()); AnnotationAwareOrderComparator.sort(configurers); for (SchedulingConfigurer configurer : configurers) { @@ -322,16 +321,15 @@ public class ScheduledAnnotationBeanPostProcessor private T resolveSchedulerBean(BeanFactory beanFactory, Class schedulerType, boolean byName) { if (byName) { T scheduler = beanFactory.getBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, schedulerType); - if (this.beanName != null && this.beanFactory instanceof ConfigurableBeanFactory) { - ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean( - DEFAULT_TASK_SCHEDULER_BEAN_NAME, this.beanName); + if (this.beanName != null && this.beanFactory instanceof ConfigurableBeanFactory cbf) { + cbf.registerDependentBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, this.beanName); } return scheduler; } - else if (beanFactory instanceof AutowireCapableBeanFactory) { - NamedBeanHolder holder = ((AutowireCapableBeanFactory) beanFactory).resolveNamedBean(schedulerType); - if (this.beanName != null && beanFactory instanceof ConfigurableBeanFactory) { - ((ConfigurableBeanFactory) beanFactory).registerDependentBean(holder.getBeanName(), this.beanName); + else if (beanFactory instanceof AutowireCapableBeanFactory acbf) { + NamedBeanHolder holder = acbf.resolveNamedBean(schedulerType); + if (this.beanName != null && beanFactory instanceof ConfigurableBeanFactory cbf) { + cbf.registerDependentBean(holder.getBeanName(), this.beanName); } return holder.getBeanInstance(); } diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskExecutor.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskExecutor.java index 8482cb14a3e..17301854989 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskExecutor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -231,10 +231,10 @@ public class ConcurrentTaskExecutor implements AsyncListenableTaskExecutor, Sche public static Runnable buildManagedTask(Runnable task, String identityName) { Map properties; - if (task instanceof SchedulingAwareRunnable) { + if (task instanceof SchedulingAwareRunnable schedulingAwareRunnable) { properties = new HashMap<>(4); properties.put(ManagedTask.LONGRUNNING_HINT, - Boolean.toString(((SchedulingAwareRunnable) task).isLongLived())); + Boolean.toString(schedulingAwareRunnable.isLongLived())); } else { properties = new HashMap<>(2); diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java index dc9a249bdd9..17e76d01900 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -239,8 +239,8 @@ public abstract class ExecutorConfigurationSupport extends CustomizableThreadFac * @see RunnableFuture#cancel(boolean) */ protected void cancelRemainingTask(Runnable task) { - if (task instanceof Future) { - ((Future) task).cancel(true); + if (task instanceof Future future) { + future.cancel(true); } } diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ScheduledExecutorFactoryBean.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ScheduledExecutorFactoryBean.java index 66103aa980e..9f01593d335 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ScheduledExecutorFactoryBean.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ScheduledExecutorFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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. @@ -38,7 +38,7 @@ import org.springframework.util.ObjectUtils; * *

Allows for registration of {@link ScheduledExecutorTask ScheduledExecutorTasks}, * automatically starting the {@link ScheduledExecutorService} on initialization and - * cancelling it on destruction of the context. In scenarios that only require static + * canceling it on destruction of the context. In scenarios that only require static * registration of tasks at startup, there is no need to access the * {@link ScheduledExecutorService} instance itself in application code at all; * {@code ScheduledExecutorFactoryBean} is then just being used for lifecycle integration. @@ -153,8 +153,8 @@ public class ScheduledExecutorFactoryBean extends ExecutorConfigurationSupport createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler); if (this.removeOnCancelPolicy) { - if (executor instanceof ScheduledThreadPoolExecutor) { - ((ScheduledThreadPoolExecutor) executor).setRemoveOnCancelPolicy(true); + if (executor instanceof ScheduledThreadPoolExecutor threadPoolExecutor) { + threadPoolExecutor.setRemoveOnCancelPolicy(true); } else { logger.debug("Could not apply remove-on-cancel policy - not a ScheduledThreadPoolExecutor"); diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java index ba6d1b8bbba..144b2962ecf 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -423,8 +423,8 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport super.cancelRemainingTask(task); // Cancel associated user-level Future handle as well Object original = this.decoratedTaskMap.get(task); - if (original instanceof Future) { - ((Future) original).cancel(true); + if (original instanceof Future future) { + future.cancel(true); } } diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskScheduler.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskScheduler.java index f779bd9bb25..42f0e5bcfab 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskScheduler.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskScheduler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -91,8 +91,8 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport */ public void setPoolSize(int poolSize) { Assert.isTrue(poolSize > 0, "'poolSize' must be 1 or higher"); - if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { - ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setCorePoolSize(poolSize); + if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor threadPoolExecutor) { + threadPoolExecutor.setCorePoolSize(poolSize); } this.poolSize = poolSize; } @@ -105,8 +105,8 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport * @see ScheduledThreadPoolExecutor#setRemoveOnCancelPolicy */ public void setRemoveOnCancelPolicy(boolean flag) { - if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { - ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(flag); + if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor threadPoolExecutor) { + threadPoolExecutor.setRemoveOnCancelPolicy(flag); } this.removeOnCancelPolicy = flag; } @@ -120,8 +120,8 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport * @see ScheduledThreadPoolExecutor#setContinueExistingPeriodicTasksAfterShutdownPolicy */ public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean flag) { - if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { - ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setContinueExistingPeriodicTasksAfterShutdownPolicy(flag); + if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor threadPoolExecutor) { + threadPoolExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(flag); } this.continueExistingPeriodicTasksAfterShutdownPolicy = flag; } @@ -135,8 +135,8 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport * @see ScheduledThreadPoolExecutor#setExecuteExistingDelayedTasksAfterShutdownPolicy */ public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean flag) { - if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { - ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setExecuteExistingDelayedTasksAfterShutdownPolicy(flag); + if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor threadPoolExecutor) { + threadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(flag); } this.executeExistingDelayedTasksAfterShutdownPolicy = flag; } @@ -170,15 +170,15 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler); - if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor scheduledPoolExecutor) { + if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor threadPoolExecutor) { if (this.removeOnCancelPolicy) { - scheduledPoolExecutor.setRemoveOnCancelPolicy(true); + threadPoolExecutor.setRemoveOnCancelPolicy(true); } if (this.continueExistingPeriodicTasksAfterShutdownPolicy) { - scheduledPoolExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); + threadPoolExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); } if (!this.executeExistingDelayedTasksAfterShutdownPolicy) { - scheduledPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + threadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); } } diff --git a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java index 6e2454df0ad..0ea0d2fe01a 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java +++ b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java @@ -111,11 +111,11 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing if (scheduler == null) { this.taskScheduler = null; } - else if (scheduler instanceof TaskScheduler) { - this.taskScheduler = (TaskScheduler) scheduler; + else if (scheduler instanceof TaskScheduler ts) { + this.taskScheduler = ts; } - else if (scheduler instanceof ScheduledExecutorService) { - this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler)); + else if (scheduler instanceof ScheduledExecutorService ses) { + this.taskScheduler = new ConcurrentTaskScheduler(ses); } else { throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass()); diff --git a/spring-context/src/main/java/org/springframework/scheduling/support/CronTrigger.java b/spring-context/src/main/java/org/springframework/scheduling/support/CronTrigger.java index 74399164a6b..aa4bd38afac 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/support/CronTrigger.java +++ b/spring-context/src/main/java/org/springframework/scheduling/support/CronTrigger.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -114,9 +114,8 @@ public class CronTrigger implements Trigger { @Override - public boolean equals(@Nullable Object other) { - return (this == other || (other instanceof CronTrigger && - this.expression.equals(((CronTrigger) other).expression))); + public boolean equals(@Nullable Object obj) { + return (this == obj || (obj instanceof CronTrigger that && this.expression.equals(that.expression))); } @Override diff --git a/spring-context/src/main/java/org/springframework/scheduling/support/TaskUtils.java b/spring-context/src/main/java/org/springframework/scheduling/support/TaskUtils.java index f27ab89c22a..7a286639e38 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/support/TaskUtils.java +++ b/spring-context/src/main/java/org/springframework/scheduling/support/TaskUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 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. @@ -63,8 +63,8 @@ public abstract class TaskUtils { public static DelegatingErrorHandlingRunnable decorateTaskWithErrorHandler( Runnable task, @Nullable ErrorHandler errorHandler, boolean isRepeatingTask) { - if (task instanceof DelegatingErrorHandlingRunnable) { - return (DelegatingErrorHandlingRunnable) task; + if (task instanceof DelegatingErrorHandlingRunnable dehRunnable) { + return dehRunnable; } ErrorHandler eh = (errorHandler != null ? errorHandler : getDefaultErrorHandler(isRepeatingTask)); return new DelegatingErrorHandlingRunnable(task, eh); diff --git a/spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptFactory.java b/spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptFactory.java index 2ff7d50977b..235593df9ed 100644 --- a/spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptFactory.java +++ b/spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2023 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. @@ -137,10 +137,10 @@ public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware { // New script content: Let's check whether it evaluates to a Class. Object result = BshScriptUtils.evaluateBshScript( scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader); - if (result instanceof Class) { + if (result instanceof Class type) { // A Class: We'll cache the Class here and create an instance // outside the synchronized block. - this.scriptClass = (Class) result; + this.scriptClass = type; } else { // Not a Class: OK, we'll simply create BeanShell objects diff --git a/spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptUtils.java b/spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptUtils.java index 1ef6c28d8fe..8469e4884ca 100644 --- a/spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptUtils.java +++ b/spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptUtils.java @@ -122,8 +122,8 @@ public abstract class BshScriptUtils { interpreter.setClassLoader(classLoader); } Object result = interpreter.eval(scriptSource); - if (result instanceof Class) { - return (Class) result; + if (result instanceof Class clazz) { + return clazz; } else if (result != null) { return result.getClass(); @@ -199,8 +199,8 @@ public abstract class BshScriptUtils { if (result == Primitive.NULL || result == Primitive.VOID) { return null; } - if (result instanceof Primitive) { - return ((Primitive) result).getValue(); + if (result instanceof Primitive primitive) { + return primitive.getValue(); } return result; } @@ -209,13 +209,12 @@ public abstract class BshScriptUtils { } } - private boolean isProxyForSameBshObject(Object other) { - if (!Proxy.isProxyClass(other.getClass())) { + private boolean isProxyForSameBshObject(Object obj) { + if (!Proxy.isProxyClass(obj.getClass())) { return false; } - InvocationHandler ih = Proxy.getInvocationHandler(other); - return (ih instanceof BshObjectInvocationHandler && - this.xt.equals(((BshObjectInvocationHandler) ih).xt)); + InvocationHandler ih = Proxy.getInvocationHandler(obj); + return (ih instanceof BshObjectInvocationHandler that && this.xt.equals(that.xt)); } } diff --git a/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptEvaluator.java b/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptEvaluator.java index 3b66d3eeb4e..21a34f10a35 100644 --- a/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptEvaluator.java +++ b/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptEvaluator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2023 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. @@ -109,8 +109,8 @@ public class GroovyScriptEvaluator implements ScriptEvaluator, BeanClassLoaderAw GroovyShell groovyShell = new GroovyShell( this.classLoader, new Binding(arguments), this.compilerConfiguration); try { - String filename = (script instanceof ResourceScriptSource ? - ((ResourceScriptSource) script).getResource().getFilename() : null); + String filename = (script instanceof ResourceScriptSource resourceScriptSource ? + resourceScriptSource.getResource().getFilename() : null); if (filename != null) { return groovyShell.evaluate(script.getScriptAsString(), filename); } diff --git a/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java b/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java index ef818cce373..21651860682 100644 --- a/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java +++ b/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 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. @@ -151,17 +151,16 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea @Override public void setBeanFactory(BeanFactory beanFactory) { - if (beanFactory instanceof ConfigurableListableBeanFactory) { - ((ConfigurableListableBeanFactory) beanFactory).ignoreDependencyType(MetaClass.class); + if (beanFactory instanceof ConfigurableListableBeanFactory clbf) { + clbf.ignoreDependencyType(MetaClass.class); } } @Override public void setBeanClassLoader(ClassLoader classLoader) { - if (classLoader instanceof GroovyClassLoader && - (this.compilerConfiguration == null || - ((GroovyClassLoader) classLoader).hasCompatibleConfiguration(this.compilerConfiguration))) { - this.groovyClassLoader = (GroovyClassLoader) classLoader; + if (classLoader instanceof GroovyClassLoader gcl && (this.compilerConfiguration == null || + gcl.hasCompatibleConfiguration(this.compilerConfiguration))) { + this.groovyClassLoader = gcl; } else { this.groovyClassLoader = buildGroovyClassLoader(classLoader); @@ -318,20 +317,20 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea @Nullable protected Object executeScript(ScriptSource scriptSource, Class scriptClass) throws ScriptCompilationException { try { - GroovyObject goo = (GroovyObject) ReflectionUtils.accessibleConstructor(scriptClass).newInstance(); + GroovyObject groovyObj = (GroovyObject) ReflectionUtils.accessibleConstructor(scriptClass).newInstance(); if (this.groovyObjectCustomizer != null) { // Allow metaclass and other customization. - this.groovyObjectCustomizer.customize(goo); + this.groovyObjectCustomizer.customize(groovyObj); } - if (goo instanceof Script) { + if (groovyObj instanceof Script script) { // A Groovy script, probably creating an instance: let's execute it. - return ((Script) goo).run(); + return script.run(); } else { // An instance of the scripted class: let's return it as-is. - return goo; + return groovyObj; } } catch (NoSuchMethodException ex) { diff --git a/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java b/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java index 949da723710..e181bc00205 100644 --- a/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -219,11 +219,11 @@ public class ScriptFactoryPostProcessor implements SmartInstantiationAwareBeanPo @Override public void setBeanFactory(BeanFactory beanFactory) { - if (!(beanFactory instanceof ConfigurableBeanFactory)) { + if (!(beanFactory instanceof ConfigurableBeanFactory cbf)) { throw new IllegalStateException("ScriptFactoryPostProcessor doesn't work with " + "non-ConfigurableBeanFactory: " + beanFactory.getClass()); } - this.beanFactory = (ConfigurableBeanFactory) beanFactory; + this.beanFactory = cbf; // Required so that references (up container hierarchies) are correctly resolved. this.scriptBeanFactory.setParentBeanFactory(this.beanFactory); @@ -282,8 +282,8 @@ public class ScriptFactoryPostProcessor implements SmartInstantiationAwareBeanPo } } catch (Exception ex) { - if (ex instanceof BeanCreationException && - ((BeanCreationException) ex).getMostSpecificCause() instanceof BeanCurrentlyInCreationException) { + if (ex instanceof BeanCreationException bce && + bce.getMostSpecificCause() instanceof BeanCurrentlyInCreationException) { if (logger.isTraceEnabled()) { logger.trace("Could not determine scripted object type for bean '" + beanName + "': " + ex.getMessage()); @@ -406,11 +406,11 @@ public class ScriptFactoryPostProcessor implements SmartInstantiationAwareBeanPo protected long resolveRefreshCheckDelay(BeanDefinition beanDefinition) { long refreshCheckDelay = this.defaultRefreshCheckDelay; Object attributeValue = beanDefinition.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE); - if (attributeValue instanceof Number) { - refreshCheckDelay = ((Number) attributeValue).longValue(); + if (attributeValue instanceof Number number) { + refreshCheckDelay = number.longValue(); } - else if (attributeValue instanceof String) { - refreshCheckDelay = Long.parseLong((String) attributeValue); + else if (attributeValue instanceof String text) { + refreshCheckDelay = Long.parseLong(text); } else if (attributeValue != null) { throw new BeanDefinitionStoreException("Invalid refresh check delay attribute [" + @@ -423,11 +423,11 @@ public class ScriptFactoryPostProcessor implements SmartInstantiationAwareBeanPo protected boolean resolveProxyTargetClass(BeanDefinition beanDefinition) { boolean proxyTargetClass = this.defaultProxyTargetClass; Object attributeValue = beanDefinition.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE); - if (attributeValue instanceof Boolean) { - proxyTargetClass = (Boolean) attributeValue; + if (attributeValue instanceof Boolean boo) { + proxyTargetClass = boo; } - else if (attributeValue instanceof String) { - proxyTargetClass = Boolean.parseBoolean((String) attributeValue); + else if (attributeValue instanceof String text) { + proxyTargetClass = Boolean.parseBoolean(text); } else if (attributeValue != null) { throw new BeanDefinitionStoreException("Invalid proxy target class attribute [" + diff --git a/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptEvaluator.java b/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptEvaluator.java index 9f3f34a6aee..7dcdaa4a20a 100644 --- a/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptEvaluator.java +++ b/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptEvaluator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2023 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. @@ -173,8 +173,8 @@ public class StandardScriptEvaluator implements ScriptEvaluator, BeanClassLoader if (StringUtils.hasText(this.engineName)) { return StandardScriptUtils.retrieveEngineByName(scriptEngineManager, this.engineName); } - else if (script instanceof ResourceScriptSource) { - Resource resource = ((ResourceScriptSource) script).getResource(); + else if (script instanceof ResourceScriptSource resourceScriptSource) { + Resource resource = resourceScriptSource.getResource(); String extension = StringUtils.getFilenameExtension(resource.getFilename()); if (extension == null) { throw new IllegalStateException( diff --git a/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptFactory.java b/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptFactory.java index c95d2d368c0..a2e4c4f32f1 100644 --- a/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptFactory.java +++ b/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptFactory.java @@ -149,7 +149,7 @@ public class StandardScriptFactory implements ScriptFactory, BeanClassLoaderAwar if (!ObjectUtils.isEmpty(actualInterfaces)) { boolean adaptationRequired = false; for (Class requestedIfc : actualInterfaces) { - if (script instanceof Class ? !requestedIfc.isAssignableFrom((Class) script) : + if (script instanceof Class clazz ? !requestedIfc.isAssignableFrom(clazz) : !requestedIfc.isInstance(script)) { adaptationRequired = true; break; @@ -210,8 +210,8 @@ public class StandardScriptFactory implements ScriptFactory, BeanClassLoaderAwar return StandardScriptUtils.retrieveEngineByName(scriptEngineManager, this.scriptEngineName); } - if (scriptSource instanceof ResourceScriptSource) { - String filename = ((ResourceScriptSource) scriptSource).getResource().getFilename(); + if (scriptSource instanceof ResourceScriptSource resourceScriptSource) { + String filename = resourceScriptSource.getResource().getFilename(); if (filename != null) { String extension = StringUtils.getFilenameExtension(filename); if (extension != null) { diff --git a/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptUtils.java b/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptUtils.java index 50bec8df80e..2d2bbd9683e 100644 --- a/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptUtils.java +++ b/spring-context/src/main/java/org/springframework/scripting/support/StandardScriptUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2023 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,7 +74,7 @@ public abstract class StandardScriptUtils { } static Bindings getBindings(Map bindings) { - return (bindings instanceof Bindings ? (Bindings) bindings : new SimpleBindings(bindings)); + return (bindings instanceof Bindings b ? b : new SimpleBindings(bindings)); } } diff --git a/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java b/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java index 765a3fb1d62..0d2b6afaeff 100644 --- a/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java +++ b/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -108,7 +108,7 @@ public class ConcurrentModel extends ConcurrentHashMap implement @Override public ConcurrentModel addAttribute(Object attributeValue) { Assert.notNull(attributeValue, "Model attribute value must not be null"); - if (attributeValue instanceof Collection && ((Collection) attributeValue).isEmpty()) { + if (attributeValue instanceof Collection collection && collection.isEmpty()) { return this; } return addAttribute(Conventions.getVariableName(attributeValue), attributeValue); diff --git a/spring-context/src/main/java/org/springframework/ui/ModelMap.java b/spring-context/src/main/java/org/springframework/ui/ModelMap.java index 6a1e438c073..48c887666fb 100644 --- a/spring-context/src/main/java/org/springframework/ui/ModelMap.java +++ b/spring-context/src/main/java/org/springframework/ui/ModelMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 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. @@ -88,7 +88,7 @@ public class ModelMap extends LinkedHashMap { */ public ModelMap addAttribute(Object attributeValue) { Assert.notNull(attributeValue, "Model object must not be null"); - if (attributeValue instanceof Collection && ((Collection) attributeValue).isEmpty()) { + if (attributeValue instanceof Collection collection && collection.isEmpty()) { return this; } return addAttribute(Conventions.getVariableName(attributeValue), attributeValue); diff --git a/spring-context/src/main/java/org/springframework/ui/context/support/UiApplicationContextUtils.java b/spring-context/src/main/java/org/springframework/ui/context/support/UiApplicationContextUtils.java index 41b0a0ded1f..879ed4690e2 100644 --- a/spring-context/src/main/java/org/springframework/ui/context/support/UiApplicationContextUtils.java +++ b/spring-context/src/main/java/org/springframework/ui/context/support/UiApplicationContextUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -59,11 +59,11 @@ public abstract class UiApplicationContextUtils { if (context.containsLocalBean(THEME_SOURCE_BEAN_NAME)) { ThemeSource themeSource = context.getBean(THEME_SOURCE_BEAN_NAME, ThemeSource.class); // Make ThemeSource aware of parent ThemeSource. - if (context.getParent() instanceof ThemeSource && themeSource instanceof HierarchicalThemeSource hts) { + if (context.getParent() instanceof ThemeSource pts && themeSource instanceof HierarchicalThemeSource hts) { if (hts.getParentThemeSource() == null) { // Only set parent context as parent ThemeSource if no parent ThemeSource // registered already. - hts.setParentThemeSource((ThemeSource) context.getParent()); + hts.setParentThemeSource(pts); } } if (logger.isDebugEnabled()) { @@ -75,9 +75,9 @@ public abstract class UiApplicationContextUtils { // Use default ThemeSource to be able to accept getTheme calls, either // delegating to parent context's default or to local ResourceBundleThemeSource. HierarchicalThemeSource themeSource = null; - if (context.getParent() instanceof ThemeSource) { + if (context.getParent() instanceof ThemeSource pts) { themeSource = new DelegatingThemeSource(); - themeSource.setParentThemeSource((ThemeSource) context.getParent()); + themeSource.setParentThemeSource(pts); } else { themeSource = new ResourceBundleThemeSource();