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 all remaining modules
except spring-test.

See spring-projectsgh-32475
  • Loading branch information
sdeleuze committed Mar 26, 2024
1 parent 1b563f8 commit 290a41d
Show file tree
Hide file tree
Showing 88 changed files with 172 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class LazyInitTargetSource extends AbstractBeanFactoryBasedTargetSource {


@Override
@Nullable
public synchronized Object getTarget() throws BeansException {
if (this.target == null) {
this.target = getBeanFactory().getBean(getTargetBeanName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected Collection<Cache> loadCaches() {
}

@Override
@Nullable
protected Cache getMissingCache(String name) {
CacheManager cacheManager = getCacheManager();
Assert.state(cacheManager != null, "No CacheManager set");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
public abstract class AnnotationJCacheOperationSource extends AbstractFallbackJCacheOperationSource {

@Override
@Nullable
protected JCacheOperation<?> findCacheOperation(Method method, @Nullable Class<?> targetType) {
CacheResult cacheResult = method.getAnnotation(CacheResult.class);
CachePut cachePut = method.getAnnotation(CachePut.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheOperationInvoker;
import org.springframework.lang.Nullable;

/**
* Intercept methods annotated with {@link CachePut}.
Expand All @@ -39,6 +40,7 @@ public CachePutInterceptor(CacheErrorHandler errorHandler) {


@Override
@Nullable
protected Object invoke(
CacheOperationInvocationContext<CachePutOperation> context, CacheOperationInvoker invoker) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheOperationInvoker;
import org.springframework.lang.Nullable;

/**
* Intercept methods annotated with {@link CacheRemoveAll}.
Expand All @@ -38,6 +39,7 @@ protected CacheRemoveAllInterceptor(CacheErrorHandler errorHandler) {


@Override
@Nullable
protected Object invoke(
CacheOperationInvocationContext<CacheRemoveAllOperation> context, CacheOperationInvoker invoker) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheOperationInvoker;
import org.springframework.lang.Nullable;

/**
* Intercept methods annotated with {@link CacheRemove}.
Expand All @@ -38,6 +39,7 @@ protected CacheRemoveEntryInterceptor(CacheErrorHandler errorHandler) {


@Override
@Nullable
protected Object invoke(
CacheOperationInvocationContext<CacheRemoveOperation> context, CacheOperationInvoker invoker) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public CacheOperationInvokerAdapter(CacheOperationInvoker delegate) {
}

@Override
@Nullable
public Object invoke() throws ThrowableWrapper {
return invokeOperation(this.delegate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.cache.interceptor.BasicOperation;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.lang.Nullable;

/**
* A simple {@link CacheResolver} that resolves the exception cache
Expand All @@ -41,6 +42,7 @@ public SimpleExceptionCacheResolver(CacheManager cacheManager) {
}

@Override
@Nullable
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
BasicOperation operation = context.getOperation();
if (!(operation instanceof CacheResultOperation cacheResultOperation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public ValueWrapper get(Object key) {
}

@Override
@Nullable
public <T> T get(Object key, @Nullable Class<T> type) {
return this.targetCache.get(key, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ protected void postProcessJobDetail(JobDetail jobDetail) {
* Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
*/
@Override
@Nullable
public Class<?> getTargetClass() {
Class<?> targetClass = super.getTargetClass();
if (targetClass == null && this.targetBeanName != null) {
Expand All @@ -212,6 +213,7 @@ public Class<?> getTargetClass() {
* Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
*/
@Override
@Nullable
public Object getTargetObject() {
Object targetObject = super.getTargetObject();
if (targetObject == null && this.targetBeanName != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.annotation.ReflectiveRuntimeHintsRegistrar;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

/**
Expand All @@ -40,7 +41,7 @@ class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar {


@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
if (!ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class ValueRetrievalException extends RuntimeException {
@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ protected Object toStoreValue(@Nullable Object userValue) {
}

@Override
@Nullable
protected Object fromStoreValue(@Nullable Object storeValue) {
if (storeValue != null && this.serialization != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void setCacheNames(Collection<String> cacheNames) {
}

@Override
@Nullable
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
return this.cacheNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, C
}

@Override
@Nullable
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
BeanRegistrationAotContribution parentAotContribution = super.processAheadOfTime(registeredBean);
Class<?> beanClass = registeredBean.getBeanClass();
Expand Down Expand Up @@ -350,6 +351,7 @@ public void resetBeanDefinition(String beanName) {
}

@Override
@Nullable
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object
return resolveBeanReference(beanMethod, beanMethodArgs, beanFactory, beanName);
}

@Nullable
private Object resolveBeanReference(Method beanMethod, Object[] beanMethodArgs,
ConfigurableBeanFactory beanFactory, String beanName) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ protected void handleAsyncError(Throwable t) {
* 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)) {
Expand Down Expand Up @@ -416,8 +416,8 @@ protected String getCondition() {
* 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");
Expand All @@ -431,7 +431,7 @@ protected String getDetailedErrorMessage(Object bean, String message) {
* 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)) {
Expand All @@ -443,7 +443,7 @@ private void assertTargetBean(Method method, Object targetBean, Object[] args) {
}
}

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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,7 @@ protected BeanFactory getInternalParentBeanFactory() {
//---------------------------------------------------------------------

@Override
@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
return getMessageSource().getMessage(code, args, defaultMessage, locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ protected boolean isUseCodeAsDefaultMessage() {


@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ protected boolean isEligibleForOverriding(String className) {
}

@Override
@Nullable
protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException {
byte[] bytes = bytesCache.get(name);
if (bytes == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public void setResourceLoader(@Nullable ResourceLoader resourceLoader) {
* 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public void setBeanClassLoader(ClassLoader classLoader) {
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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}.
Expand All @@ -29,7 +30,7 @@
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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public Object getObject() {
}

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

@Override
@Nullable
public Class<?> getObjectType() {
if (this.proxyInterfaces != null) {
if (this.proxyInterfaces.length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class AsyncConfigurerSupport implements AsyncConfigurer {

@Override
@Nullable
public Executor getAsyncExecutor() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,19 @@ public LastExecutionAdapter(@Nullable LastExecution le) {
}

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void setBeanFactory(@Nullable BeanFactory beanFactory) {


@Override
@Nullable
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
return determineTargetScheduler(task).schedule(task, trigger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public String getExpression() {
* 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ private static Temporal rollbackToMidnight(Temporal current, Temporal result) {


@Override
@Nullable
public <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal) {
T result = adjust(temporal);
if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;

/**
Expand All @@ -38,6 +39,7 @@ class ScriptingDefaultsParser implements BeanDefinitionParser {


@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
BeanDefinition bd =
LangNamespaceUtils.registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, Str
}

@Override
@Nullable
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
// We only apply special treatment to ScriptFactory implementations here.
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
Expand Down

0 comments on commit 290a41d

Please sign in to comment.