Skip to content

Commit 38acdf3

Browse files
committed
Add nullability annotations to module/spring-boot-mustache
See gh-46587
1 parent 1c13bba commit 38acdf3

File tree

8 files changed

+65
-26
lines changed

8 files changed

+65
-26
lines changed

module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheProperties.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424
import java.util.function.Supplier;
2525

26+
import org.jspecify.annotations.Nullable;
27+
2628
import org.springframework.boot.context.properties.ConfigurationProperties;
2729
import org.springframework.http.MediaType;
2830
import org.springframework.util.MimeType;
@@ -51,12 +53,12 @@ public class MustacheProperties {
5153
/**
5254
* View names that can be resolved.
5355
*/
54-
private String[] viewNames;
56+
private String @Nullable [] viewNames;
5557

5658
/**
5759
* Name of the RequestContext attribute for all views.
5860
*/
59-
private String requestContextAttribute;
61+
private @Nullable String requestContextAttribute;
6062

6163
/**
6264
* Whether to enable MVC view resolution for Mustache.
@@ -107,19 +109,19 @@ public void setSuffix(String suffix) {
107109
this.suffix = suffix;
108110
}
109111

110-
public String[] getViewNames() {
112+
public String @Nullable [] getViewNames() {
111113
return this.viewNames;
112114
}
113115

114-
public void setViewNames(String[] viewNames) {
116+
public void setViewNames(String @Nullable [] viewNames) {
115117
this.viewNames = viewNames;
116118
}
117119

118-
public String getRequestContextAttribute() {
120+
public @Nullable String getRequestContextAttribute() {
119121
return this.requestContextAttribute;
120122
}
121123

122-
public void setRequestContextAttribute(String requestContextAttribute) {
124+
public void setRequestContextAttribute(@Nullable String requestContextAttribute) {
123125
this.requestContextAttribute = requestContextAttribute;
124126
}
125127

@@ -128,7 +130,7 @@ public Charset getCharset() {
128130
}
129131

130132
public String getCharsetName() {
131-
return (this.charset != null) ? this.charset.name() : null;
133+
return this.charset.name();
132134
}
133135

134136
public void setCharset(Charset charset) {
@@ -275,13 +277,13 @@ public static class Reactive {
275277
/**
276278
* Media types supported by Mustache views.
277279
*/
278-
private List<MediaType> mediaTypes;
280+
private @Nullable List<MediaType> mediaTypes;
279281

280-
public List<MediaType> getMediaTypes() {
282+
public @Nullable List<MediaType> getMediaTypes() {
281283
return this.mediaTypes;
282284
}
283285

284-
public void setMediaTypes(List<MediaType> mediaTypes) {
286+
public void setMediaTypes(@Nullable List<MediaType> mediaTypes) {
285287
this.mediaTypes = mediaTypes;
286288
}
287289

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

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

module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheView.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@
2828

2929
import com.samskivert.mustache.Mustache.Compiler;
3030
import com.samskivert.mustache.Template;
31+
import org.jspecify.annotations.Nullable;
3132
import reactor.core.publisher.Flux;
3233
import reactor.core.publisher.Mono;
3334

35+
import org.springframework.context.ApplicationContext;
3436
import org.springframework.core.io.Resource;
3537
import org.springframework.core.io.buffer.DataBuffer;
3638
import org.springframework.core.io.buffer.DataBufferUtils;
3739
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3840
import org.springframework.http.MediaType;
41+
import org.springframework.util.Assert;
3942
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
4043
import org.springframework.web.reactive.result.view.View;
4144
import org.springframework.web.server.ServerWebExchange;
@@ -48,9 +51,9 @@
4851
*/
4952
public class MustacheView extends AbstractUrlBasedView {
5053

51-
private Compiler compiler;
54+
private @Nullable Compiler compiler;
5255

53-
private String charset;
56+
private @Nullable String charset;
5457

5558
/**
5659
* Set the JMustache compiler to be used by this view. Typically this property is not
@@ -66,7 +69,7 @@ public void setCompiler(Compiler compiler) {
6669
* Set the charset used for reading Mustache template files.
6770
* @param charset the charset to use for reading template files
6871
*/
69-
public void setCharset(String charset) {
72+
public void setCharset(@Nullable String charset) {
7073
this.charset = charset;
7174
}
7275

@@ -76,7 +79,8 @@ public boolean checkResourceExists(Locale locale) throws Exception {
7679
}
7780

7881
@Override
79-
protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType, ServerWebExchange exchange) {
82+
protected Mono<Void> renderInternal(Map<String, Object> model, @Nullable MediaType contentType,
83+
ServerWebExchange exchange) {
8084
Resource resource = resolveResource();
8185
if (resource == null) {
8286
return Mono
@@ -86,6 +90,7 @@ protected Mono<Void> renderInternal(Map<String, Object> model, MediaType content
8690
.bufferFactory()
8791
.allocateBuffer(DefaultDataBufferFactory.DEFAULT_INITIAL_CAPACITY);
8892
try (Reader reader = getReader(resource)) {
93+
Assert.state(this.compiler != null, "'compiler' must not be null");
8994
Template template = this.compiler.compile(reader);
9095
Charset charset = getCharset(contentType).orElseGet(this::getDefaultCharset);
9196
try (Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset)) {
@@ -100,8 +105,13 @@ protected Mono<Void> renderInternal(Map<String, Object> model, MediaType content
100105
return exchange.getResponse().writeWith(Flux.just(dataBuffer));
101106
}
102107

103-
private Resource resolveResource() {
104-
Resource resource = getApplicationContext().getResource(getUrl());
108+
private @Nullable Resource resolveResource() {
109+
ApplicationContext applicationContext = getApplicationContext();
110+
String url = getUrl();
111+
if (applicationContext == null || url == null) {
112+
return null;
113+
}
114+
Resource resource = applicationContext.getResource(url);
105115
if (resource == null || !resource.exists()) {
106116
return null;
107117
}
@@ -115,7 +125,7 @@ private Reader getReader(Resource resource) throws IOException {
115125
return new InputStreamReader(resource.getInputStream());
116126
}
117127

118-
private Optional<Charset> getCharset(MediaType mediaType) {
128+
private Optional<Charset> getCharset(@Nullable MediaType mediaType) {
119129
return Optional.ofNullable((mediaType != null) ? mediaType.getCharset() : null);
120130
}
121131

module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheViewResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.samskivert.mustache.Mustache;
2020
import com.samskivert.mustache.Mustache.Compiler;
21+
import org.jspecify.annotations.Nullable;
2122

2223
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
2324
import org.springframework.web.reactive.result.view.UrlBasedViewResolver;
@@ -34,7 +35,7 @@ public class MustacheViewResolver extends UrlBasedViewResolver {
3435

3536
private final Compiler compiler;
3637

37-
private String charset;
38+
private @Nullable String charset;
3839

3940
/**
4041
* Create a {@code MustacheViewResolver} backed by a default instance of a

module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
* Additional {@link org.springframework.web.reactive.result.view.View Views} for use with
1919
* WebFlux.
2020
*/
21+
@NullMarked
2122
package org.springframework.boot.mustache.reactive.view;
23+
24+
import org.jspecify.annotations.NullMarked;

module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheView.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
import com.samskivert.mustache.Template;
2727
import jakarta.servlet.http.HttpServletRequest;
2828
import jakarta.servlet.http.HttpServletResponse;
29+
import org.jspecify.annotations.Nullable;
2930

31+
import org.springframework.context.ApplicationContext;
3032
import org.springframework.core.io.Resource;
33+
import org.springframework.util.Assert;
3134
import org.springframework.web.servlet.View;
3235
import org.springframework.web.servlet.view.AbstractTemplateView;
3336

@@ -41,9 +44,9 @@
4144
*/
4245
public class MustacheView extends AbstractTemplateView {
4346

44-
private Compiler compiler;
47+
private @Nullable Compiler compiler;
4548

46-
private String charset;
49+
private @Nullable String charset;
4750

4851
/**
4952
* Set the Mustache compiler to be used by this view.
@@ -61,27 +64,40 @@ public void setCompiler(Compiler compiler) {
6164
* Set the charset used for reading Mustache template files.
6265
* @param charset the charset to use for reading template files
6366
*/
64-
public void setCharset(String charset) {
67+
public void setCharset(@Nullable String charset) {
6568
this.charset = charset;
6669
}
6770

6871
@Override
6972
public boolean checkResource(Locale locale) throws Exception {
70-
Resource resource = getApplicationContext().getResource(getUrl());
71-
return (resource != null && resource.exists());
73+
Resource resource = getResource();
74+
return resource != null;
7275
}
7376

7477
@Override
7578
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request,
7679
HttpServletResponse response) throws Exception {
77-
Template template = createTemplate(getApplicationContext().getResource(getUrl()));
80+
Resource resource = getResource();
81+
Assert.state(resource != null, "'resource' must not be null");
82+
Template template = createTemplate(resource);
7883
if (template != null) {
7984
template.execute(model, response.getWriter());
8085
}
8186
}
8287

88+
private @Nullable Resource getResource() {
89+
ApplicationContext applicationContext = getApplicationContext();
90+
String url = getUrl();
91+
if (applicationContext == null || url == null) {
92+
return null;
93+
}
94+
Resource resource = applicationContext.getResource(url);
95+
return (resource.exists()) ? resource : null;
96+
}
97+
8398
private Template createTemplate(Resource resource) throws IOException {
8499
try (Reader reader = getReader(resource)) {
100+
Assert.state(this.compiler != null, "'compiler' must not be null");
85101
return this.compiler.compile(reader);
86102
}
87103
}

module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheViewResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.samskivert.mustache.Mustache;
2020
import com.samskivert.mustache.Mustache.Compiler;
21+
import org.jspecify.annotations.Nullable;
2122

2223
import org.springframework.web.servlet.ViewResolver;
2324
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
@@ -33,7 +34,7 @@ public class MustacheViewResolver extends AbstractTemplateViewResolver {
3334

3435
private final Mustache.Compiler compiler;
3536

36-
private String charset;
37+
private @Nullable String charset;
3738

3839
/**
3940
* Create a {@code MustacheViewResolver} backed by a default instance of a
@@ -63,7 +64,7 @@ protected Class<?> requiredViewClass() {
6364
* Set the charset.
6465
* @param charset the charset
6566
*/
66-
public void setCharset(String charset) {
67+
public void setCharset(@Nullable String charset) {
6768
this.charset = charset;
6869
}
6970

module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Additional {@link org.springframework.web.servlet.View Views} for use with Web MVC.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.mustache.servlet.view;
22+
23+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)