Make getters and setters null-safety consistent

This commit ensure that null-safety is consistent between
getters and setters in order to be able to provide beans
with properties with a common type when type safety is
taken in account like with Kotlin.

It also add a few missing property level @Nullable
annotations.

Issue: SPR-15792
This commit is contained in:
Sebastien Deleuze
2017-07-19 08:55:05 +02:00
parent ff85726fa9
commit fb4ddb0746
201 changed files with 579 additions and 489 deletions

View File

@@ -90,7 +90,7 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
public void setApplicationContext(@Nullable ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

View File

@@ -43,6 +43,7 @@ import org.springframework.web.server.ServerWebExchange;
*/
public class PathResourceResolver extends AbstractResourceResolver {
@Nullable
private Resource[] allowedLocations;
@@ -61,7 +62,7 @@ public class PathResourceResolver extends AbstractResourceResolver {
* to match its list of locations.
* @param locations the list of allowed locations
*/
public void setAllowedLocations(Resource... locations) {
public void setAllowedLocations(@Nullable Resource... locations) {
this.allowedLocations = locations;
}

View File

@@ -35,6 +35,7 @@ import org.springframework.web.server.ServerWebExchange;
*/
public abstract class ResourceTransformerSupport implements ResourceTransformer {
@Nullable
private ResourceUrlProvider resourceUrlProvider;
@@ -45,7 +46,7 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer
* relative links.
* @param resourceUrlProvider the URL provider to use
*/
public void setResourceUrlProvider(ResourceUrlProvider resourceUrlProvider) {
public void setResourceUrlProvider(@Nullable ResourceUrlProvider resourceUrlProvider) {
this.resourceUrlProvider = resourceUrlProvider;
}

View File

@@ -95,8 +95,10 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
private final List<ResourceTransformer> resourceTransformers = new ArrayList<>(4);
@Nullable
private CacheControl cacheControl;
@Nullable
private ResourceHttpMessageWriter resourceHttpMessageWriter;
@@ -160,7 +162,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
* Set the {@link org.springframework.http.CacheControl} instance to build
* the Cache-Control HTTP response header.
*/
public void setCacheControl(CacheControl cacheControl) {
public void setCacheControl(@Nullable CacheControl cacheControl) {
this.cacheControl = cacheControl;
}
@@ -177,7 +179,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
* Configure the {@link ResourceHttpMessageWriter} to use.
* <p>By default a {@link ResourceHttpMessageWriter} will be configured.
*/
public void setResourceHttpMessageWriter(ResourceHttpMessageWriter httpMessageWriter) {
public void setResourceHttpMessageWriter(@Nullable ResourceHttpMessageWriter httpMessageWriter) {
this.resourceHttpMessageWriter = httpMessageWriter;
}

View File

@@ -76,7 +76,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, Application
* Configure HTTP message readers to de-serialize the request body with.
* <p>By default this is set to {@link ServerCodecConfigurer} with defaults.
*/
public void setMessageCodecConfigurer(ServerCodecConfigurer configurer) {
public void setMessageCodecConfigurer(@Nullable ServerCodecConfigurer configurer) {
this.messageCodecConfigurer = configurer;
}
@@ -92,7 +92,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, Application
* Provide a WebBindingInitializer with "global" initialization to apply
* to every DataBinder instance.
*/
public void setWebBindingInitializer(WebBindingInitializer webBindingInitializer) {
public void setWebBindingInitializer(@Nullable WebBindingInitializer webBindingInitializer) {
this.webBindingInitializer = webBindingInitializer;
}
@@ -107,8 +107,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, Application
/**
* Configure resolvers for controller method arguments.
*/
public void setArgumentResolverConfigurer(ArgumentResolverConfigurer configurer) {
Assert.notNull(configurer, "ArgumentResolverConfigurer is required");
public void setArgumentResolverConfigurer(@Nullable ArgumentResolverConfigurer configurer) {
this.argumentResolverConfigurer = configurer;
}
@@ -125,7 +124,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, Application
* <p>By default this is an instance of {@link ReactiveAdapterRegistry} with
* default settings.
*/
public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry) {
public void setReactiveAdapterRegistry(@Nullable ReactiveAdapterRegistry registry) {
this.reactiveAdapterRegistry = registry;
}

View File

@@ -30,6 +30,7 @@ import org.springframework.lang.Nullable;
*/
public abstract class AbstractUrlBasedView extends AbstractView implements InitializingBean {
@Nullable
private String url;
@@ -51,7 +52,7 @@ public abstract class AbstractUrlBasedView extends AbstractView implements Initi
* Set the URL of the resource that this view wraps.
* The URL must be appropriate for the concrete View implementation.
*/
public void setUrl(String url) {
public void setUrl(@Nullable String url) {
this.url = url;
}

View File

@@ -134,7 +134,7 @@ public abstract class AbstractView implements View, ApplicationContextAware {
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
public void setApplicationContext(@Nullable ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

View File

@@ -111,9 +111,10 @@ public class RedirectView extends AbstractUrlBasedView {
* {@link HttpStatus#TEMPORARY_REDIRECT} or
* {@link HttpStatus#PERMANENT_REDIRECT}.
*/
public void setStatusCode(HttpStatus statusCode) {
Assert.notNull(statusCode, "HttpStatus must not be null");
Assert.isTrue(statusCode.is3xxRedirection(), "Must be a redirection (3xx status code)");
public void setStatusCode(@Nullable HttpStatus statusCode) {
if (statusCode != null) {
Assert.isTrue(statusCode.is3xxRedirection(), "Must be a redirection (3xx status code)");
}
this.statusCode = statusCode;
}

View File

@@ -73,18 +73,22 @@ public class UrlBasedViewResolver extends ViewResolverSupport
public static final String REDIRECT_URL_PREFIX = "redirect:";
@Nullable
private Class<?> viewClass;
private String prefix = "";
private String suffix = "";
@Nullable
private String[] viewNames;
private Function<String, RedirectView> redirectViewProvider = RedirectView::new;
@Nullable
private String requestContextAttribute;
@Nullable
private ApplicationContext applicationContext;
@@ -93,8 +97,8 @@ public class UrlBasedViewResolver extends ViewResolverSupport
* @param viewClass a class that is assignable to the required view class
* which by default is AbstractUrlBasedView
*/
public void setViewClass(Class<?> viewClass) {
if (!requiredViewClass().isAssignableFrom(viewClass)) {
public void setViewClass(@Nullable Class<?> viewClass) {
if (viewClass != null && !requiredViewClass().isAssignableFrom(viewClass)) {
String name = viewClass.getName();
throw new IllegalArgumentException("Given view class [" + name + "] " +
"is not of type [" + requiredViewClass().getName() + "]");
@@ -153,7 +157,7 @@ public class UrlBasedViewResolver extends ViewResolverSupport
* 'my*', '*Report' and '*Repo*' will all match the view name 'myReport'.
* @see #canHandle
*/
public void setViewNames(String... viewNames) {
public void setViewNames(@Nullable String... viewNames) {
this.viewNames = viewNames;
}
@@ -179,7 +183,7 @@ public class UrlBasedViewResolver extends ViewResolverSupport
* @param requestContextAttribute name of the RequestContext attribute
* @see AbstractView#setRequestContextAttribute
*/
public void setRequestContextAttribute(String requestContextAttribute) {
public void setRequestContextAttribute(@Nullable String requestContextAttribute) {
this.requestContextAttribute = requestContextAttribute;
}
@@ -200,7 +204,7 @@ public class UrlBasedViewResolver extends ViewResolverSupport
* @see #applyLifecycleMethods
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
public void setApplicationContext(@Nullable ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

View File

@@ -67,8 +67,10 @@ import org.springframework.web.server.ServerWebExchange;
*/
public class FreeMarkerView extends AbstractUrlBasedView {
@Nullable
private Configuration configuration;
@Nullable
private String encoding;
@@ -78,7 +80,7 @@ public class FreeMarkerView extends AbstractUrlBasedView {
* {@link FreeMarkerConfig} is expected in the Spring application context
* which is used to obtain the FreeMarker configuration.
*/
public void setConfiguration(Configuration configuration) {
public void setConfiguration(@Nullable Configuration configuration) {
this.configuration = configuration;
}
@@ -109,7 +111,7 @@ public class FreeMarkerView extends AbstractUrlBasedView {
* encoding in the FreeMarker Configuration rather than per template if all
* your templates share a common encoding.
*/
public void setEncoding(String encoding) {
public void setEncoding(@Nullable String encoding) {
this.encoding = encoding;
}