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
This commit is contained in:
committed by
Sam Brannen
parent
b33d2fe683
commit
a142d21700
@@ -595,8 +595,8 @@ public class Jackson2ObjectMapperBuilder {
|
|||||||
* @see #modulesToInstall(Module...)
|
* @see #modulesToInstall(Module...)
|
||||||
* @see com.fasterxml.jackson.databind.Module
|
* @see com.fasterxml.jackson.databind.Module
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SafeVarargs
|
||||||
public Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... modules) {
|
public final Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... modules) {
|
||||||
this.moduleClasses = modules;
|
this.moduleClasses = modules;
|
||||||
this.findWellKnownModules = true;
|
this.findWellKnownModules = true;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -409,8 +409,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
|
|||||||
* @since 4.0.1
|
* @since 4.0.1
|
||||||
* @see com.fasterxml.jackson.databind.Module
|
* @see com.fasterxml.jackson.databind.Module
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SafeVarargs
|
||||||
public void setModulesToInstall(Class<? extends Module>... modules) {
|
public final void setModulesToInstall(Class<? extends Module>... modules) {
|
||||||
this.builder.modulesToInstall(modules);
|
this.builder.modulesToInstall(modules);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -242,7 +242,6 @@ class Jackson2ObjectMapperBuilderTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
void modulesToInstallByClass() {
|
void modulesToInstallByClass() {
|
||||||
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
|
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
|
||||||
.modulesToInstall(CustomIntegerModule.class)
|
.modulesToInstall(CustomIntegerModule.class)
|
||||||
@@ -292,7 +291,6 @@ class Jackson2ObjectMapperBuilderTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // SPR-12634
|
@Test // SPR-12634
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
void customizeWellKnownModulesWithModuleClass()
|
void customizeWellKnownModulesWithModuleClass()
|
||||||
throws JsonProcessingException, UnsupportedEncodingException {
|
throws JsonProcessingException, UnsupportedEncodingException {
|
||||||
|
|
||||||
|
|||||||
@@ -213,7 +213,6 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // SPR-12634
|
@Test // SPR-12634
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public void customizeDefaultModulesWithModuleClass() throws JsonProcessingException, UnsupportedEncodingException {
|
public void customizeDefaultModulesWithModuleClass() throws JsonProcessingException, UnsupportedEncodingException {
|
||||||
this.factory.setModulesToInstall(CustomIntegerModule.class);
|
this.factory.setModulesToInstall(CustomIntegerModule.class);
|
||||||
this.factory.afterPropertiesSet();
|
this.factory.afterPropertiesSet();
|
||||||
|
|||||||
Reference in New Issue
Block a user