Skip to content

Commit 8b741f3

Browse files
committed
Refine null-safety in the spring-beans module
Closes gh-34152
1 parent 1c37e70 commit 8b741f3

12 files changed

+21
-19
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
9898

9999
private boolean configValueEditorsActive = false;
100100

101-
private @Nullable Map<Class<?>, PropertyEditor> defaultEditors;
101+
@SuppressWarnings("NullAway.Init")
102+
private Map<Class<?>, PropertyEditor> defaultEditors;
102103

103104
private @Nullable Map<Class<?>, PropertyEditor> overriddenDefaultEditors;
104105

@@ -171,7 +172,6 @@ public void overrideDefaultEditor(Class<?> requiredType, PropertyEditor property
171172
* @return the default editor, or {@code null} if none found
172173
* @see #registerDefaultEditors
173174
*/
174-
@SuppressWarnings("NullAway")
175175
public @Nullable PropertyEditor getDefaultEditor(Class<?> requiredType) {
176176
if (!this.defaultEditorsActive) {
177177
return null;

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ public BeanDefinitionStoreException(@Nullable String resourceDescription, String
100100
* @param cause the root cause (may be {@code null})
101101
*/
102102
public BeanDefinitionStoreException(
103-
@Nullable String resourceDescription, String beanName, String msg, @Nullable Throwable cause) {
103+
@Nullable String resourceDescription, String beanName, @Nullable String msg, @Nullable Throwable cause) {
104104

105-
super("Invalid bean definition with name '" + beanName + "' defined in " + resourceDescription + ": " + msg,
105+
super(msg == null ?
106+
"Invalid bean definition with name '" + beanName + "' defined in " + resourceDescription :
107+
"Invalid bean definition with name '" + beanName + "' defined in " + resourceDescription + ": " + msg,
106108
cause);
107109
this.resourceDescription = resourceDescription;
108110
this.beanName = beanName;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public abstract class AbstractFactoryBean<T>
7474

7575
private boolean initialized = false;
7676

77-
private @Nullable T singletonInstance;
77+
@SuppressWarnings("NullAway.Init")
78+
private T singletonInstance;
7879

7980
private @Nullable T earlySingletonInstance;
8081

@@ -146,7 +147,6 @@ public void afterPropertiesSet() throws Exception {
146147
* @see #getEarlySingletonInterfaces()
147148
*/
148149
@Override
149-
@SuppressWarnings("NullAway")
150150
public final T getObject() throws Exception {
151151
if (isSingleton()) {
152152
return (this.initialized ? this.singletonInstance : getEarlySingletonInstance());

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

-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ public void setBeanFactory(BeanFactory beanFactory) {
224224
this.beanFactory = beanFactory;
225225
}
226226

227-
@SuppressWarnings("NullAway")
228227
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
229228
StringValueResolver valueResolver) {
230229

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ public class PropertyPathFactoryBean implements FactoryBean<Object>, BeanNameAwa
8989

9090
private @Nullable BeanWrapper targetBeanWrapper;
9191

92-
private @Nullable String targetBeanName;
92+
@SuppressWarnings("NullAway.Init")
93+
private String targetBeanName;
9394

9495
private @Nullable String propertyPath;
9596

9697
private @Nullable Class<?> resultType;
9798

98-
private @Nullable String beanName;
99+
@SuppressWarnings("NullAway.Init")
100+
private String beanName;
99101

100102
private @Nullable BeanFactory beanFactory;
101103

@@ -156,7 +158,6 @@ public void setBeanName(String beanName) {
156158

157159

158160
@Override
159-
@SuppressWarnings("NullAway")
160161
public void setBeanFactory(BeanFactory beanFactory) {
161162
this.beanFactory = beanFactory;
162163

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ else if (this.currentBeanDefinition != null) {
696696
}
697697
}
698698

699-
@SuppressWarnings("NullAway")
699+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
700700
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
701701
XmlReaderContext readerContext = this.groovyDslXmlBeanDefinitionReader.createReaderContext(
702702
new DescriptiveResource("Groovy"));

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public ConstructorResolver(AbstractAutowireCapableBeanFactory beanFactory) {
131131
* or {@code null} if none (-> use constructor argument values from bean definition)
132132
* @return a BeanWrapper for the new instance
133133
*/
134-
@SuppressWarnings("NullAway")
134+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
135135
public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
136136
Constructor<?> @Nullable [] chosenCtors, @Nullable Object @Nullable [] explicitArgs) {
137137

@@ -393,7 +393,7 @@ private boolean isStaticCandidate(Method method, Class<?> factoryClass) {
393393
* method, or {@code null} if none (-> use constructor argument values from bean definition)
394394
* @return a BeanWrapper for the new instance
395395
*/
396-
@SuppressWarnings("NullAway")
396+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
397397
public BeanWrapper instantiateUsingFactoryMethod(
398398
String beanName, RootBeanDefinition mbd, @Nullable Object @Nullable [] explicitArgs) {
399399

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ else if (descriptor.supportsLazyResolution()) {
14991499
return doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
15001500
}
15011501

1502-
@SuppressWarnings("NullAway")
1502+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
15031503
public @Nullable Object doResolveDependency(DependencyDescriptor descriptor, @Nullable String beanName,
15041504
@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {
15051505

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public void addSingletonCallback(String beanName, Consumer<Object> singletonCons
234234
* with, if necessary
235235
* @return the registered singleton object
236236
*/
237-
@SuppressWarnings("NullAway")
237+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
238238
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
239239
Assert.notNull(beanName, "Bean name must not be null");
240240

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDesc
7272
* Match the given dependency type with its generic type information against the given
7373
* candidate bean definition.
7474
*/
75-
@SuppressWarnings("NullAway")
75+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
7676
protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
7777
ResolvableType dependencyType = descriptor.getResolvableType();
7878
if (dependencyType.getType() instanceof Class) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable
145145
}
146146

147147
@Override
148-
@SuppressWarnings("NullAway")
148+
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1113
149149
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
150150
@Nullable Object factoryBean, Method factoryMethod, @Nullable Object... args) {
151151

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ public BeanDefinitionDefaults getBeanDefinitionDefaults() {
407407
* if there were errors during parse. Errors are reported to the
408408
* {@link org.springframework.beans.factory.parsing.ProblemReporter}.
409409
*/
410-
@SuppressWarnings("NullAway")
411410
public @Nullable BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) {
412411
String id = ele.getAttribute(ID_ATTRIBUTE);
413412
String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);
@@ -457,7 +456,8 @@ public BeanDefinitionDefaults getBeanDefinitionDefaults() {
457456
}
458457
}
459458
catch (Exception ex) {
460-
error(ex.getMessage(), ele);
459+
String message = ex.getMessage();
460+
error(message == null ? "" : message, ele);
461461
return null;
462462
}
463463
}

0 commit comments

Comments
 (0)