Add nullability annotations to module/spring-boot-validation
See gh-46587
This commit is contained in:
		
							parent
							
								
									e327eb7b45
								
							
						
					
					
						commit
						9a485959c4
					
				|  | @ -16,6 +16,8 @@ | ||||||
| 
 | 
 | ||||||
| package org.springframework.boot.validation.autoconfigure; | package org.springframework.boot.validation.autoconfigure; | ||||||
| 
 | 
 | ||||||
|  | import org.jspecify.annotations.Nullable; | ||||||
|  | 
 | ||||||
| import org.springframework.beans.BeansException; | import org.springframework.beans.BeansException; | ||||||
| import org.springframework.beans.factory.BeanFactory; | import org.springframework.beans.factory.BeanFactory; | ||||||
| import org.springframework.beans.factory.BeanFactoryAware; | import org.springframework.beans.factory.BeanFactoryAware; | ||||||
|  | @ -24,6 +26,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||||||
| import org.springframework.beans.factory.support.BeanDefinitionRegistry; | import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||||||
| import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||||||
| import org.springframework.core.type.AnnotationMetadata; | import org.springframework.core.type.AnnotationMetadata; | ||||||
|  | import org.springframework.util.Assert; | ||||||
| import org.springframework.validation.Validator; | import org.springframework.validation.Validator; | ||||||
| import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | ||||||
| 
 | 
 | ||||||
|  | @ -46,7 +49,7 @@ class PrimaryDefaultValidatorPostProcessor implements ImportBeanDefinitionRegist | ||||||
| 	 */ | 	 */ | ||||||
| 	private static final String VALIDATOR_BEAN_NAME = "defaultValidator"; | 	private static final String VALIDATOR_BEAN_NAME = "defaultValidator"; | ||||||
| 
 | 
 | ||||||
