Refine null-safety in more modules

This commit refines the null-safety in:
 - spring-jdbc
 - spring-r2dbc
 - spring-orm
 - spring-beans
 - spring-aop

See gh-32475
This commit is contained in:
Sébastien Deleuze
2024-03-26 09:46:34 +01:00
parent dea31dd55e
commit 1b563f8ba4
40 changed files with 69 additions and 17 deletions

View File

@@ -69,7 +69,7 @@ public class BeanInstantiationException extends FatalBeanException {
* @param cause the root cause
* @since 4.3
*/
public BeanInstantiationException(Constructor<?> constructor, String msg, @Nullable Throwable cause) {
public BeanInstantiationException(Constructor<?> constructor, @Nullable String msg, @Nullable Throwable cause) {
super("Failed to instantiate [" + constructor.getDeclaringClass().getName() + "]: " + msg, cause);
this.beanClass = constructor.getDeclaringClass();
this.constructor = constructor;
@@ -84,7 +84,7 @@ public class BeanInstantiationException extends FatalBeanException {
* @param cause the root cause
* @since 4.3
*/
public BeanInstantiationException(Method constructingMethod, String msg, @Nullable Throwable cause) {
public BeanInstantiationException(Method constructingMethod, @Nullable String msg, @Nullable Throwable cause) {
super("Failed to instantiate [" + constructingMethod.getReturnType().getName() + "]: " + msg, cause);
this.beanClass = constructingMethod.getReturnType();
this.constructor = null;

View File

@@ -18,6 +18,8 @@ package org.springframework.beans;
import java.beans.PropertyChangeEvent;
import org.springframework.lang.Nullable;
/**
* Thrown when a bean property getter or setter method throws an exception,
* analogous to an InvocationTargetException.
@@ -38,7 +40,7 @@ public class MethodInvocationException extends PropertyAccessException {
* @param propertyChangeEvent the PropertyChangeEvent that resulted in an exception
* @param cause the Throwable raised by the invoked method
*/
public MethodInvocationException(PropertyChangeEvent propertyChangeEvent, Throwable cause) {
public MethodInvocationException(PropertyChangeEvent propertyChangeEvent, @Nullable Throwable cause) {
super(propertyChangeEvent, "Property '" + propertyChangeEvent.getPropertyName() + "' threw exception", cause);
}

View File

@@ -94,7 +94,7 @@ public class BeanCreationException extends FatalBeanException {
* @param beanName the name of the bean requested
* @param msg the detail message
*/
public BeanCreationException(@Nullable String resourceDescription, @Nullable String beanName, String msg) {
public BeanCreationException(@Nullable String resourceDescription, @Nullable String beanName, @Nullable String msg) {
super("Error creating bean with name '" + beanName + "'" +
(resourceDescription != null ? " defined in " + resourceDescription : "") + ": " + msg);
this.resourceDescription = resourceDescription;
@@ -110,7 +110,7 @@ public class BeanCreationException extends FatalBeanException {
* @param msg the detail message
* @param cause the root cause
*/
public BeanCreationException(@Nullable String resourceDescription, String beanName, String msg, Throwable cause) {
public BeanCreationException(@Nullable String resourceDescription, String beanName, @Nullable String msg, Throwable cause) {
this(resourceDescription, beanName, msg);
initCause(cause);
}

View File

@@ -44,7 +44,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
* @param msg the detail message
*/
public UnsatisfiedDependencyException(
@Nullable String resourceDescription, @Nullable String beanName, String propertyName, String msg) {
@Nullable String resourceDescription, @Nullable String beanName, String propertyName, @Nullable String msg) {
super(resourceDescription, beanName,
"Unsatisfied dependency expressed through bean property '" + propertyName + "'" +
@@ -75,7 +75,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
* @since 4.3
*/
public UnsatisfiedDependencyException(
@Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, String msg) {
@Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, @Nullable String msg) {
super(resourceDescription, beanName,
"Unsatisfied dependency expressed through " + injectionPoint +

View File

@@ -37,6 +37,7 @@ import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.ManagedSet;
import org.springframework.javapoet.AnnotationSpec;
import org.springframework.javapoet.CodeBlock;
import org.springframework.lang.Nullable;
/**
* Code generator {@link Delegate} for common bean definition property values.
@@ -101,6 +102,7 @@ abstract class BeanDefinitionPropertyValueCodeGeneratorDelegates {
private static final CodeBlock EMPTY_RESULT = CodeBlock.of("$T.ofEntries()", ManagedMap.class);
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
if (value instanceof ManagedMap<?, ?> managedMap) {
return generateManagedMapCode(valueCodeGenerator, managedMap);
@@ -137,6 +139,7 @@ abstract class BeanDefinitionPropertyValueCodeGeneratorDelegates {
private static class LinkedHashMapDelegate extends MapDelegate {
@Override
@Nullable
protected CodeBlock generateMapCode(ValueCodeGenerator valueCodeGenerator, Map<?, ?> map) {
GeneratedMethods generatedMethods = valueCodeGenerator.getGeneratedMethods();
if (map instanceof LinkedHashMap<?, ?> && generatedMethods != null) {
@@ -172,6 +175,7 @@ abstract class BeanDefinitionPropertyValueCodeGeneratorDelegates {
private static class BeanReferenceDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
if (value instanceof RuntimeBeanReference runtimeBeanReference &&
runtimeBeanReference.getBeanType() != null) {
@@ -193,6 +197,7 @@ abstract class BeanDefinitionPropertyValueCodeGeneratorDelegates {
private static class TypedStringValueDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
if (value instanceof TypedStringValue typedStringValue) {
return generateTypeStringValueCode(valueCodeGenerator, typedStringValue);

View File

@@ -151,6 +151,7 @@ public abstract class AbstractFactoryBean<T>
* @see #getEarlySingletonInterfaces()
*/
@Override
@Nullable
public final T getObject() throws Exception {
if (isSingleton()) {
return (this.initialized ? this.singletonInstance : getEarlySingletonInstance());

View File

@@ -226,6 +226,7 @@ public class FieldRetrievingFactoryBean
}
@Override
@Nullable
public Class<?> getObjectType() {
return (this.fieldObject != null ? this.fieldObject.getType() : null);
}

View File

@@ -136,6 +136,7 @@ public class MethodInvokingFactoryBean extends MethodInvokingBean implements Fac
* or {@code null} if not known in advance.
*/
@Override
@Nullable
public Class<?> getObjectType() {
if (!isPrepared()) {
// Not fully initialized yet -> return null to indicate "not known yet".

View File

@@ -224,6 +224,7 @@ public class PropertyPathFactoryBean implements FactoryBean<Object>, BeanNameAwa
}
@Override
@Nullable
public Class<?> getObjectType() {
return this.resultType;
}

View File

@@ -335,6 +335,7 @@ public class ServiceLocatorFactoryBean implements FactoryBean<Object>, BeanFacto
}
@Override
@Nullable
public Class<?> getObjectType() {
return this.serviceLocatorInterface;
}

View File

@@ -241,6 +241,7 @@ public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationSt
}
@Override
@Nullable
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
// Cast is safe, as CallbackFilter filters are used selectively.
LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);

View File

@@ -1434,6 +1434,7 @@ class ConstructorResolver {
}
@Override
@Nullable
public Object resolveShortcut(BeanFactory beanFactory) {
String shortcut = this.shortcut;
return (shortcut != null ? beanFactory.getBean(shortcut, getDependencyType()) : null);

View File

@@ -342,7 +342,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
}
}
void logDestroyMethodException(Method destroyMethod, Throwable ex) {
void logDestroyMethodException(Method destroyMethod, @Nullable Throwable ex) {
if (logger.isWarnEnabled()) {
String msg = "Custom destroy method '" + destroyMethod.getName() + "' on bean with name '" +
this.beanName + "' propagated an exception";

View File

@@ -83,6 +83,7 @@ public interface InstanceSupplier<T> extends ThrowingSupplier<T> {
return after.applyWithException(registeredBean, InstanceSupplier.this.get(registeredBean));
}
@Override
@Nullable
public Method getFactoryMethod() {
return InstanceSupplier.this.getFactoryMethod();
}
@@ -126,6 +127,7 @@ public interface InstanceSupplier<T> extends ThrowingSupplier<T> {
return supplier.getWithException();
}
@Override
@Nullable
public Method getFactoryMethod() {
return factoryMethod;
}

View File

@@ -277,6 +277,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
@Override
@Nullable
public String getParentName() {
return null;
}

View File

@@ -232,11 +232,13 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
}
@Override
@Nullable
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return getType(name, true);
}
@Override
@Nullable
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
String beanName = BeanFactoryUtils.transformedBeanName(name);