Nullability fine-tuning around declaration inconsistencies

Issue: SPR-15720
Issue: SPR-15792
This commit is contained in:
Juergen Hoeller
2017-07-19 22:22:14 +02:00
parent 68e6b148cb
commit 46eba3dbfa
186 changed files with 986 additions and 619 deletions

View File

@@ -72,9 +72,8 @@ public interface AsyncHandlerInterceptor extends HandlerInterceptor {
* execution, for type and/or instance examination
* @throws Exception in case of errors
*/
default void afterConcurrentHandlingStarted(
HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
default void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
}
}

View File

@@ -120,9 +120,8 @@ public interface HandlerInterceptor {
* (can also be {@code null})
* @throws Exception in case of errors
*/
default void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView)
throws Exception {
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
}
/**
@@ -145,9 +144,8 @@ public interface HandlerInterceptor {
* @param ex exception thrown on handler execution, if any
* @throws Exception in case of errors
*/
default void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex)
throws Exception {
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
}
}

View File

@@ -681,10 +681,13 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
static class CompositeUriComponentsContributorFactoryBean
implements FactoryBean<CompositeUriComponentsContributor>, InitializingBean {
@Nullable
private RequestMappingHandlerAdapter handlerAdapter;
@Nullable
private ConversionService conversionService;
@Nullable
private CompositeUriComponentsContributor uriComponentsContributor;
public void setHandlerAdapter(RequestMappingHandlerAdapter handlerAdapter) {
@@ -697,6 +700,7 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
@Override
public void afterPropertiesSet() {
Assert.state(this.handlerAdapter != null, "No RequestMappingHandlerAdapter set");
this.uriComponentsContributor = new CompositeUriComponentsContributor(
this.handlerAdapter.getArgumentResolvers(), this.conversionService);
}

View File

@@ -37,8 +37,10 @@ import org.springframework.web.context.request.async.WebAsyncTask;
*/
public class AsyncSupportConfigurer {
@Nullable
private AsyncTaskExecutor taskExecutor;
@Nullable
private Long timeout;
private final List<CallableProcessingInterceptor> callableInterceptors = new ArrayList<>();

View File

@@ -39,14 +39,19 @@ import org.springframework.web.util.pattern.ParsingPathMatcher;
*/
public class PathMatchConfigurer {
@Nullable
private Boolean suffixPatternMatch;
@Nullable
private Boolean trailingSlashMatch;
@Nullable
private Boolean registeredSuffixPatternMatch;
@Nullable
private UrlPathHelper urlPathHelper;
@Nullable
private PathMatcher pathMatcher;
@@ -131,7 +136,8 @@ public class PathMatchConfigurer {
@Nullable
public PathMatcher getPathMatcher() {
if (this.pathMatcher instanceof ParsingPathMatcher && (this.trailingSlashMatch || this.suffixPatternMatch)) {
if (this.pathMatcher instanceof ParsingPathMatcher &&
(Boolean.TRUE.equals(this.trailingSlashMatch) || Boolean.TRUE.equals(this.suffixPatternMatch))) {
throw new IllegalStateException("When using a ParsingPathMatcher, useTrailingSlashMatch" +
" and useSuffixPatternMatch should be set to 'false'.");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -46,27 +46,24 @@ public abstract class HandlerInterceptorAdapter implements AsyncHandlerIntercept
* This implementation is empty.
*/
@Override
public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView)
throws Exception {
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
}
/**
* This implementation is empty.
*/
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
}
/**
* This implementation is empty.
*/
@Override
public void afterConcurrentHandlingStarted(
HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
}
}

View File

@@ -136,26 +136,6 @@ public final class MappedInterceptor implements HandlerInterceptor {
return this.interceptor;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return this.interceptor.preHandle(request, response, handler);
}
@Override
public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView)
throws Exception {
this.interceptor.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex)
throws Exception {
this.interceptor.afterCompletion(request, response, handler, ex);
}
/**
* Returns {@code true} if the interceptor applies to the given request path.
@@ -183,4 +163,26 @@ public final class MappedInterceptor implements HandlerInterceptor {
return false;
}
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
return this.interceptor.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
this.interceptor.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
this.interceptor.afterCompletion(request, response, handler, ex);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 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.
@@ -59,16 +59,16 @@ public class WebRequestHandlerInterceptorAdapter implements AsyncHandlerIntercep
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView)
throws Exception {
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
this.requestInterceptor.postHandle(new DispatcherServletWebRequest(request, response),
(modelAndView != null && !modelAndView.wasCleared() ? modelAndView.getModelMap() : null));
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
this.requestInterceptor.afterCompletion(new DispatcherServletWebRequest(request, response), ex);
}

View File

@@ -66,7 +66,7 @@ public abstract class AbstractLocaleContextResolver extends AbstractLocaleResolv
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, @Nullable Locale locale) {
public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
setLocaleContext(request, response, (locale != null ? new SimpleLocaleContext(locale) : null));
}

View File

@@ -127,7 +127,7 @@ public class AcceptHeaderLocaleResolver implements LocaleResolver {
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, @Nullable Locale locale) {
public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
throw new UnsupportedOperationException(
"Cannot change HTTP accept header - use a different locale resolution strategy");
}

View File

@@ -52,6 +52,7 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
private String paramName = DEFAULT_PARAM_NAME;
@Nullable
private String[] httpMethods;
private boolean ignoreInvalidLocale = false;
@@ -80,7 +81,7 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
* @param httpMethods the methods
* @since 4.2
*/
public void setHttpMethods(String... httpMethods) {
public void setHttpMethods(@Nullable String... httpMethods) {
this.httpMethods = httpMethods;
}
@@ -88,6 +89,7 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
* Return the configured HTTP methods.
* @since 4.2
*/
@Nullable
public String[] getHttpMethods() {
return this.httpMethods;
}
@@ -112,8 +114,6 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
* Specify whether to parse request parameter values as BCP 47 language tags
* instead of Java's legacy locale specification format.
* The default is {@code false}.
* <p>Note: This mode requires JDK 7 or higher. Set this flag to {@code true}
* for BCP 47 compliance on JDK 7+ only.
* @since 4.3
* @see Locale#forLanguageTag(String)
* @see Locale#toLanguageTag()

View File

@@ -20,7 +20,6 @@ import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -254,18 +253,16 @@ public class WebContentInterceptor extends WebContentGenerator implements Handle
* This implementation is empty.
*/
@Override
public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView)
throws Exception {
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
}
/**
* This implementation is empty.
*/
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
}
}

View File

@@ -60,6 +60,7 @@ abstract class AbstractNameValueExpression<T> implements NameValueExpression<T>
}
@Override
@Nullable
public T getValue() {
return this.value;
}

View File

@@ -79,7 +79,7 @@ public class DeferredResultMethodReturnValueHandler implements HandlerMethodRetu
DeferredResult<Object> result = new DeferredResult<>();
future.addCallback(new ListenableFutureCallback<Object>() {
@Override
public void onSuccess(Object value) {
public void onSuccess(@Nullable Object value) {
result.setResult(value);
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@@ -23,6 +23,8 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.context.ServletContextAware;
@@ -66,8 +68,10 @@ public class DefaultServletHttpRequestHandler implements HttpRequestHandler, Ser
private static final String WEBSPHERE_DEFAULT_SERVLET_NAME = "SimpleFileServlet";
@Nullable
private String defaultServletName;
@Nullable
private ServletContext servletContext;
@@ -114,6 +118,7 @@ public class DefaultServletHttpRequestHandler implements HttpRequestHandler, Ser
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Assert.state(this.servletContext != null, "No ServletContext set");
RequestDispatcher rd = this.servletContext.getNamedDispatcher(this.defaultServletName);
if (rd == null) {
throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" +

View File

@@ -53,6 +53,7 @@ public class TransformedResource extends ByteArrayResource {
@Override
@Nullable
public String getFilename() {
return this.filename;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 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.
@@ -21,6 +21,8 @@ import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.util.JavaScriptUtils;
/**
@@ -45,6 +47,7 @@ public class EscapeBodyTag extends HtmlEscapingAwareTag implements BodyTag {
private boolean javaScriptEscape = false;
@Nullable
private BodyContent bodyContent;
@@ -94,6 +97,7 @@ public class EscapeBodyTag extends HtmlEscapingAwareTag implements BodyTag {
* @throws IOException if reading failed
*/
protected String readBodyContent() throws IOException {
Assert.state(this.bodyContent != null, "No BodyContent set");
return this.bodyContent.getString();
}
@@ -104,6 +108,7 @@ public class EscapeBodyTag extends HtmlEscapingAwareTag implements BodyTag {
* @throws IOException if writing failed
*/
protected void writeBodyContent(String content) throws IOException {
Assert.state(this.bodyContent != null, "No BodyContent set");
this.bodyContent.getEnclosingWriter().print(content);
}

View File

@@ -103,6 +103,7 @@ public abstract class AbstractView extends WebApplicationObjectSupport implement
* Return the content type for this view.
*/
@Override
@Nullable
public String getContentType() {
return this.contentType;
}

View File

@@ -21,6 +21,8 @@ import java.nio.charset.Charset;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import org.springframework.lang.Nullable;
/**
* An implementation of Spring MVC's {@link ScriptTemplateConfig} for creating
* a {@code ScriptEngine} for use in a web application.
@@ -48,22 +50,31 @@ import javax.script.ScriptEngine;
*/
public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
@Nullable
private ScriptEngine engine;
@Nullable
private String engineName;
@Nullable
private Boolean sharedEngine;
@Nullable
private String[] scripts;
@Nullable
private String renderObject;
@Nullable
private String renderFunction;
@Nullable
private String contentType;
@Nullable
private Charset charset;
@Nullable
private String resourceLoaderPath;
@@ -90,11 +101,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* one (since it implies multiple lazy instantiations of the script engine).
* @see #setEngineName(String)
*/
public void setEngine(ScriptEngine engine) {
public void setEngine(@Nullable ScriptEngine engine) {
this.engine = engine;
}
@Override
@Nullable
public ScriptEngine getEngine() {
return this.engine;
}
@@ -105,11 +117,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* You must define {@code engine} or {@code engineName}, not both.
* @see #setEngine(ScriptEngine)
*/
public void setEngineName(String engineName) {
public void setEngineName(@Nullable String engineName) {
this.engineName = engineName;
}
@Override
@Nullable
public String getEngineName() {
return this.engineName;
}
@@ -127,11 +140,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* (one per thread).
* @see <a href="http://docs.oracle.com/javase/8/docs/api/javax/script/ScriptEngineFactory.html#getParameter-java.lang.String-">THREADING ScriptEngine parameter<a/>
*/
public void setSharedEngine(Boolean sharedEngine) {
public void setSharedEngine(@Nullable Boolean sharedEngine) {
this.sharedEngine = sharedEngine;
}
@Override
@Nullable
public Boolean isSharedEngine() {
return this.sharedEngine;
}
@@ -147,11 +161,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* @see #setResourceLoaderPath
* @see <a href="http://www.webjars.org">WebJars</a>
*/
public void setScripts(String... scriptNames) {
public void setScripts(@Nullable String... scriptNames) {
this.scripts = scriptNames;
}
@Override
@Nullable
public String[] getScripts() {
return this.scripts;
}
@@ -161,11 +176,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* For example, in order to call {@code Mustache.render()}, {@code renderObject}
* should be set to {@code "Mustache"} and {@code renderFunction} to {@code "render"}.
*/
public void setRenderObject(String renderObject) {
public void setRenderObject(@Nullable String renderObject) {
this.renderObject = renderObject;
}
@Override
@Nullable
public String getRenderObject() {
return this.renderObject;
}
@@ -181,11 +197,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* </ol>
* @see RenderingContext
*/
public void setRenderFunction(String renderFunction) {
public void setRenderFunction(@Nullable String renderFunction) {
this.renderFunction = renderFunction;
}
@Override
@Nullable
public String getRenderFunction() {
return this.renderFunction;
}
@@ -195,7 +212,7 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* ({@code text/html} by default).
* @since 4.2.1
*/
public void setContentType(String contentType) {
public void setContentType(@Nullable String contentType) {
this.contentType = contentType;
}
@@ -204,6 +221,7 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* @since 4.2.1
*/
@Override
@Nullable
public String getContentType() {
return this.contentType;
}
@@ -212,11 +230,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* Set the charset used to read script and template files.
* ({@code UTF-8} by default).
*/
public void setCharset(Charset charset) {
public void setCharset(@Nullable Charset charset) {
this.charset = charset;
}
@Override
@Nullable
public Charset getCharset() {
return this.charset;
}
@@ -229,11 +248,12 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
* Relative paths are allowed when running in an ApplicationContext.
* <p>Default is "classpath:".
*/
public void setResourceLoaderPath(String resourceLoaderPath) {
public void setResourceLoaderPath(@Nullable String resourceLoaderPath) {
this.resourceLoaderPath = resourceLoaderPath;
}
@Override
@Nullable
public String getResourceLoaderPath() {
return this.resourceLoaderPath;
}