Apply "instanceof pattern matching" and minor refactorings in spring-context
Closes gh-29222
This commit is contained in:
parent
f2cdced501
commit
8783075e40
|
@ -63,7 +63,7 @@ public class SimpleKey implements Serializable {
|
|||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other ||
|
||||
(other instanceof SimpleKey && Arrays.deepEquals(this.params, ((SimpleKey) other).params)));
|
||||
(other instanceof SimpleKey simpleKey && Arrays.deepEquals(this.params, simpleKey.params)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -55,7 +55,7 @@ public class NoOpCacheManager implements CacheManager {
|
|||
public Cache getCache(String name) {
|
||||
Cache cache = this.caches.get(name);
|
||||
if (cache == null) {
|
||||
this.caches.computeIfAbsent(name, key -> new NoOpCache(name));
|
||||
this.caches.computeIfAbsent(name, NoOpCache::new);
|
||||
synchronized (this.cacheNames) {
|
||||
this.cacheNames.add(name);
|
||||
}
|
||||
|
|
|
@ -292,8 +292,8 @@ public class AnnotatedBeanDefinitionReader {
|
|||
*/
|
||||
private static Environment getOrCreateEnvironment(BeanDefinitionRegistry registry) {
|
||||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
|
||||
if (registry instanceof EnvironmentCapable) {
|
||||
return ((EnvironmentCapable) registry).getEnvironment();
|
||||
if (registry instanceof EnvironmentCapable environmentCapable) {
|
||||
return environmentCapable.getEnvironment();
|
||||
}
|
||||
return new StandardEnvironment();
|
||||
}
|
||||
|
|
|
@ -77,8 +77,8 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
|
|||
|
||||
@Override
|
||||
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
|
||||
if (definition instanceof AnnotatedBeanDefinition) {
|
||||
String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);
|
||||
if (definition instanceof AnnotatedBeanDefinition annotatedBeanDefinition) {
|
||||
String beanName = determineBeanNameFromAnnotation(annotatedBeanDefinition);
|
||||
if (StringUtils.hasText(beanName)) {
|
||||
// Explicit bean name found.
|
||||
return beanName;
|
||||
|
@ -107,14 +107,12 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
|
|||
});
|
||||
if (isStereotypeWithNameValue(type, metaTypes, attributes)) {
|
||||
Object value = attributes.get("value");
|
||||
if (value instanceof String strVal) {
|
||||
if (StringUtils.hasLength(strVal)) {
|
||||
if (beanName != null && !strVal.equals(beanName)) {
|
||||
throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
|
||||
"component names: '" + beanName + "' versus '" + strVal + "'");
|
||||
}
|
||||
beanName = strVal;
|
||||
if (value instanceof String strVal && StringUtils.hasLength(strVal)) {
|
||||
if (beanName != null && !strVal.equals(beanName)) {
|
||||
throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
|
||||
"component names: '" + beanName + "' versus '" + strVal + "'");
|
||||
}
|
||||
beanName = strVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,8 +56,8 @@ final class BeanMethod extends ConfigurationMethod {
|
|||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
return ((this == obj) || ((obj instanceof BeanMethod) &&
|
||||
this.metadata.equals(((BeanMethod) obj).metadata)));
|
||||
return ((this == obj) || ((obj instanceof BeanMethod beanMethod) &&
|
||||
this.metadata.equals((beanMethod).metadata)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -138,7 +138,7 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
|
|||
Environment environment) {
|
||||
|
||||
this(registry, useDefaultFilters, environment,
|
||||
(registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
|
||||
(registry instanceof ResourceLoader resourceLoader ? resourceLoader : null));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -278,11 +278,11 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
|
|||
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
|
||||
candidate.setScope(scopeMetadata.getScopeName());
|
||||
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
|
||||
if (candidate instanceof AbstractBeanDefinition) {
|
||||
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
|
||||
if (candidate instanceof AbstractBeanDefinition abstractBeanDefinition) {
|
||||
postProcessBeanDefinition(abstractBeanDefinition, beanName);
|
||||
}
|
||||
if (candidate instanceof AnnotatedBeanDefinition) {
|
||||
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
|
||||
if (candidate instanceof AnnotatedBeanDefinition annotatedBeanDefinition) {
|
||||
AnnotationConfigUtils.processCommonDefinitionAnnotations(annotatedBeanDefinition);
|
||||
}
|
||||
if (checkCandidate(beanName, candidate)) {
|
||||
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
|
||||
|
@ -373,8 +373,8 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
|
|||
*/
|
||||
private static Environment getOrCreateEnvironment(BeanDefinitionRegistry registry) {
|
||||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
|
||||
if (registry instanceof EnvironmentCapable) {
|
||||
return ((EnvironmentCapable) registry).getEnvironment();
|
||||
if (registry instanceof EnvironmentCapable environmentCapable) {
|
||||
return environmentCapable.getEnvironment();
|
||||
}
|
||||
return new StandardEnvironment();
|
||||
}
|
||||
|
|
|
@ -83,8 +83,8 @@ class ConditionEvaluator {
|
|||
}
|
||||
|
||||
if (phase == null) {
|
||||
if (metadata instanceof AnnotationMetadata &&
|
||||
ConfigurationClassUtils.isConfigurationCandidate((AnnotationMetadata) metadata)) {
|
||||
if (metadata instanceof AnnotationMetadata annotationMetadata &&
|
||||
ConfigurationClassUtils.isConfigurationCandidate(annotationMetadata)) {
|
||||
return shouldSkip(metadata, ConfigurationPhase.PARSE_CONFIGURATION);
|
||||
}
|
||||
return shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN);
|
||||
|
@ -102,8 +102,8 @@ class ConditionEvaluator {
|
|||
|
||||
for (Condition condition : conditions) {
|
||||
ConfigurationPhase requiredPhase = null;
|
||||
if (condition instanceof ConfigurationCondition) {
|
||||
requiredPhase = ((ConfigurationCondition) condition).getConfigurationPhase();
|
||||
if (condition instanceof ConfigurationCondition configurationCondition) {
|
||||
requiredPhase = configurationCondition.getConfigurationPhase();
|
||||
}
|
||||
if ((requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)) {
|
||||
return true;
|
||||
|
@ -156,25 +156,25 @@ class ConditionEvaluator {
|
|||
|
||||
@Nullable
|
||||
private ConfigurableListableBeanFactory deduceBeanFactory(@Nullable BeanDefinitionRegistry source) {
|
||||
if (source instanceof ConfigurableListableBeanFactory) {
|
||||
return (ConfigurableListableBeanFactory) source;
|
||||
if (source instanceof ConfigurableListableBeanFactory configurableListableBeanFactory) {
|
||||
return configurableListableBeanFactory;
|
||||
}
|
||||
if (source instanceof ConfigurableApplicationContext) {
|
||||
return (((ConfigurableApplicationContext) source).getBeanFactory());
|
||||
if (source instanceof ConfigurableApplicationContext configurableApplicationContext) {
|
||||
return configurableApplicationContext.getBeanFactory();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Environment deduceEnvironment(@Nullable BeanDefinitionRegistry source) {
|
||||
if (source instanceof EnvironmentCapable) {
|
||||
return ((EnvironmentCapable) source).getEnvironment();
|
||||
if (source instanceof EnvironmentCapable environmentCapable) {
|
||||
return environmentCapable.getEnvironment();
|
||||
}
|
||||
return new StandardEnvironment();
|
||||
}
|
||||
|
||||
private ResourceLoader deduceResourceLoader(@Nullable BeanDefinitionRegistry source) {
|
||||
if (source instanceof ResourceLoader) {
|
||||
return (ResourceLoader) source;
|
||||
if (source instanceof ResourceLoader resourceLoaderSource) {
|
||||
return resourceLoaderSource;
|
||||
}
|
||||
return new DefaultResourceLoader();
|
||||
}
|
||||
|
|
|
@ -238,8 +238,8 @@ final class ConfigurationClass {
|
|||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof ConfigurationClass &&
|
||||
getMetadata().getClassName().equals(((ConfigurationClass) other).getMetadata().getClassName())));
|
||||
return (this == other || (other instanceof ConfigurationClass configurationClass &&
|
||||
getMetadata().getClassName().equals(configurationClass.getMetadata().getClassName())));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -160,11 +160,11 @@ class ConfigurationClassParser {
|
|||
for (BeanDefinitionHolder holder : configCandidates) {
|
||||
BeanDefinition bd = holder.getBeanDefinition();
|
||||
try {
|
||||
if (bd instanceof AnnotatedBeanDefinition) {
|
||||
parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
|
||||
if (bd instanceof AnnotatedBeanDefinition annotatedBeanDefinition) {
|
||||
parse(annotatedBeanDefinition.getMetadata(), holder.getBeanName());
|
||||
}
|
||||
else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
|
||||
parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
|
||||
else if (bd instanceof AbstractBeanDefinition abstractBeanDefinition && abstractBeanDefinition.hasBeanClass()) {
|
||||
parse(abstractBeanDefinition.getBeanClass(), holder.getBeanName());
|
||||
}
|
||||
else {
|
||||
parse(bd.getBeanClassName(), holder.getBeanName());
|
||||
|
@ -488,8 +488,8 @@ class ConfigurationClassParser {
|
|||
if (selectorFilter != null) {
|
||||
exclusionFilter = exclusionFilter.or(selectorFilter);
|
||||
}
|
||||
if (selector instanceof DeferredImportSelector) {
|
||||
this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector);
|
||||
if (selector instanceof DeferredImportSelector deferredImportSelector) {
|
||||
this.deferredImportSelectorHandler.handle(configClass, deferredImportSelector);
|
||||
}
|
||||
else {
|
||||
String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
|
||||
|
@ -553,8 +553,8 @@ class ConfigurationClassParser {
|
|||
*/
|
||||
private SourceClass asSourceClass(ConfigurationClass configurationClass, Predicate<String> filter) throws IOException {
|
||||
AnnotationMetadata metadata = configurationClass.getMetadata();
|
||||
if (metadata instanceof StandardAnnotationMetadata) {
|
||||
return asSourceClass(((StandardAnnotationMetadata) metadata).getIntrospectedClass(), filter);
|
||||
if (metadata instanceof StandardAnnotationMetadata standardAnnotationMetadata) {
|
||||
return asSourceClass(standardAnnotationMetadata.getIntrospectedClass(), filter);
|
||||
}
|
||||
return asSourceClass(metadata.getClassName(), filter);
|
||||
}
|
||||
|
@ -1003,8 +1003,8 @@ class ConfigurationClassParser {
|
|||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof SourceClass &&
|
||||
this.metadata.getClassName().equals(((SourceClass) other).metadata.getClassName())));
|
||||
return (this == other || (other instanceof SourceClass sourceClass &&
|
||||
this.metadata.getClassName().equals(sourceClass.metadata.getClassName())));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -50,8 +50,8 @@ public final class ImportAwareAotBeanPostProcessor implements BeanPostProcessor,
|
|||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) {
|
||||
if (bean instanceof ImportAware) {
|
||||
setAnnotationMetadata((ImportAware) bean);
|
||||
if (bean instanceof ImportAware importAware) {
|
||||
setAnnotationMetadata(importAware);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue