Refine null-safety in more modules

This commit refines the null-safety in all remaining modules
except spring-test.

See gh-32475
This commit is contained in:
Sébastien Deleuze
2024-03-26 15:39:18 +01:00
parent 1b563f8ba4
commit 290a41d398
88 changed files with 172 additions and 72 deletions

View File

@@ -315,7 +315,7 @@ public interface Cache {
@Nullable
private final Object key;
public ValueRetrievalException(@Nullable Object key, Callable<?> loader, Throwable ex) {
public ValueRetrievalException(@Nullable Object key, Callable<?> loader, @Nullable Throwable ex) {
super(String.format("Value for key '%s' could not be loaded using '%s'", key, loader), ex);
this.key = key;
}

View File

@@ -223,6 +223,7 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
}
@Override
@Nullable
protected Object fromStoreValue(@Nullable Object storeValue) {
if (storeValue != null && this.serialization != null) {
try {

View File

@@ -52,6 +52,7 @@ public class NamedCacheResolver extends AbstractCacheResolver {
}
@Override
@Nullable
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
return this.cacheNames;
}

View File

@@ -314,6 +314,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
}
@Override
@Nullable
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
BeanRegistrationAotContribution parentAotContribution = super.processAheadOfTime(registeredBean);
Class<?> beanClass = registeredBean.getBeanClass();
@@ -350,6 +351,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
}
@Override
@Nullable
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
return null;
}

View File

