Browse Source

Refine null-safety in the spring-beans module

Closes gh-34152
pull/34171/head
Sébastien Deleuze 12 months ago
parent
commit
8b741f3a46
  1. 4
      spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java
  2. 6
      spring-beans/src/main/java/org/springframework/beans/factory/BeanDefinitionStoreException.java
  3. 4
      spring-beans/src/main/java/org/springframework/beans/factory/config/AbstractFactoryBean.java
  4. 1
      spring-beans/src/main/java/org/springframework/beans/factory/config/PlaceholderConfigurerSupport.java
  5. 7
      spring-beans/src/main/java/org/springframework/beans/factory/config/PropertyPathFactoryBean.java
  6. 2
      spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java
  7. 4
      spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java
  8. 2
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
  9. 2
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java
  10. 2
      spring-beans/src/main/java/org/springframework/beans/factory/support/GenericTypeAwareAutowireCandidateResolver.java
  11. 2
      spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java
  12. 4
      spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java

4
spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java

@ -98,7 +98,8 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry { @@ -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 { @@ -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;

6
spring-beans/src/main/java/org/springframework/beans/factory/BeanDefinitionStoreException.java

@ -100,9 +100,11 @@ public class BeanDefinitionStoreException extends FatalBeanException { @@ -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;

4
spring-beans/src/main/java/org/springframework/beans/factory/config/AbstractFactoryBean.java

@ -74,7 +74,8 @@ public abstract class AbstractFactoryBean<T> @@ -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> @@ -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());

1
spring-beans/src/main/java/org/springframework/beans/factory/config/PlaceholderConfigurerSupport.java

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

7
spring-beans/src/main/java/org/springframework/beans/factory/config/PropertyPathFactoryBean.java

@ -89,13 +89,15 @@ public class PropertyPathFactoryBean implements FactoryBean<Object>, BeanNameAwa @@ -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 @@ -156,7 +158,6 @@ public class PropertyPathFactoryBean implements FactoryBean<Object>, BeanNameAwa
@Override
@SuppressWarnings("NullAway")
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;

2
spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java

@ -696,7 +696,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp @@ -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"));

4
spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

@ -131,7 +131,7 @@ class ConstructorResolver { @@ -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 { @@ -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) {

2
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

@ -1499,7 +1499,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -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 {

2
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java

@ -234,7 +234,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements @@ -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");

2
spring-beans/src/main/java/org/springframework/beans/factory/support/GenericTypeAwareAutowireCandidateResolver.java

@ -72,7 +72,7 @@ public class GenericTypeAwareAutowireCandidateResolver extends SimpleAutowireCan @@ -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) {

2
spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java

@ -145,7 +145,7 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy { @@ -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) {

4
spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java

@ -407,7 +407,6 @@ public class BeanDefinitionParserDelegate { @@ -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 { @@ -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;
}
}

Loading…
Cancel
Save