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