@@ -334,6 +334,7 @@ class ConfigurationClassEnhancer {
return resolveBeanReference(beanMethod, beanMethodArgs, beanFactory, beanName);
}
@Nullable
private Object resolveBeanReference(Method beanMethod, Object[] beanMethodArgs,
ConfigurableBeanFactory beanFactory, String beanName) {

View File

@@ -350,7 +350,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
* Invoke the event listener method with the given argument values.
*/
@Nullable
protected Object doInvoke(Object... args) {
protected Object doInvoke(@Nullable Object... args) {
Object bean = getTargetBean();
// Detect package-protected NullBean instance through equals(null) check
if (bean.equals(null)) {
@@ -416,8 +416,8 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
* the given error message.
* @param message error message to append the HandlerMethod details to
*/
protected String getDetailedErrorMessage(Object bean, String message) {
StringBuilder sb = new StringBuilder(message).append('\n');
protected String getDetailedErrorMessage(Object bean, @Nullable String message) {
StringBuilder sb = (StringUtils.hasLength(message) ? new StringBuilder(message).append('\n') : new StringBuilder());
sb.append("HandlerMethod details: \n");
sb.append("Bean [").append(bean.getClass().getName()).append("]\n");
sb.append("Method [").append(this.method.toGenericString()).append("]\n");
@@ -431,7 +431,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
* beans, and others). Event listener beans that require proxying should prefer
* class-based proxy mechanisms.
*/
private void assertTargetBean(Method method, Object targetBean, Object[] args) {
private void assertTargetBean(Method method, Object targetBean, @Nullable Object[] args) {
Class<?> methodDeclaringClass = method.getDeclaringClass();
Class<?> targetBeanClass = targetBean.getClass();
if (!methodDeclaringClass.isAssignableFrom(targetBeanClass)) {
@@ -443,7 +443,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
}
}
private String getInvocationErrorMessage(Object bean, String message, Object[] resolvedArgs) {
private String getInvocationErrorMessage(Object bean, @Nullable String message, @Nullable Object[] resolvedArgs) {
StringBuilder sb = new StringBuilder(getDetailedErrorMessage(bean, message));
sb.append("Resolved arguments: \n");
for (int i = 0; i < resolvedArgs.length; i++) {

View File

@@ -1463,6 +1463,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
//---------------------------------------------------------------------
@Override
@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
return getMessageSource().getMessage(code, args, defaultMessage, locale);
}

View File

@@ -137,6 +137,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
@Override
@Nullable
public final String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
String msg = getMessageInternal(code, args, locale);
if (msg != null) {

View File

@@ -123,6 +123,7 @@ class ContextTypeMatchClassLoader extends DecoratingClassLoader implements Smart
}
@Override
@Nullable
protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException {
byte[] bytes = bytesCache.get(name);
if (bytes == null) {

View File

@@ -191,6 +191,7 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased
* returning the value found in the bundle as-is (without MessageFormat parsing).
*/
@Override
@Nullable
protected String resolveCodeWithoutArguments(String code, Locale locale) {
if (getCacheMillis() < 0) {
PropertiesHolder propHolder = getMergedProperties(locale);

View File

@@ -145,6 +145,7 @@ public class ResourceBundleMessageSource extends AbstractResourceBasedMessageSou
* returning the value found in the bundle as-is (without MessageFormat parsing).
*/
@Override
@Nullable
protected String resolveCodeWithoutArguments(String code, Locale locale) {
Set<String> basenames = getBasenameSet();
for (String basename : basenames) {

View File

@@ -19,6 +19,7 @@ package org.springframework.format.support;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.lang.Nullable;
/**
* {@link RuntimeHintsRegistrar} to register hints for {@link DefaultFormattingConversionService}.
@@ -29,7 +30,7 @@ import org.springframework.aot.hint.TypeReference;
class FormattingConversionServiceRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
hints.reflection().registerType(TypeReference.of("javax.money.MonetaryAmount"));
}
}

View File

@@ -18,6 +18,8 @@ package org.springframework.jmx.access;
import javax.management.JMRuntimeException;
import org.springframework.lang.Nullable;
/**
* Thrown when trying to invoke an operation on a proxy that is not exposed
* by the proxied MBean resource's management interface.
@@ -35,7 +37,7 @@ public class InvalidInvocationException extends JMRuntimeException {
* error message.
* @param msg the detail message
*/
public InvalidInvocationException(String msg) {
public InvalidInvocationException(@Nullable String msg) {
super(msg);
}

View File

@@ -108,6 +108,7 @@ public class MBeanProxyFactoryBean extends MBeanClientInterceptor
}
@Override
@Nullable
public Class<?> getObjectType() {
return this.proxyInterface;
}

View File

@@ -273,6 +273,7 @@ public class JndiObjectFactoryBean extends JndiObjectLocator
}
@Override
@Nullable
public Class<?> getObjectType() {
if (this.proxyInterfaces != null) {
if (this.proxyInterfaces.length == 1) {

View File

@@ -34,6 +34,7 @@ import org.springframework.lang.Nullable;
public class AsyncConfigurerSupport implements AsyncConfigurer {
@Override
@Nullable
public Executor getAsyncExecutor() {
return null;
}

View File

@@ -334,16 +334,19 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
}
@Override
@Nullable
public Instant lastScheduledExecution() {
return (this.le != null ? toInstant(this.le.getScheduledStart()) : null);
}
@Override
@Nullable
public Instant lastActualExecution() {
return (this.le != null ? toInstant(this.le.getRunStart()) : null);
}
@Override
@Nullable
public Instant lastCompletion() {
return (this.le != null ? toInstant(this.le.getRunEnd()) : null);
}

View File

@@ -106,6 +106,7 @@ public class TaskSchedulerRouter implements TaskScheduler, BeanNameAware, BeanFa
@Override
@Nullable
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
return determineTargetScheduler(task).schedule(task, trigger);
}

View File

@@ -112,6 +112,7 @@ public class CronTrigger implements Trigger {
* previous execution; therefore, overlapping executions won't occur.
*/
@Override
@Nullable
public Instant nextExecution(TriggerContext triggerContext) {
Instant timestamp = determineLatestTimestamp(triggerContext);
ZoneId zone = (this.zoneId != null ? this.zoneId : triggerContext.getClock().getZone());

View File

@@ -338,6 +338,7 @@ final class QuartzCronField extends CronField {
@Override
@Nullable
public <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal) {
T result = adjust(temporal);
if (result != null) {

View File

@@ -22,6 +22,7 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
@@ -38,6 +39,7 @@ class ScriptingDefaultsParser implements BeanDefinitionParser {
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
BeanDefinition bd =
LangNamespaceUtils.registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry());

View File

@@ -305,6 +305,7 @@ public class ScriptFactoryPostProcessor implements SmartInstantiationAwareBeanPo
}
@Override
@Nullable
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
// We only apply special treatment to ScriptFactory implementations here.
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {

View File

@@ -70,6 +70,7 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul
* @see #getPropertyAccessor()
*/
@Override
@Nullable
public PropertyEditorRegistry getPropertyEditorRegistry() {
return (getTarget() != null ? getPropertyAccessor() : null);
}
@@ -109,6 +110,7 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul
* @see #getCustomEditor
*/
@Override
@Nullable
protected Object formatFieldValue(String field, @Nullable Object value) {
String fixedField = fixedField(field);
// Try custom editor...

View File

@@ -147,6 +147,7 @@ public class SimpleErrors implements Errors, Serializable {
}
@Override
@Nullable
public Class<?> getFieldType(String field) {
PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(this.target.getClass(), field);
if (pd != null) {

View File

@@ -147,6 +147,7 @@ public class ParameterErrors extends ParameterValidationResult implements Errors
}
@Override
@Nullable
public ObjectError getGlobalError() {
return this.errors.getGlobalError();
}
@@ -167,6 +168,7 @@ public class ParameterErrors extends ParameterValidationResult implements Errors
}
@Override
@Nullable
public FieldError getFieldError() {
return this.errors.getFieldError();
}
@@ -187,16 +189,19 @@ public class ParameterErrors extends ParameterValidationResult implements Errors
}
@Override
@Nullable
public FieldError getFieldError(String field) {
return this.errors.getFieldError(field);
}
@Override
@Nullable
public Object getFieldValue(String field) {
return this.errors.getFieldError(field);
}
@Override
@Nullable
public Class<?> getFieldType(String field) {
return this.errors.getFieldType(field);
}