Skip to content

Commit

Permalink
Refine null-safety in spring-webmvc
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Mar 22, 2024
1 parent 52b8c71 commit a16e8c3
Show file tree
Hide file tree
Showing 31 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {


@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext context) {
Object source = context.extractSource(element);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ViewControllerBeanDefinitionParser implements BeanDefinitionParser {


@Override
@Nullable
@SuppressWarnings("unchecked")
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;
import org.springframework.util.xml.DomUtils;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class ViewResolversBeanDefinitionParser implements BeanDefinitionParser {


@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext context) {
Object source = context.extractSource(element);
context.pushContainingComponent(new CompositeComponentDefinition(element.getTagName(), source));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> excep
}

@Override
@Nullable
public Validator getValidator() {
Validator selected = null;
for (WebMvcConfigurer configurer : this.delegates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public MultiValueMap<String, Cookie> cookies() {
}

@Override
@Nullable
public ModelAndView writeTo(HttpServletRequest request, HttpServletResponse response,
Context context) throws ServletException, IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public T entity() {
}

@Override
@Nullable
protected ModelAndView writeToInternal(HttpServletRequest servletRequest,
HttpServletResponse servletResponse, Context context)
throws ServletException, IOException {
Expand Down Expand Up @@ -363,6 +364,7 @@ public CompletionStageEntityResponse(HttpStatusCode statusCode, HttpHeaders head
}

@Override
@Nullable
protected ModelAndView writeToInternal(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
Context context) throws ServletException, IOException {

Expand Down Expand Up @@ -416,6 +418,7 @@ public PublisherEntityResponse(HttpStatusCode statusCode, HttpHeaders headers,
}

@Override
@Nullable
protected ModelAndView writeToInternal(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
Context context) throws ServletException, IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -229,6 +230,7 @@ public WriteFunctionResponse(HttpStatusCode statusCode, HttpHeaders headers, Mul
}

@Override
@Nullable
protected ModelAndView writeToInternal(HttpServletRequest request, HttpServletResponse response,
Context context) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ protected boolean hasCorsConfigurationSource(Object handler) {
}

@Override
@Nullable
protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request) {
CorsConfiguration corsConfig = super.getCorsConfiguration(handler, request);
if (handler instanceof HandlerMethod handlerMethod) {
Expand Down Expand Up @@ -600,6 +601,7 @@ public List<T> getMappingsByDirectPath(String urlPath) {
/**
* Return handler methods by mapping name. Thread-safe for concurrent use.
*/
@Nullable
public List<HandlerMethod> getHandlerMethodsByMappingName(String mappingName) {
return this.nameLookup.get(mappingName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ public void setAttribute(String name, Object value) {
}

@Override
@Nullable
public Object getAttribute(String name) {
return this.attributes.get(name);
}
Expand Down Expand Up @@ -532,6 +533,7 @@ private static class LookupPathMatchableHandlerMapping implements MatchableHandl
}

@Override
@Nullable
public PathPatternParser getPatternParser() {
return this.delegate.getPatternParser();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public Locale getLocale() {
return getDefaultLocale();
}
@Override
@Nullable
public TimeZone getTimeZone() {
return getDefaultTimeZone();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public boolean isStatusOnly() {
* @see #getViewName()
*/
@Override
@Nullable
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public void setBeanName(String name) {


@Override
@Nullable
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public void afterPropertiesSet() throws Exception {
* @see jakarta.servlet.Servlet#service(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)
*/
@Override
@Nullable
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes


@Override
public void setMessageSource(MessageSource messageSource) {
public void setMessageSource(@Nullable MessageSource messageSource) {
this.messageSource = messageSource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ protected Set<String> getDirectPaths(RequestMappingInfo info) {
* @return an info in case of a match; or {@code null} otherwise.
*/
@Override
@Nullable
protected RequestMappingInfo getMatchingMapping(RequestMappingInfo info, HttpServletRequest request) {
return info.getMatchingCondition(request);
}
Expand Down Expand Up @@ -243,6 +244,7 @@ private Map<String, MultiValueMap<String, String>> extractMatrixVariables(
* but not by consumable/producible media types
*/
@Override
@Nullable
protected HandlerMethod handleNoMatch(
Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public boolean supportsParameter(MethodParameter parameter) {
}

@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private static class ExtendedServletRequestValueResolver extends ServletRequestV
}

@Override
@Nullable
protected Object getRequestParameter(String name, Class<?> type) {
Object value = super.getRequestParameter(name, type);
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ protected boolean supportsInternal(HandlerMethod handlerMethod) {
}

@Override
@Nullable
protected ModelAndView handleInternal(HttpServletRequest request,
HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ private void updateConsumesCondition(RequestMappingInfo info, Method method) {
}

@Override
@Nullable
public RequestMatchResult match(HttpServletRequest request, String pattern) {
Assert.state(getPatternParser() == null, "This HandlerMapping uses PathPatterns.");
RequestMappingInfo info = RequestMappingInfo.paths(pattern).options(this.config).build();
Expand All @@ -535,6 +536,7 @@ public RequestMatchResult match(HttpServletRequest request, String pattern) {
}

@Override
@Nullable
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
Class<?> beanType = handlerMethod.getBeanType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public boolean supportsReturnType(MethodParameter returnType) {
* converter to read the content with.
*/
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

Expand All @@ -152,6 +153,7 @@ public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewC
}

@Override
@Nullable
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,
Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public MethodParameter getReturnValueType(@Nullable Object returnValue) {
* Bridge to controller method-level annotations.
*/
@Override
@Nullable
public <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) {
return ServletInvocableHandlerMethod.this.getMethodAnnotation(annotationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public boolean supportsParameter(MethodParameter parameter) {
}

@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected Locale getFallbackLocale() {
* request, session or application scope; if not found, returns {@code null}.
*/
@Override
@Nullable
protected TimeZone getFallbackTimeZone() {
if (jstlPresent) {
TimeZone timeZone = JstlPageLocaleResolver.getJstlTimeZone(getPageContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ protected MessageSource getMessageSource() {
/**
* Return default exception message.
*/
@Nullable
protected String getNoSuchMessageExceptionDescription(NoSuchMessageException ex) {
return ex.getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public String getElement() {
* since we're dealing with multiple HTML elements.
*/
@Override
@Nullable
protected String resolveId() throws JspException {
Object id = evaluate("id", getId());
if (id != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ private void renderOption(Object value, String label, TagWriter tagWriter) throw
}

@Override
@Nullable
protected String autogenerateId() throws JspException {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ protected int writeTagContent(TagWriter tagWriter) throws JspException {
* since we're dealing with multiple HTML elements.
*/
@Override
@Nullable
protected String resolveId() throws JspException {
Object id = evaluate("id", getId());
if (id != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public void afterPropertiesSet() throws BeansException {


@Override
@Nullable
protected View loadView(String viewName, Locale locale) throws Exception {
BeanFactory factory = initFactory(locale);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ protected Object getCacheKey(String viewName, Locale locale) {
* @see #requiredViewClass
*/
@Override
@Nullable
protected View createView(String viewName, Locale locale) throws Exception {
// If this resolver is not supposed to handle the given view,
// return null to pass on to the next resolver in the chain.
Expand Down Expand Up @@ -545,6 +546,7 @@ protected AbstractUrlBasedView instantiateView() {
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/
@Override
@Nullable
protected View loadView(String viewName, Locale locale) throws Exception {
AbstractUrlBasedView view = buildView(viewName);
View result = applyLifecycleMethods(viewName, view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ protected Object getCacheKey(String viewName, Locale locale) {
}

@Override
@Nullable
protected View loadView(String viewName, Locale locale) throws BeansException {
BeanFactory factory = initFactory();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ protected Class<?>[] getSourceTypes() {
* @return the adapted XSLT Source
* @throws IllegalArgumentException if the given Object is not of a supported type
*/
protected Source convertSource(Object sourceObject) throws Exception {
protected Source convertSource(@Nullable Object sourceObject) throws Exception {
if (sourceObject instanceof Source source) {
return source;
}
Expand Down

0 comments on commit a16e8c3

Please sign in to comment.