Refine null-safety in the spring-context module

Closes gh-34151
This commit is contained in:
Sébastien Deleuze
2024-12-24 18:27:13 +01:00
parent cf90beec0a
commit f368147f9d
19 changed files with 26 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -43,7 +43,7 @@ import org.springframework.core.KotlinDetector;
public class SimpleKeyGenerator implements KeyGenerator {
@Override
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1113
public Object generate(Object target, Method method, @Nullable Object... params) {
return generateKey((KotlinDetector.isSuspendingFunction(method) ?
Arrays.copyOf(params, params.length - 1) : params));

View File

@@ -45,7 +45,7 @@ final class BeanMethod extends ConfigurationMethod {
@Override
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Reflection
public void validate(ProblemReporter problemReporter) {
if (getMetadata().getAnnotationAttributes(Autowired.class.getName()) != null) {
// declared as @Autowired: semantic mismatch since @Bean method arguments are autowired

View File

@@ -218,7 +218,7 @@ final class ConfigurationClass {
return this.importBeanDefinitionRegistrars;
}
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Reflection
void validate(ProblemReporter problemReporter) {
Map<String, Object> attributes = this.metadata.getAnnotationAttributes(Configuration.class.getName());

View File

@@ -291,7 +291,7 @@ class ConfigurationClassBeanDefinitionReader {
this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Reflection
protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String beanName) {
if (!this.registry.containsBeanDefinition(beanName)) {
return false;

View File

@@ -821,12 +821,12 @@ class ConfigurationClassParser {
deferredImport.getConfigurationClass());
}
@SuppressWarnings("NullAway")
void processGroupImports() {
for (DeferredImportSelectorGrouping grouping : this.groupings.values()) {
Predicate<String> filter = grouping.getCandidateFilter();
grouping.getImports().forEach(entry -> {
ConfigurationClass configurationClass = this.configurationClasses.get(entry.getMetadata());
Assert.state(configurationClass != null, "ConfigurationClass must not be null");
try {
processImports(configurationClass, asSourceClass(configurationClass, filter),
Collections.singleton(asSourceClass(entry.getImportClassName(), filter)),

View File

@@ -319,7 +319,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
}
@Override
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Lazy initialization
public @Nullable BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
boolean hasPropertySourceDescriptors = !CollectionUtils.isEmpty(this.propertySourceDescriptors);
boolean hasImportRegistry = beanFactory.containsBean(IMPORT_REGISTRY_BEAN_NAME);

View File

@@ -127,7 +127,6 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat
}
@Override
@SuppressWarnings("NullAway")
public Object getTarget() {
Object cachedTarget = this.cachedTarget;
if (cachedTarget != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -31,7 +31,7 @@ import org.springframework.util.MultiValueMap;
class ProfileCondition implements Condition {
@Override
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Reflection
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -229,7 +229,7 @@ public abstract class AbstractApplicationEventMulticaster
* @param retriever the ListenerRetriever, if supposed to populate one (for caching purposes)
* @return the pre-filtered list of application listeners for the given event and source type
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
private Collection<ApplicationListener<?>> retrieveApplicationListeners(
ResolvableType eventType, @Nullable Class<?> sourceType, @Nullable CachedListenerRetriever retriever) {

View File

@@ -51,7 +51,7 @@ import org.springframework.format.annotation.DateTimeFormat.ISO;
* @see org.springframework.format.FormatterRegistrar#registerFormatters
* @see org.springframework.format.datetime.DateFormatterRegistrar
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Well-known map keys
public class DateTimeFormatterRegistrar implements FormatterRegistrar {
private enum Type {DATE, TIME, DATE_TIME}

View File

@@ -987,7 +987,6 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
* Unregister the configured {@link NotificationListener NotificationListeners}
* from the {@link MBeanServer}.
*/
@SuppressWarnings("NullAway")
private void unregisterNotificationListeners() {
if (this.server != null) {
this.registeredNotificationListeners.forEach((bean, mappedObjectNames) -> {

View File

@@ -669,7 +669,6 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
}
}
@SuppressWarnings("NullAway")
private void assertValidators(@Nullable Validator... validators) {
Object target = getTarget();
for (Validator validator : validators) {
@@ -728,7 +727,6 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
* {@link #setExcludedValidators(Predicate) exclude predicate}.
* @since 6.1
*/
@SuppressWarnings("NullAway")
public List<Validator> getValidatorsToApply() {
return (this.excludedValidators != null ?
this.validators.stream().filter(validator -> !this.excludedValidators.test(validator)).toList() :
@@ -1232,7 +1230,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
* @see #getBindingErrorProcessor
* @see BindingErrorProcessor#processMissingFieldError
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
protected void checkRequiredFields(MutablePropertyValues mpvs) {
String[] requiredFields = getRequiredFields();
if (!ObjectUtils.isEmpty(requiredFields)) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2024 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.
@@ -243,7 +243,6 @@ public class DefaultMessageCodesResolver implements MessageCodesResolver, Serial
* {@link DefaultMessageCodesResolver#CODE_SEPARATOR}, skipping zero-length or
* null elements altogether.
*/
@SuppressWarnings("NullAway")
public static String toDelimitedString(@Nullable String... elements) {
StringJoiner rtn = new StringJoiner(CODE_SEPARATOR);
for (String element : elements) {