Skip to content

Commit a6dab10

Browse files
committed
Update code regarding null-safety semantics
See gh-30083
1 parent b617e16 commit a6dab10

File tree

33 files changed

+120
-46
lines changed

33 files changed

+120
-46
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/AbstractBeanFactoryBasedTargetSourceCreator.java

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public abstract class AbstractBeanFactoryBasedTargetSourceCreator
5858

5959
protected final Log logger = LogFactory.getLog(getClass());
6060

61+
@Nullable
6162
private ConfigurableBeanFactory beanFactory;
6263

6364
/** Internally used DefaultListableBeanFactory instances, keyed by bean name. */
@@ -76,6 +77,7 @@ public final void setBeanFactory(BeanFactory beanFactory) {
7677
/**
7778
* Return the BeanFactory that this TargetSourceCreators runs in.
7879
*/
80+
@Nullable
7981
protected final BeanFactory getBeanFactory() {
8082
return this.beanFactory;
8183
}

spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.aop.TargetSource;
2525
import org.springframework.beans.factory.BeanFactory;
2626
import org.springframework.beans.factory.BeanFactoryAware;
27+
import org.springframework.lang.Nullable;
2728
import org.springframework.util.ObjectUtils;
2829

2930
/**
@@ -169,7 +170,7 @@ protected void copyFrom(AbstractBeanFactoryBasedTargetSource other) {
169170

170171

171172
@Override
172-
public boolean equals(Object other) {
173+
public boolean equals(@Nullable Object other) {
173174
if (this == other) {
174175
return true;
175176
}

spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private Object readResolve() {
131131
}
132132

133133
@Override
134-
public boolean equals(Object other) {
134+
public boolean equals(@Nullable Object other) {
135135
if (this == other) {
136136
return true;
137137
}

spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.Serializable;
2020

2121
import org.springframework.aop.TargetSource;
22+
import org.springframework.lang.Nullable;
2223
import org.springframework.util.Assert;
2324

2425
/**
@@ -100,7 +101,7 @@ public synchronized Object swap(Object newTarget) throws IllegalArgumentExceptio
100101
* objects are equal.
101102
*/
102103
@Override
103-
public boolean equals(Object obj) {
104+
public boolean equals(@Nullable Object obj) {
104105
return (this == obj || (obj instanceof HotSwappableTargetSource that &&
105106
this.target.equals(that.target)));
106107
}

spring-aop/src/main/java/org/springframework/aop/target/LazyInitTargetSource.java

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public class LazyInitTargetSource extends AbstractBeanFactoryBasedTargetSource {
6565

6666

6767
@Override
68-
@Nullable
6968
public synchronized Object getTarget() throws BeansException {
7069
if (this.target == null) {
7170
this.target = getBeanFactory().getBean(getTargetBeanName());

spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.Serializable;
2020

2121
import org.springframework.aop.TargetSource;
22+
import org.springframework.lang.Nullable;
2223
import org.springframework.util.Assert;
2324
import org.springframework.util.ObjectUtils;
2425

@@ -82,7 +83,7 @@ public boolean isStatic() {
8283
* targets or the targets are equal.
8384
*/
8485
@Override
85-
public boolean equals(Object other) {
86+
public boolean equals(@Nullable Object other) {
8687
if (this == other) {
8788
return true;
8889
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,19 @@ else if (methodName.startsWith("is") && method.getParameterCount() == 0 && metho
8888
BasicPropertyDescriptor pd = pdMap.get(propertyName);
8989
if (pd != null) {
9090
if (setter) {
91-
if (pd.getWriteMethod() == null ||
92-
pd.getWriteMethod().getParameterTypes()[0].isAssignableFrom(method.getParameterTypes()[0])) {
91+
Method writedMethod = pd.getWriteMethod();
92+
if (writedMethod == null ||
93+
writedMethod.getParameterTypes()[0].isAssignableFrom(method.getParameterTypes()[0])) {
9394
pd.setWriteMethod(method);
9495
}
9596
else {
9697
pd.addWriteMethod(method);
9798
}
9899
}
99100
else {
100-
if (pd.getReadMethod() == null ||
101-
(pd.getReadMethod().getReturnType() == method.getReturnType() && method.getName().startsWith("is"))) {
101+
Method readMethod = pd.getReadMethod();
102+
if (readMethod == null ||
103+
(readMethod.getReturnType() == method.getReturnType() && method.getName().startsWith("is"))) {
102104
pd.setReadMethod(method);
103105
}
104106
}

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, C
280280
}
281281

282282
@Override
283+
@Nullable
283284
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
284285
Class<?> beanClass = registeredBean.getBeanClass();
285286
String beanName = registeredBean.getBeanName();
@@ -323,10 +324,10 @@ public Class<?> determineBeanType(Class<?> beanClass, String beanName) throws Be
323324
checkLookupMethods(beanClass, beanName);
324325

325326
// Pick up subclass with fresh lookup method override from above
326-
if (this.beanFactory instanceof AbstractAutowireCapableBeanFactory aacbf) {
327+
if (this.beanFactory instanceof AbstractAutowireCapableBeanFactory aacBeanFactory) {
327328
RootBeanDefinition mbd = (RootBeanDefinition) this.beanFactory.getMergedBeanDefinition(beanName);
328329
if (mbd.getFactoryMethodName() == null && mbd.hasBeanClass()) {
329-
return aacbf.getInstantiationStrategy().getActualBeanClass(mbd, beanName, this.beanFactory);
330+
return aacBeanFactory.getInstantiationStrategy().getActualBeanClass(mbd, beanName, aacBeanFactory);
330331
}
331332
}
332333
return beanClass;

spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, C
157157
}
158158

159159
@Override
160+
@Nullable
160161
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
161162
RootBeanDefinition beanDefinition = registeredBean.getMergedBeanDefinition();
162163
beanDefinition.resolveDestroyMethodIfNecessary();

spring-beans/src/main/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHints.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.aot.hint.RuntimeHints;
2222
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.lang.Nullable;
2324
import org.springframework.util.ClassUtils;
2425

2526
/**
@@ -31,7 +32,7 @@
3132
class JakartaAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
3233

3334
@Override
34-
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
35+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
3536
if (ClassUtils.isPresent("jakarta.inject.Inject", classLoader)) {
3637
Stream.of("jakarta.inject.Inject", "jakarta.inject.Qualifier").forEach(annotationType ->
3738
hints.reflection().registerType(ClassUtils.resolveClassName(annotationType, classLoader)));

spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.springframework.beans.SimpleTypeConverter;
2727
import org.springframework.beans.TypeConverter;
28+
import org.springframework.beans.factory.BeanFactory;
2829
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2930
import org.springframework.beans.factory.config.BeanDefinitionHolder;
3031
import org.springframework.beans.factory.config.DependencyDescriptor;
@@ -240,10 +241,11 @@ protected boolean checkQualifier(
240241
}
241242
}
242243
if (targetAnnotation == null) {
244+
BeanFactory beanFactory = getBeanFactory();
243245
// Look for matching annotation on the target class
244-
if (getBeanFactory() != null) {
246+
if (beanFactory != null) {
245247
try {
246-
Class<?> beanType = getBeanFactory().getType(bdHolder.getBeanName());
248+
Class<?> beanType = beanFactory.getType(bdHolder.getBeanName());
247249
if (beanType != null) {
248250
targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
249251
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public Map<Integer, ValueHolder> getIndexedArgumentValues() {
188188
* rather than matched multiple times.
189189
* @param value the argument value
190190
*/
191-
public void addGenericArgumentValue(Object value) {
191+
public void addGenericArgumentValue(@Nullable Object value) {
192192
this.genericArgumentValues.add(new ValueHolder(value));
193193
}
194194

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

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.core.io.DescriptiveResource;
5454
import org.springframework.core.io.Resource;
5555
import org.springframework.core.io.support.EncodedResource;
56+
import org.springframework.lang.Nullable;
5657
import org.springframework.util.ObjectUtils;
5758
import org.springframework.util.StringUtils;
5859

@@ -149,8 +150,10 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
149150

150151
private MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(getClass());
151152

153+
@Nullable
152154
private Binding binding;
153155

156+
@Nullable
154157
private GroovyBeanDefinitionWrapper currentBeanDefinition;
155158

156159

@@ -203,6 +206,7 @@ public void setBinding(Binding binding) {
203206
/**
204207
* Return a specified binding for Groovy variables, if any.
205208
*/
209+
@Nullable
206210
public Binding getBinding() {
207211
return this.binding;
208212
}

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.beans.factory.config.RuntimeBeanReference;
3131
import org.springframework.beans.factory.support.AbstractBeanDefinition;
3232
import org.springframework.beans.factory.support.GenericBeanDefinition;
33+
import org.springframework.lang.Nullable;
34+
import org.springframework.util.Assert;
3335
import org.springframework.util.CollectionUtils;
3436

3537
/**
@@ -55,35 +57,41 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
5557
FACTORY_BEAN, FACTORY_METHOD, INIT_METHOD, DESTROY_METHOD, SINGLETON);
5658

5759

60+
@Nullable
5861
private String beanName;
5962

60-
private Class<?> clazz;
63+
@Nullable
64+
private final Class<?> clazz;
6165

62-
private Collection<?> constructorArgs;
66+
@Nullable
67+
private final Collection<?> constructorArgs;
6368

69+
@Nullable
6470
private AbstractBeanDefinition definition;
6571

72+
@Nullable
6673
private BeanWrapper definitionWrapper;
6774

75+
@Nullable
6876
private String parentName;
6977

7078

71-
public GroovyBeanDefinitionWrapper(String beanName) {
72-
this.beanName = beanName;
79+
GroovyBeanDefinitionWrapper(String beanName) {
80+
this(beanName, null);
7381
}
7482

75-
public GroovyBeanDefinitionWrapper(String beanName, Class<?> clazz) {
76-
this.beanName = beanName;
77-
this.clazz = clazz;
83+
GroovyBeanDefinitionWrapper(@Nullable String beanName, @Nullable Class<?> clazz) {
84+
this(beanName, clazz, null);
7885
}
7986

80-
public GroovyBeanDefinitionWrapper(String beanName, Class<?> clazz, Collection<?> constructorArgs) {
87+
GroovyBeanDefinitionWrapper(@Nullable String beanName, Class<?> clazz, @Nullable Collection<?> constructorArgs) {
8188
this.beanName = beanName;
8289
this.clazz = clazz;
8390
this.constructorArgs = constructorArgs;
8491
}
8592

8693

94+
@Nullable
8795
public String getBeanName() {
8896
return this.beanName;
8997
}
@@ -151,6 +159,7 @@ GroovyBeanDefinitionWrapper addProperty(String propertyName, Object propertyValu
151159

152160
@Override
153161
public Object getProperty(String property) {
162+
Assert.state(this.definitionWrapper != null, "BeanDefinition wrapper not initialized");
154163
if (this.definitionWrapper.isReadableProperty(property)) {
155164
return this.definitionWrapper.getPropertyValue(property);
156165
}
@@ -167,6 +176,7 @@ public void setProperty(String property, Object newValue) {
167176
}
168177
else {
169178
AbstractBeanDefinition bd = getBeanDefinition();
179+
Assert.state(this.definitionWrapper != null, "BeanDefinition wrapper not initialized");
170180
if (AUTOWIRE.equals(property)) {
171181
if ("byName".equals(newValue)) {
172182
bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);

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

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.beans.factory.config.BeanDefinitionHolder;
3232
import org.springframework.beans.factory.support.AbstractBeanDefinition;
3333
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
34+
import org.springframework.lang.Nullable;
3435

3536
/**
3637
* Used by GroovyBeanDefinitionReader to read a Spring XML namespace expression
@@ -68,6 +69,7 @@ public GroovyDynamicElementReader(String namespace, Map<String, String> namespac
6869

6970

7071
@Override
72+
@Nullable
7173
public Object invokeMethod(String name, Object obj) {
7274
Object[] args = (Object[]) obj;
7375
if (name.equals("doCall")) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,7 @@ public AutowireByTypeDependencyDescriptor(MethodParameter methodParameter, boole
19581958
}
19591959

19601960
@Override
1961+
@Nullable
19611962
public String getDependencyName() {
19621963
return null;
19631964
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -879,10 +879,12 @@ public void setPropertyValues(MutablePropertyValues propertyValues) {
879879
*/
880880
@Override
881881
public MutablePropertyValues getPropertyValues() {
882-
if (this.propertyValues == null) {
883-
this.propertyValues = new MutablePropertyValues();
882+
MutablePropertyValues pvs = this.propertyValues;
883+
if (pvs == null) {
884+
pvs = new MutablePropertyValues();
885+
this.propertyValues = pvs;
884886
}
885-
return this.propertyValues;
887+
return pvs;
886888
}
887889

888890
/**

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ protected boolean isActuallyInCreation(String beanName) {
340340
* (within the entire factory).
341341
* @param beanName the name of the bean
342342
*/
343-
public boolean isSingletonCurrentlyInCreation(String beanName) {
343+
public boolean isSingletonCurrentlyInCreation(@Nullable String beanName) {
344344
return this.singletonsCurrentlyInCreation.contains(beanName);
345345
}
346346

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ public NamespaceHandlerResolver getNamespaceHandlerResolver() {
547547
* @see DefaultNamespaceHandlerResolver#DefaultNamespaceHandlerResolver(ClassLoader)
548548
*/
549549
protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
550-
ClassLoader cl = (getResourceLoader() != null ? getResourceLoader().getClassLoader() : getBeanClassLoader());
550+
ResourceLoader resourceLoader = getResourceLoader();
551+
ClassLoader cl = (resourceLoader != null ? resourceLoader.getClassLoader() : getBeanClassLoader());
551552
return new DefaultNamespaceHandlerResolver(cl);
552553
}
553554

spring-beans/src/main/java/org/springframework/beans/support/ArgumentConvertingMethodInvoker.java

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyE
111111
* @see #doFindMatchingMethod
112112
*/
113113
@Override
114+
@Nullable
114115
protected Method findMatchingMethod() {
115116
Method matchingMethod = super.findMatchingMethod();
116117
// Second pass: look for method where arguments can be converted to parameter types.

spring-context/src/main/java/org/springframework/context/config/SpringConfiguredBeanDefinitionParser.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.beans.factory.support.RootBeanDefinition;
2424
import org.springframework.beans.factory.xml.BeanDefinitionParser;
2525
import org.springframework.beans.factory.xml.ParserContext;
26+
import org.springframework.lang.Nullable;
2627

2728
/**
2829
* {@link BeanDefinitionParser} responsible for parsing the
@@ -44,6 +45,7 @@ class SpringConfiguredBeanDefinitionParser implements BeanDefinitionParser {
4445

4546

4647
@Override
48+
@Nullable
4749
public BeanDefinition parse(Element element, ParserContext parserContext) {
4850
if (!parserContext.getRegistry().containsBeanDefinition(BEAN_CONFIGURER_ASPECT_BEAN_NAME)) {
4951
RootBeanDefinition def = new RootBeanDefinition();

spring-web/src/main/java/org/springframework/web/context/request/async/CallableInterceptorChain.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
2525

26+
import org.springframework.lang.Nullable;
2627
import org.springframework.web.context.request.NativeWebRequest;
2728

2829
/**
@@ -40,6 +41,7 @@ class CallableInterceptorChain {
4041

4142
private int preProcessIndex = -1;
4243

44+
@Nullable
4345
private volatile Future<?> taskFuture;
4446

4547

@@ -66,7 +68,7 @@ public void applyPreProcess(NativeWebRequest request, Callable<?> task) throws E
6668
}
6769
}
6870

69-
public Object applyPostProcess(NativeWebRequest request, Callable<?> task, Object concurrentResult) {
71+
public Object applyPostProcess(NativeWebRequest request, Callable<?> task, @Nullable Object concurrentResult) {
7072
Throwable exceptionResult = null;
7173
for (int i = this.preProcessIndex; i >= 0; i--) {
7274
try {

0 commit comments

Comments
 (0)