| 	private ConfigurableListableBeanFactory beanFactory; | 	private @Nullable ConfigurableListableBeanFactory beanFactory; | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | 	public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||||||
|  | @ -63,7 +66,7 @@ class PrimaryDefaultValidatorPostProcessor implements ImportBeanDefinitionRegist | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private BeanDefinition getAutoConfiguredValidator(BeanDefinitionRegistry registry) { | 	private @Nullable BeanDefinition getAutoConfiguredValidator(BeanDefinitionRegistry registry) { | ||||||
| 		if (registry.containsBeanDefinition(VALIDATOR_BEAN_NAME)) { | 		if (registry.containsBeanDefinition(VALIDATOR_BEAN_NAME)) { | ||||||
| 			BeanDefinition definition = registry.getBeanDefinition(VALIDATOR_BEAN_NAME); | 			BeanDefinition definition = registry.getBeanDefinition(VALIDATOR_BEAN_NAME); | ||||||
| 			if (definition.getRole() == BeanDefinition.ROLE_INFRASTRUCTURE | 			if (definition.getRole() == BeanDefinition.ROLE_INFRASTRUCTURE | ||||||
|  | @ -79,6 +82,7 @@ class PrimaryDefaultValidatorPostProcessor implements ImportBeanDefinitionRegist | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private boolean hasPrimarySpringValidator() { | 	private boolean hasPrimarySpringValidator() { | ||||||
|  | 		Assert.state(this.beanFactory != null, "'beanFactory' must not be null"); | ||||||
| 		String[] validatorBeans = this.beanFactory.getBeanNamesForType(Validator.class, false, false); | 		String[] validatorBeans = this.beanFactory.getBeanNamesForType(Validator.class, false, false); | ||||||
| 		for (String validatorBean : validatorBeans) { | 		for (String validatorBean : validatorBeans) { | ||||||
| 			BeanDefinition definition = this.beanFactory.getBeanDefinition(validatorBean); | 			BeanDefinition definition = this.beanFactory.getBeanDefinition(validatorBean); | ||||||
|  |  | ||||||
|  | @ -17,6 +17,7 @@ | ||||||
| package org.springframework.boot.validation.autoconfigure; | package org.springframework.boot.validation.autoconfigure; | ||||||
| 
 | 
 | ||||||
| import jakarta.validation.ValidationException; | import jakarta.validation.ValidationException; | ||||||
|  | import org.jspecify.annotations.Nullable; | ||||||
| 
 | 
 | ||||||
| import org.springframework.beans.BeansException; | import org.springframework.beans.BeansException; | ||||||
| import org.springframework.beans.factory.DisposableBean; | import org.springframework.beans.factory.DisposableBean; | ||||||
|  | @ -26,6 +27,7 @@ import org.springframework.boot.validation.MessageInterpolatorFactory; | ||||||
| import org.springframework.context.ApplicationContext; | import org.springframework.context.ApplicationContext; | ||||||
| import org.springframework.context.ApplicationContextAware; | import org.springframework.context.ApplicationContextAware; | ||||||
| import org.springframework.context.MessageSource; | import org.springframework.context.MessageSource; | ||||||
|  | import org.springframework.util.Assert; | ||||||
| import org.springframework.validation.Errors; | import org.springframework.validation.Errors; | ||||||
| import org.springframework.validation.SmartValidator; | import org.springframework.validation.SmartValidator; | ||||||
| import org.springframework.validation.Validator; | import org.springframework.validation.Validator; | ||||||
|  | @ -104,7 +106,7 @@ public class ValidatorAdapter implements SmartValidator, ApplicationContextAware | ||||||
| 	 * @param validator an existing validator to use or {@code null} | 	 * @param validator an existing validator to use or {@code null} | ||||||
| 	 * @return the validator to use | 	 * @return the validator to use | ||||||
| 	 */ | 	 */ | ||||||
| 	public static Validator get(ApplicationContext applicationContext, Validator validator) { | 	public static Validator get(ApplicationContext applicationContext, @Nullable Validator validator) { | ||||||
| 		if (validator != null) { | 		if (validator != null) { | ||||||
| 			return wrap(validator, false); | 			return wrap(validator, false); | ||||||
| 		} | 		} | ||||||
|  | @ -119,7 +121,7 @@ public class ValidatorAdapter implements SmartValidator, ApplicationContextAware | ||||||
| 		return create(applicationContext); | 		return create(applicationContext); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private static Validator getExisting(ApplicationContext applicationContext) { | 	private static @Nullable Validator getExisting(ApplicationContext applicationContext) { | ||||||
| 		try { | 		try { | ||||||
| 			jakarta.validation.Validator validatorBean = applicationContext.getBean(jakarta.validation.Validator.class); | 			jakarta.validation.Validator validatorBean = applicationContext.getBean(jakarta.validation.Validator.class); | ||||||
| 			if (validatorBean instanceof Validator validator) { | 			if (validatorBean instanceof Validator validator) { | ||||||
|  | @ -156,7 +158,8 @@ public class ValidatorAdapter implements SmartValidator, ApplicationContextAware | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	@SuppressWarnings("unchecked") | 	@SuppressWarnings("unchecked") | ||||||
| 	public <T> T unwrap(Class<T> type) { | 	public <T> @Nullable T unwrap(@Nullable Class<T> type) { | ||||||
|  | 		Assert.state(type != null, "'type' must not be null"); | ||||||
| 		if (type.isInstance(this.target)) { | 		if (type.isInstance(this.target)) { | ||||||
| 			return (T) this.target; | 			return (T) this.target; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -17,4 +17,7 @@ | ||||||
| /** | /** | ||||||
|  * Auto-configuration for (JSR-303) Validation. |  * Auto-configuration for (JSR-303) Validation. | ||||||
|  */ |  */ | ||||||
|  | @NullMarked | ||||||
| package org.springframework.boot.validation.autoconfigure; | package org.springframework.boot.validation.autoconfigure; | ||||||
|  | 
 | ||||||
|  | import org.jspecify.annotations.NullMarked; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue