Skip to content

Commit

Permalink
Nullability refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jun 8, 2021
1 parent 090e394 commit 23f396a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@

import java.util.Map;

import org.springframework.lang.Nullable;
import org.springframework.ui.ConcurrentModel;
import org.springframework.validation.BindingResult;

Expand All @@ -36,17 +37,19 @@
* @author Rossen Stoyanchev
* @since 5.0
* @see BindingResult
* @see BindingAwareModelMap
*/
@SuppressWarnings("serial")
public class BindingAwareConcurrentModel extends ConcurrentModel {

@Override
public Object put(String key, Object value) {
@Nullable
public Object put(String key, @Nullable Object value) {
removeBindingResultIfNecessary(key, value);
return super.put(key, value);
}

private void removeBindingResultIfNecessary(String key, Object value) {
private void removeBindingResultIfNecessary(String key, @Nullable Object value) {
if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
String resultKey = BindingResult.MODEL_KEY_PREFIX + key;
BindingResult result = (BindingResult) get(resultKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@

import java.util.Map;

import org.springframework.lang.Nullable;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.validation.BindingResult;

Expand All @@ -39,7 +40,7 @@
public class BindingAwareModelMap extends ExtendedModelMap {

@Override
public Object put(String key, Object value) {
public Object put(String key, @Nullable Object value) {
removeBindingResultIfNecessary(key, value);
return super.put(key, value);
}
Expand All @@ -50,7 +51,7 @@ public void putAll(Map<? extends String, ?> map) {
super.putAll(map);
}

private void removeBindingResultIfNecessary(Object key, Object value) {
private void removeBindingResultIfNecessary(Object key, @Nullable Object value) {
if (key instanceof String) {
String attributeName = (String) key;
if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini
private boolean detectHandlerFunctionsInAncestorContexts = false;



/**
* Create an empty {@code RouterFunctionMapping}.
* <p>If this constructor is used, this mapping will detect all
Expand All @@ -91,6 +90,7 @@ public RouterFunctionMapping(RouterFunction<?> routerFunction) {
this.routerFunction = routerFunction;
}


/**
* Set the router function to map to.
* <p>If this property is used, no application context detection will occur.
Expand All @@ -111,6 +111,10 @@ public RouterFunction<?> getRouterFunction() {
return this.routerFunction;
}

/**
* Set the message body converters to use.
* <p>These converters are used to convert from and to HTTP requests and responses.
*/
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
this.messageConverters = messageConverters;
}
Expand All @@ -127,6 +131,7 @@ public void setDetectHandlerFunctionsInAncestorContexts(boolean detectHandlerFun
this.detectHandlerFunctionsInAncestorContexts = detectHandlerFunctionsInAncestorContexts;
}


@Override
public void afterPropertiesSet() throws Exception {
if (this.routerFunction == null) {
Expand Down Expand Up @@ -204,8 +209,9 @@ private void initMessageConverters() {
this.messageConverters = messageConverters;
}

@Nullable

@Override
@Nullable
protected Object getHandlerInternal(HttpServletRequest servletRequest) throws Exception {
if (this.routerFunction != null) {
ServerRequest request = ServerRequest.create(servletRequest, this.messageConverters);
Expand All @@ -219,8 +225,10 @@ protected Object getHandlerInternal(HttpServletRequest servletRequest) throws Ex
}

private void setAttributes(HttpServletRequest servletRequest, ServerRequest request,
HandlerFunction<?> handlerFunction) {
PathPattern matchingPattern = (PathPattern) servletRequest.getAttribute(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
@Nullable HandlerFunction<?> handlerFunction) {

PathPattern matchingPattern =
(PathPattern) servletRequest.getAttribute(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
if (matchingPattern != null) {
servletRequest.removeAttribute(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
servletRequest.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, matchingPattern.getPatternString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ protected void handlerMethodsInitialized(Map<T, HandlerMethod> handlerMethods) {
* Look up a handler method for the given request.
*/
@Override
@Nullable
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
String lookupPath = initLookupPath(request);
this.mappingRegistry.acquireReadLock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.server.PathContainer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -117,6 +118,7 @@ protected Comparator<RequestMappingInfo> getMappingComparator(final HttpServletR
}

@Override
@Nullable
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
request.removeAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ public boolean isRunning() {
return this.running;
}

@Nullable

@Override
@Nullable
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
Object handler = super.getHandlerInternal(request);
return matchWebSocketUpgrade(handler, request) ? handler : null;
return (matchWebSocketUpgrade(handler, request) ? handler : null);
}

private boolean matchWebSocketUpgrade(@Nullable Object handler, HttpServletRequest request) {
Expand Down

0 comments on commit 23f396a

Please sign in to comment.