Refine null-safety in the spring-beans module

Closes gh-34152
This commit is contained in:
Sébastien Deleuze
2024-12-26 11:24:05 +01:00
parent 1c37e7092e
commit 8b741f3a46
12 changed files with 21 additions and 19 deletions

View File

@@ -98,7 +98,8 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
private boolean configValueEditorsActive = false;
private @Nullable Map<Class<?>, PropertyEditor> defaultEditors;
@SuppressWarnings("NullAway.Init")
private Map<Class<?>, PropertyEditor> defaultEditors;
private @Nullable Map<Class<?>, PropertyEditor> overriddenDefaultEditors;
@@ -171,7 +172,6 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
* @return the default editor, or {@code null} if none found
* @see #registerDefaultEditors
*/
@SuppressWarnings("NullAway")
public @Nullable PropertyEditor getDefaultEditor(Class<?> requiredType) {
if (!this.defaultEditorsActive) {
return null;

View File

@@ -100,9 +100,11 @@ public class BeanDefinitionStoreException extends FatalBeanException {
* @param cause the root cause (may be {@code null})
*/
public BeanDefinitionStoreException(
@Nullable String resourceDescription, String beanName, String msg, @Nullable Throwable cause) {
@Nullable String resourceDescription, String beanName, @Nullable String msg, @Nullable Throwable cause) {
super("Invalid bean definition with name '" + beanName + "' defined in " + resourceDescription + ": " + msg,
super(msg == null ?
"Invalid bean definition with name '" + beanName + "' defined in " + resourceDescription :
"Invalid bean definition with name '" + beanName + "' defined in " + resourceDescription + ": " + msg,
cause);
this.resourceDescription = resourceDescription;
this.beanName = beanName;

View File

@@ -74,7 +74,8 @@ public abstract class AbstractFactoryBean<T>
private boolean initialized = false;
private @Nullable T singletonInstance;
@SuppressWarnings("NullAway.Init")
private T singletonInstance;
private @Nullable T earlySingletonInstance;
@@ -146,7 +147,6 @@ public abstract class AbstractFactoryBean<T>
* @see #getEarlySingletonInterfaces()
*/
@Override
@SuppressWarnings("NullAway")
public final T getObject() throws Exception {
if (isSingleton()) {
return (this.initialized ? this.singletonInstance : getEarlySingletonInstance());

View File

@@ -224,7 +224,6 @@ public abstract class PlaceholderConfigurerSupport extends PropertyResourceConfi
this.beanFactory = beanFactory;
}
@SuppressWarnings("NullAway")
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
StringValueResolver valueResolver) {

View File

@@ -89,13 +89,15 @@ public class PropertyPathFactoryBean implements FactoryBean<Object>, BeanNameAwa
private @Nullable BeanWrapper targetBeanWrapper;
private @Nullable String targetBeanName;
@SuppressWarnings("NullAway.Init")
private String targetBeanName;
private @Nullable String propertyPath;
private @Nullable Class<?> resultType;
private @Nullable String beanName;
@SuppressWarnings("NullAway.Init")
private String beanName;
private @Nullable BeanFactory beanFactory;
@@ -156,7 +158,6 @@ public class PropertyPathFactoryBean implements FactoryBean<Object>, BeanNameAwa
@Override
@SuppressWarnings("NullAway")
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;

View File

@@ -696,7 +696,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
}
}
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
XmlReaderContext readerContext = this.groovyDslXmlBeanDefinitionReader.createReaderContext(
new DescriptiveResource("Groovy"));

View File

@@ -131,7 +131,7 @@ class ConstructorResolver {
* or {@code null} if none (-> use constructor argument values from bean definition)
* @return a BeanWrapper for the new instance
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
Constructor<?> @Nullable [] chosenCtors, @Nullable Object @Nullable [] explicitArgs) {
@@ -393,7 +393,7 @@ class ConstructorResolver {
* method, or {@code null} if none (-> use constructor argument values from bean definition)
* @return a BeanWrapper for the new instance
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
public BeanWrapper instantiateUsingFactoryMethod(
String beanName, RootBeanDefinition mbd, @Nullable Object @Nullable [] explicitArgs) {

View File

@@ -1499,7 +1499,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
}
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
public @Nullable Object doResolveDependency(DependencyDescriptor descriptor, @Nullable String beanName,
@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {

View File

@@ -234,7 +234,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
* with, if necessary
* @return the registered singleton object
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");

View File

@@ -72,7 +72,7 @@ public class GenericTypeAwareAutowireCandidateResolver extends SimpleAutowireCan
* Match the given dependency type with its generic type information against the given
* candidate bean definition.
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
ResolvableType dependencyType = descriptor.getResolvableType();
if (dependencyType.getType() instanceof Class) {

View File

@@ -145,7 +145,7 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy {
}
@Override
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1113
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
@Nullable Object factoryBean, Method factoryMethod, @Nullable Object... args) {

View File

@@ -407,7 +407,6 @@ public class BeanDefinitionParserDelegate {
* if there were errors during parse. Errors are reported to the
* {@link org.springframework.beans.factory.parsing.ProblemReporter}.
*/
@SuppressWarnings("NullAway")
public @Nullable BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) {
String id = ele.getAttribute(ID_ATTRIBUTE);
String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);
@@ -457,7 +456,8 @@ public class BeanDefinitionParserDelegate {
}
}
catch (Exception ex) {
error(ex.getMessage(), ele);
String message = ex.getMessage();
error(message == null ? "" : message, ele);
return null;
}
}