Skip to content

Commit a142d21

Browse files
marschallsbrannen
authored andcommitted
Use @SafeVarargs in Jackson builder and factory
Using @SafeVarargs in Jackson mapper builder and factory bean classes allows the varargs methods to be used without a compiler warning. The implementations of these methods do not perform unsafe operations on their varargs parameter. It is therefore safe to add this annotation. The following two methods are changed: - add @SafeVarargs to Jackson2ObjectMapperBuilder#modulesToInstall and make it final - add @SafeVarargs to Jackson2ObjectMapperFactoryBean#setModulesToInstall and make it final This is a backwards incompatible change as these methods now have to be declared final. Existing subclasses that override one of these methods will break. Closes gh-25311
1 parent b33d2fe commit a142d21

File tree

4 files changed

+4
-7
lines changed

4 files changed

+4
-7
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ public Jackson2ObjectMapperBuilder modulesToInstall(Module... modules) {
595595
* @see #modulesToInstall(Module...)
596596
* @see com.fasterxml.jackson.databind.Module
597597
*/
598-
@SuppressWarnings("unchecked")
599-
public Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... modules) {
598+
@SafeVarargs
599+
public final Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... modules) {
600600
this.moduleClasses = modules;
601601
this.findWellKnownModules = true;
602602
return this;

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ public void setModules(List<Module> modules) {
409409
* @since 4.0.1
410410
* @see com.fasterxml.jackson.databind.Module
411411
*/
412-
@SuppressWarnings("unchecked")
413-
public void setModulesToInstall(Class<? extends Module>... modules) {
412+
@SafeVarargs
413+
public final void setModulesToInstall(Class<? extends Module>... modules) {
414414
this.builder.modulesToInstall(modules);
415415
}
416416

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ void modules() {
242242
}
243243

244244
@Test
245-
@SuppressWarnings("unchecked")
246245
void modulesToInstallByClass() {
247246
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
248247
.modulesToInstall(CustomIntegerModule.class)
@@ -292,7 +291,6 @@ void customizeWellKnownModulesWithModule()
292291
}
293292

294293
@Test // SPR-12634
295-
@SuppressWarnings("unchecked")
296294
void customizeWellKnownModulesWithModuleClass()
297295
throws JsonProcessingException, UnsupportedEncodingException {
298296

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ public void defaultModules() throws JsonProcessingException, UnsupportedEncoding
213213
}
214214

215215
@Test // SPR-12634
216-
@SuppressWarnings("unchecked")
217216
public void customizeDefaultModulesWithModuleClass() throws JsonProcessingException, UnsupportedEncodingException {
218217
this.factory.setModulesToInstall(CustomIntegerModule.class);
219218
this.factory.afterPropertiesSet();

0 commit comments

Comments
 (0)