Apply "instanceof pattern matching" in spring-context and polishing

See gh-29222
This commit is contained in:
Sam Brannen
2023-01-29 14:20:30 +01:00
parent 8783075e40
commit e0f60dc09d
7 changed files with 36 additions and 37 deletions

View File

@@ -61,9 +61,9 @@ public class SimpleKey implements Serializable {
@Override
public boolean equals(@Nullable Object other) {
return (this == other ||
(other instanceof SimpleKey simpleKey && Arrays.deepEquals(this.params, simpleKey.params)));
public boolean equals(@Nullable Object obj) {
return (this == obj ||
(obj instanceof SimpleKey that && Arrays.deepEquals(this.params, that.params)));
}
@Override

View File

@@ -60,7 +60,6 @@ public class NoOpCacheManager implements CacheManager {
this.cacheNames.add(name);
}
}
return this.caches.get(name);
}

View File

@@ -107,7 +107,7 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
});
if (isStereotypeWithNameValue(type, metaTypes, attributes)) {
Object value = attributes.get("value");
if (value instanceof String strVal && StringUtils.hasLength(strVal)) {
if (value instanceof String strVal && !strVal.isEmpty()) {
if (beanName != null && !strVal.equals(beanName)) {
throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
"component names: '" + beanName + "' versus '" + strVal + "'");

View File

@@ -56,8 +56,8 @@ final class BeanMethod extends ConfigurationMethod {
@Override
public boolean equals(@Nullable Object obj) {
return ((this == obj) || ((obj instanceof BeanMethod beanMethod) &&
this.metadata.equals((beanMethod).metadata)));
return (this == obj ||
(obj instanceof BeanMethod that && this.metadata.equals(that.metadata)));
}
@Override
@@ -73,8 +73,8 @@ final class BeanMethod extends ConfigurationMethod {
private class NonOverridableMethodError extends Problem {
NonOverridableMethodError() {
super(String.format("@Bean method '%s' must not be private or final; change the method's modifiers to continue",
getMetadata().getMethodName()), getResourceLocation());
super("@Bean method '%s' must not be private or final; change the method's modifiers to continue."
.formatted(getMetadata().getMethodName()), getResourceLocation());
}
}

View File

@@ -155,7 +155,7 @@ class ConditionEvaluator {
}
@Nullable
private ConfigurableListableBeanFactory deduceBeanFactory(@Nullable BeanDefinitionRegistry source) {
private static ConfigurableListableBeanFactory deduceBeanFactory(@Nullable BeanDefinitionRegistry source) {
if (source instanceof ConfigurableListableBeanFactory configurableListableBeanFactory) {
return configurableListableBeanFactory;
}
@@ -165,22 +165,22 @@ class ConditionEvaluator {
return null;
}
private Environment deduceEnvironment(@Nullable BeanDefinitionRegistry source) {
private static Environment deduceEnvironment(@Nullable BeanDefinitionRegistry source) {
if (source instanceof EnvironmentCapable environmentCapable) {
return environmentCapable.getEnvironment();
}
return new StandardEnvironment();
}
private ResourceLoader deduceResourceLoader(@Nullable BeanDefinitionRegistry source) {
if (source instanceof ResourceLoader resourceLoaderSource) {
return resourceLoaderSource;
private static ResourceLoader deduceResourceLoader(@Nullable BeanDefinitionRegistry source) {
if (source instanceof ResourceLoader resourceLoader) {
return resourceLoader;
}
return new DefaultResourceLoader();
}
@Nullable
private ClassLoader deduceClassLoader(@Nullable ResourceLoader resourceLoader,
private static ClassLoader deduceClassLoader(@Nullable ResourceLoader resourceLoader,
@Nullable ConfigurableListableBeanFactory beanFactory) {
if (resourceLoader != null) {

View File

@@ -237,9 +237,9 @@ final class ConfigurationClass {
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof ConfigurationClass configurationClass &&
getMetadata().getClassName().equals(configurationClass.getMetadata().getClassName())));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof ConfigurationClass that &&
getMetadata().getClassName().equals(that.getMetadata().getClassName())));
}
@Override

View File

@@ -160,11 +160,11 @@ class ConfigurationClassParser {
for (BeanDefinitionHolder holder : configCandidates) {
BeanDefinition bd = holder.getBeanDefinition();
try {
if (bd instanceof AnnotatedBeanDefinition annotatedBeanDefinition) {
parse(annotatedBeanDefinition.getMetadata(), holder.getBeanName());
if (bd instanceof AnnotatedBeanDefinition annotatedBeanDef) {
parse(annotatedBeanDef.getMetadata(), holder.getBeanName());
}
else if (bd instanceof AbstractBeanDefinition abstractBeanDefinition && abstractBeanDefinition.hasBeanClass()) {
parse(abstractBeanDefinition.getBeanClass(), holder.getBeanName());
else if (bd instanceof AbstractBeanDefinition abstractBeanDef && abstractBeanDef.hasBeanClass()) {
parse(abstractBeanDef.getBeanClass(), holder.getBeanName());
}
else {
parse(bd.getBeanClassName(), holder.getBeanName());
@@ -839,8 +839,8 @@ class ConfigurationClassParser {
public SourceClass(Object source) {
this.source = source;
if (source instanceof Class) {
this.metadata = AnnotationMetadata.introspect((Class<?>) source);
if (source instanceof Class<?> sourceClass) {
this.metadata = AnnotationMetadata.introspect(sourceClass);
}
else {
this.metadata = ((MetadataReader) source).getAnnotationMetadata();
@@ -858,23 +858,23 @@ class ConfigurationClassParser {
}
public Class<?> loadClass() throws ClassNotFoundException {
if (this.source instanceof Class) {
return (Class<?>) this.source;
if (this.source instanceof Class<?> sourceClass) {
return sourceClass;
}
String className = ((MetadataReader) this.source).getClassMetadata().getClassName();
return ClassUtils.forName(className, resourceLoader.getClassLoader());
}
public boolean isAssignable(Class<?> clazz) throws IOException {
if (this.source instanceof Class) {
return clazz.isAssignableFrom((Class<?>) this.source);
if (this.source instanceof Class<?> sourceClass) {
return clazz.isAssignableFrom(sourceClass);
}
return new AssignableTypeFilter(clazz).match((MetadataReader) this.source, metadataReaderFactory);
}
public ConfigurationClass asConfigClass(ConfigurationClass importedBy) {
if (this.source instanceof Class) {
return new ConfigurationClass((Class<?>) this.source, importedBy);
if (this.source instanceof Class<?> sourceClass) {
return new ConfigurationClass(sourceClass, importedBy);
}
return new ConfigurationClass((MetadataReader) this.source, importedBy);
}
@@ -917,8 +917,8 @@ class ConfigurationClassParser {
}
public SourceClass getSuperClass() throws IOException {
if (this.source instanceof Class) {
return asSourceClass(((Class<?>) this.source).getSuperclass(), DEFAULT_EXCLUSION_FILTER);
if (this.source instanceof Class<?> sourceClass) {
return asSourceClass(sourceClass.getSuperclass(), DEFAULT_EXCLUSION_FILTER);
}
return asSourceClass(
((MetadataReader) this.source).getClassMetadata().getSuperClassName(), DEFAULT_EXCLUSION_FILTER);
@@ -985,9 +985,9 @@ class ConfigurationClassParser {
}
private SourceClass getRelated(String className) throws IOException {
if (this.source instanceof Class) {
if (this.source instanceof Class<?> sourceClass) {
try {
Class<?> clazz = ClassUtils.forName(className, ((Class<?>) this.source).getClassLoader());
Class<?> clazz = ClassUtils.forName(className, sourceClass.getClassLoader());
return asSourceClass(clazz, DEFAULT_EXCLUSION_FILTER);
}
catch (ClassNotFoundException ex) {
@@ -1002,9 +1002,9 @@ class ConfigurationClassParser {
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof SourceClass sourceClass &&
this.metadata.getClassName().equals(sourceClass.metadata.getClassName())));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof SourceClass that &&
this.metadata.getClassName().equals(that.metadata.getClassName())));
}
@Override