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:
Philippe Marschall 2020-06-24 12:48:32 +02:00 committed by Sam Brannen
parent b33d2fe683
commit a142d21700
4 changed files with 4 additions and 7 deletions

View File

@ -595,8 +595,8 @@ public class Jackson2ObjectMapperBuilder {
* @see #modulesToInstall(Module...)
* @see com.fasterxml.jackson.databind.Module
*/
@SuppressWarnings("unchecked")
public Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... modules) {
@SafeVarargs
public final Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... modules) {
this.moduleClasses = modules;
this.findWellKnownModules = true;
return this;

View File

@ -409,8 +409,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
* @since 4.0.1
* @see com.fasterxml.jackson.databind.Module
*/
@SuppressWarnings("unchecked")
public void setModulesToInstall(Class<? extends Module>... modules) {
@SafeVarargs
public final void setModulesToInstall(Class<? extends Module>... modules) {
this.builder.modulesToInstall(modules);
}

View File

@ -242,7 +242,6 @@ class Jackson2ObjectMapperBuilderTests {
}
@Test
@SuppressWarnings("unchecked")
void modulesToInstallByClass() {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
.modulesToInstall(CustomIntegerModule.class)
@ -292,7 +291,6 @@ class Jackson2ObjectMapperBuilderTests {
}
@Test // SPR-12634
@SuppressWarnings("unchecked")
void customizeWellKnownModulesWithModuleClass()
throws JsonProcessingException, UnsupportedEncodingException {

View File

@ -213,7 +213,6 @@ public class Jackson2ObjectMapperFactoryBeanTests {
}
@Test // SPR-12634
@SuppressWarnings("unchecked")
public void customizeDefaultModulesWithModuleClass() throws JsonProcessingException, UnsupportedEncodingException {
this.factory.setModulesToInstall(CustomIntegerModule.class);
this.factory.afterPropertiesSet();