Skip to content

Commit

Permalink
Refine null-safety in more modules
Browse files Browse the repository at this point in the history
This commit refines the null-safety in:
 - spring-jdbc
 - spring-r2dbc
 - spring-orm
 - spring-beans
 - spring-aop

See spring-projectsgh-32475
  • Loading branch information
sdeleuze committed Mar 26, 2024
1 parent dea31dd commit 1b563f8
Show file tree
Hide file tree
Showing 40 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

/**
Expand All @@ -38,6 +39,7 @@ class AspectJAdvisorBeanRegistrationAotProcessor implements BeanRegistrationAotP


@Override
@Nullable
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
if (aspectjPresent) {
Class<?> beanClass = registeredBean.getBeanClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.lang.Nullable;

/**
* {@link BeanDefinitionParser} responsible for parsing the
Expand Down Expand Up @@ -51,6 +52,7 @@ class SpringConfiguredBeanDefinitionParser implements BeanDefinitionParser {


@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
if (!parserContext.getRegistry().containsBeanDefinition(BEAN_CONFIGURER_ASPECT_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public Object getEarlyBeanReference(Object bean, String beanName) {
}

@Override
@Nullable
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
Object cacheKey = getCacheKey(beanClass, beanName);

Expand Down Expand Up @@ -311,6 +312,7 @@ public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, Str
* @see #getAdvicesAndAdvisorsForBean
*/
@Override
@Nullable
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void setBeanNames(String... beanNames) {
* @see #setBeanNames(String...)
*/
@Override
@Nullable
protected TargetSource getCustomTargetSource(Class<?> beanClass, String beanName) {
return (isSupportedBeanName(beanClass, beanName) ?
super.getCustomTargetSource(beanClass, beanName) : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ScopedProxyBeanRegistrationAotProcessor implements BeanRegistrationAotProc


@Override
@Nullable
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
Class<?> beanClass = registeredBean.getBeanClass();
if (beanClass.equals(ScopedProxyFactoryBean.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void setBeanFactory(BeanFactory beanFactory) {


@Override
@Nullable
public Object getObject() {
if (this.proxy == null) {
throw new FactoryBeanNotInitializedException();
Expand All @@ -125,6 +126,7 @@ public Object getObject() {
}

@Override
@Nullable
public Class<?> getObjectType() {
if (this.proxy != null) {
return this.proxy.getClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public BeanInstantiationException(Class<?> beanClass, String msg, @Nullable Thro
* @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;
Expand All @@ -84,7 +84,7 @@ public BeanInstantiationException(Constructor<?> constructor, String msg, @Nulla
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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.
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public BeanCreationException(String beanName, String msg, Throwable cause) {
* @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;
Expand All @@ -110,7 +110,7 @@ public BeanCreationException(@Nullable String resourceDescription, @Nullable Str
* @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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "'" +
Expand Down Expand Up @@ -75,7 +75,7 @@ public UnsatisfiedDependencyException(
* @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 +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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.
Expand Down Expand Up @@ -101,6 +102,7 @@ private static class ManagedMapDelegate implements Delegate {
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);
Expand Down Expand Up @@ -137,6 +139,7 @@ private <K, V> CodeBlock generateManagedMapCode(ValueCodeGenerator valueCodeGene
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) {
Expand Down Expand Up @@ -172,6 +175,7 @@ private CodeBlock generateLinkedHashMapCode(ValueCodeGenerator valueCodeGenerato
private static class BeanReferenceDelegate implements Delegate {

@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
if (value instanceof RuntimeBeanReference runtimeBeanReference &&
runtimeBeanReference.getBeanType() != null) {
Expand All @@ -193,6 +197,7 @@ else if (value instanceof BeanReference beanReference) {
private static class TypedStringValueDelegate implements Delegate {

@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
if (value instanceof TypedStringValue typedStringValue) {
return generateTypeStringValueCode(valueCodeGenerator, typedStringValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public void afterPropertiesSet() throws Exception {
* @see #getEarlySingletonInterfaces()
*/
@Override
@Nullable
public final T getObject() throws Exception {
if (isSingleton()) {
return (this.initialized ? this.singletonInstance : getEarlySingletonInstance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public Object getObject() throws IllegalAccessException {
}

@Override
@Nullable
public Class<?> getObjectType() {
return (this.fieldObject != null ? this.fieldObject.getType() : null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public Object getObject() throws Exception {
* 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".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public Object getObject() throws BeansException {
}

@Override
@Nullable
public Class<?> getObjectType() {
return this.resultType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public Object getObject() {
}

@Override
@Nullable
public Class<?> getObjectType() {
return this.serviceLocatorInterface;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public LookupOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFa
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ public boolean hasShortcut() {
}

@Override
@Nullable
public Object resolveShortcut(BeanFactory beanFactory) {
String shortcut = this.shortcut;
return (shortcut != null ? beanFactory.getBean(shortcut, getDependencyType()) : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ else if (!reactiveStreamsPresent || !new ReactiveDestroyMethodHandler().await(de
}
}

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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public V get(RegisteredBean registeredBean) throws Exception {
return after.applyWithException(registeredBean, InstanceSupplier.this.get(registeredBean));
}
@Override
@Nullable
public Method getFactoryMethod() {
return InstanceSupplier.this.getFactoryMethod();
}
Expand Down Expand Up @@ -126,6 +127,7 @@ public T get(RegisteredBean registeredBean) throws Exception {
return supplier.getWithException();
}
@Override
@Nullable
public Method getFactoryMethod() {
return factoryMethod;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ public RootBeanDefinition(RootBeanDefinition original) {


@Override
@Nullable
public String getParentName() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,13 @@ public boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws N
}

@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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void setStaticMethod(String staticMethod) {
* Set arguments for the method invocation. If this property is not set,
* or the Object array is of length 0, a method with no arguments is assumed.
*/
public void setArguments(Object... arguments) {
public void setArguments(@Nullable Object... arguments) {
this.arguments = arguments;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void handleInvocationTargetException(InvocationTargetException ex)
* @param ex the exception to rethrow
* @throws RuntimeException the rethrown exception
*/
public static void rethrowRuntimeException(Throwable ex) {
public static void rethrowRuntimeException(@Nullable Throwable ex) {
if (ex instanceof RuntimeException runtimeException) {
throw runtimeException;
}
Expand All @@ -158,7 +158,7 @@ public static void rethrowRuntimeException(Throwable ex) {
* @param throwable the exception to rethrow
* @throws Exception the rethrown exception (in case of a checked exception)
*/
public static void rethrowException(Throwable throwable) throws Exception {
public static void rethrowException(@Nullable Throwable throwable) throws Exception {
if (throwable instanceof Exception exception) {
throw exception;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.sql.SQLException;

import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.lang.Nullable;

/**
* Exception thrown when SQL specified is invalid. Such exceptions always have
Expand Down Expand Up @@ -52,6 +53,7 @@ public BadSqlGrammarException(String task, String sql, SQLException ex) {
/**
* Return the wrapped SQLException.
*/
@Nullable
public SQLException getSQLException() {
return (SQLException) getCause();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public InvalidResultSetAccessException(SQLException ex) {
/**
* Return the wrapped SQLException.
*/
@Nullable
public SQLException getSQLException() {
return (SQLException) getCause();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.sql.SQLWarning;

import org.springframework.dao.UncategorizedDataAccessException;
import org.springframework.lang.Nullable;

/**
* Exception thrown when we're not ignoring {@link java.sql.SQLWarning SQLWarnings}.
Expand Down Expand Up @@ -49,6 +50,7 @@ public SQLWarningException(String msg, SQLWarning ex) {
* Return the underlying {@link SQLWarning}.
* @since 5.3.29
*/
@Nullable
public SQLWarning getSQLWarning() {
return (SQLWarning) getCause();
}
Expand All @@ -58,6 +60,7 @@ public SQLWarning getSQLWarning() {
* @deprecated as of 5.3.29, in favor of {@link #getSQLWarning()}
*/
@Deprecated(since = "5.3.29")
@Nullable
public SQLWarning SQLWarning() {
return getSQLWarning();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public UncategorizedSQLException(String task, @Nullable String sql, SQLException
/**
* Return the underlying SQLException.
*/
@Nullable
public SQLException getSQLException() {
return (SQLException) getCause();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,11 +907,13 @@ public <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T>

@Deprecated
@Override
@Nullable
public <T> T queryForObject(String sql, @Nullable Object[] args, Class<T> requiredType) throws DataAccessException {
return queryForObject(sql, args, getSingleColumnRowMapper(requiredType));
}

@Override
@Nullable
public <T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args) throws DataAccessException {
return queryForObject(sql, args, getSingleColumnRowMapper(requiredType));
}
Expand Down
Loading

0 comments on commit 1b563f8

Please sign in to comment.