Skip to content

Commit 56db1a4

Browse files
committed
Add nullability annotations to module/spring-boot-servlet
See gh-46587
1 parent d78d803 commit 56db1a4

14 files changed

+59
-24
lines changed

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/MultipartConfigFactory.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.servlet;
1818

1919
import jakarta.servlet.MultipartConfigElement;
20+
import org.jspecify.annotations.Nullable;
2021

2122
import org.springframework.util.unit.DataSize;
2223

@@ -28,43 +29,43 @@
2829
*/
2930
public class MultipartConfigFactory {
3031

31-
private String location;
32+
private @Nullable String location;
3233

33-
private DataSize maxFileSize;
34+
private @Nullable DataSize maxFileSize;
3435

35-
private DataSize maxRequestSize;
36+
private @Nullable DataSize maxRequestSize;
3637

37-
private DataSize fileSizeThreshold;
38+
private @Nullable DataSize fileSizeThreshold;
3839

3940
/**
4041
* Sets the directory location where files will be stored.
4142
* @param location the location
4243
*/
43-
public void setLocation(String location) {
44+
public void setLocation(@Nullable String location) {
4445
this.location = location;
4546
}
4647

4748
/**
4849
* Sets the maximum {@link DataSize size} allowed for uploaded files.
4950
* @param maxFileSize the maximum file size
5051
*/
51-
public void setMaxFileSize(DataSize maxFileSize) {
52+
public void setMaxFileSize(@Nullable DataSize maxFileSize) {
5253
this.maxFileSize = maxFileSize;
5354
}
5455

5556
/**
5657
* Sets the maximum {@link DataSize} allowed for multipart/form-data requests.
5758
* @param maxRequestSize the maximum request size
5859
*/
59-
public void setMaxRequestSize(DataSize maxRequestSize) {
60+
public void setMaxRequestSize(@Nullable DataSize maxRequestSize) {
6061
this.maxRequestSize = maxRequestSize;
6162
}
6263

6364
/**
6465
* Sets the {@link DataSize size} threshold after which files will be written to disk.
6566
* @param fileSizeThreshold the file size threshold
6667
*/
67-
public void setFileSizeThreshold(DataSize fileSizeThreshold) {
68+
public void setFileSizeThreshold(@Nullable DataSize fileSizeThreshold) {
6869
this.fileSizeThreshold = fileSizeThreshold;
6970
}
7071

@@ -87,7 +88,7 @@ public MultipartConfigElement createMultipartConfig() {
8788
* @param defaultValue the default value if the size is {@code null} or negative
8889
* @return the amount of bytes to use
8990
*/
90-
private long convertToBytes(DataSize size, int defaultValue) {
91+
private long convertToBytes(@Nullable DataSize size, int defaultValue) {
9192
if (size != null && !size.isNegative()) {
9293
return size.toBytes();
9394
}

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/actuate/exchanges/HttpExchangesFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import jakarta.servlet.http.HttpServletRequest;
2828
import jakarta.servlet.http.HttpServletResponse;
2929
import jakarta.servlet.http.HttpSession;
30+
import org.jspecify.annotations.Nullable;
3031

3132
import org.springframework.boot.actuate.web.exchanges.HttpExchange;
3233
import org.springframework.boot.actuate.web.exchanges.HttpExchangeRepository;
@@ -106,7 +107,7 @@ private boolean isRequestValid(HttpServletRequest request) {
106107
}
107108
}
108109

109-
private String getSessionId(HttpServletRequest request) {
110+
private @Nullable String getSessionId(HttpServletRequest request) {
110111
HttpSession session = request.getSession(false);
111112
return (session != null) ? session.getId() : null;
112113
}

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/actuate/exchanges/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
*
2020
* @see org.springframework.boot.actuate.web.exchanges.HttpExchangeRepository
2121
*/
22+
@NullMarked
2223
package org.springframework.boot.servlet.actuate.exchanges;
24+
25+
import org.jspecify.annotations.NullMarked;

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/actuate/mappings/FiltersMappingDescriptionProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import jakarta.servlet.Filter;
2323
import jakarta.servlet.ServletContext;
24+
import org.jspecify.annotations.Nullable;
2425

2526
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
2627
import org.springframework.aot.hint.RuntimeHints;
@@ -29,6 +30,7 @@
2930
import org.springframework.boot.servlet.actuate.mappings.FiltersMappingDescriptionProvider.FiltersMappingDescriptionProviderRuntimeHints;
3031
import org.springframework.context.ApplicationContext;
3132
import org.springframework.context.annotation.ImportRuntimeHints;
33+
import org.springframework.util.Assert;
3234
import org.springframework.web.context.WebApplicationContext;
3335

3436
/**
@@ -44,8 +46,9 @@ public class FiltersMappingDescriptionProvider implements MappingDescriptionProv
4446
@Override
4547
public List<FilterRegistrationMappingDescription> describeMappings(ApplicationContext context) {
4648
if (context instanceof WebApplicationContext webApplicationContext) {
47-
return webApplicationContext.getServletContext()
48-
.getFilterRegistrations()
49+
ServletContext servletContext = webApplicationContext.getServletContext();
50+
Assert.state(servletContext != null, "'servletContext' must not be null");
51+
return servletContext.getFilterRegistrations()
4952
.values()
5053
.stream()
5154
.map(FilterRegistrationMappingDescription::new)
@@ -64,7 +67,7 @@ static class FiltersMappingDescriptionProviderRuntimeHints implements RuntimeHin
6467
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
6568

6669
@Override
67-
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
70+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
6871
this.bindingRegistrar.registerReflectionHints(hints.reflection(),
6972
FilterRegistrationMappingDescription.class);
7073
}

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/actuate/mappings/ServletsMappingDescriptionProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import jakarta.servlet.Servlet;
2323
import jakarta.servlet.ServletContext;
24+
import org.jspecify.annotations.Nullable;
2425

2526
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
2627
import org.springframework.aot.hint.RuntimeHints;
@@ -29,6 +30,7 @@
2930
import org.springframework.boot.servlet.actuate.mappings.ServletsMappingDescriptionProvider.ServletsMappingDescriptionProviderRuntimeHints;
3031
import org.springframework.context.ApplicationContext;
3132
import org.springframework.context.annotation.ImportRuntimeHints;
33+
import org.springframework.util.Assert;
3234
import org.springframework.web.context.WebApplicationContext;
3335

3436
/**
@@ -44,8 +46,9 @@ public class ServletsMappingDescriptionProvider implements MappingDescriptionPro
4446
@Override
4547
public List<ServletRegistrationMappingDescription> describeMappings(ApplicationContext context) {
4648
if (context instanceof WebApplicationContext webApplicationContext) {
47-
return webApplicationContext.getServletContext()
48-
.getServletRegistrations()
49+
ServletContext servletContext = webApplicationContext.getServletContext();
50+
Assert.state(servletContext != null, "'servletContext' must not be null");
51+
return servletContext.getServletRegistrations()
4952
.values()
5053
.stream()
5154
.map(ServletRegistrationMappingDescription::new)
@@ -64,7 +67,7 @@ static class ServletsMappingDescriptionProviderRuntimeHints implements RuntimeHi
6467
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
6568

6669
@Override
67-
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
70+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
6871
this.bindingRegistrar.registerReflectionHints(hints.reflection(),
6972
ServletRegistrationMappingDescription.class);
7073
}

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/actuate/mappings/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Actuator servlet request mappings support.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.servlet.actuate.mappings;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/autoconfigure/MultipartProperties.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.servlet.autoconfigure;
1818

1919
import jakarta.servlet.MultipartConfigElement;
20+
import org.jspecify.annotations.Nullable;
2021

2122
import org.springframework.boot.context.properties.ConfigurationProperties;
2223
import org.springframework.boot.context.properties.PropertyMapper;
@@ -57,7 +58,7 @@ public class MultipartProperties {
5758
/**
5859
* Intermediate location of uploaded files.
5960
*/
60-
private String location;
61+
private @Nullable String location;
6162

6263
/**
6364
* Max file size.
@@ -94,11 +95,11 @@ public void setEnabled(boolean enabled) {
9495
this.enabled = enabled;
9596
}
9697

97-
public String getLocation() {
98+
public @Nullable String getLocation() {
9899
return this.location;
99100
}
100101

101-
public void setLocation(String location) {
102+
public void setLocation(@Nullable String location) {
102103
this.location = location;
103104
}
104105

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/autoconfigure/ServletEncodingProperties.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.nio.charset.Charset;
2020
import java.nio.charset.StandardCharsets;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.boot.context.properties.ConfigurationProperties;
2325

2426
/**
@@ -45,18 +47,18 @@ public class ServletEncodingProperties {
4547
* Whether to force the encoding to the configured charset on HTTP requests and
4648
* responses.
4749
*/
48-
private Boolean force;
50+
private @Nullable Boolean force;
4951

5052
/**
5153
* Whether to force the encoding to the configured charset on HTTP requests. Defaults
5254
* to true when "force" has not been specified.
5355
*/
54-
private Boolean forceRequest;
56+
private @Nullable Boolean forceRequest;
5557

5658
/**
5759
* Whether to force the encoding to the configured charset on HTTP responses.
5860
*/
59-
private Boolean forceResponse;
61+
private @Nullable Boolean forceResponse;
6062

6163
public Charset getCharset() {
6264
return this.charset;

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/autoconfigure/actuate/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Actuator Servlet support.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.servlet.autoconfigure.actuate;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-servlet/src/main/java/org/springframework/boot/servlet/autoconfigure/actuate/web/ServletManagementChildContextConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.context.annotation.Configuration;
3434
import org.springframework.security.config.BeanIds;
3535
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
36+
import org.springframework.util.Assert;
3637

3738
/**
3839
* {@link ManagementContextConfiguration @ManagementContextConfiguration} for Servlet web
@@ -64,14 +65,16 @@ static class ServletManagementContextSecurityConfiguration {
6465
@Bean
6566
Filter springSecurityFilterChain(HierarchicalBeanFactory beanFactory) {
6667
BeanFactory parent = beanFactory.getParentBeanFactory();
68+
Assert.state(parent != null, "'parent' must not be null");
6769
return parent.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN, Filter.class);
6870
}
6971

7072
@Bean
7173
@ConditionalOnBean(name = "securityFilterChainRegistration", search = SearchStrategy.ANCESTORS)
7274
DelegatingFilterProxyRegistrationBean securityFilterChainRegistration(HierarchicalBeanFactory beanFactory) {
73-
return beanFactory.getParentBeanFactory()
74-
.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class);
75+
BeanFactory parent = beanFactory.getParentBeanFactory();
76+
Assert.state(parent != null, "'parent' must not be null");
77+
return parent.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class);
7578
}
7679

7780
}

0 commit comments

Comments
 (0)