Class JacksonSpiTypeIdResolver

java.lang.Object
com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
dev.orne.beans.JacksonSpiTypeIdResolver
All Implemented Interfaces:
com.fasterxml.jackson.databind.jsontype.TypeIdResolver

@API(status=EXPERIMENTAL, since="0.6") public class JacksonSpiTypeIdResolver extends com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
Implementation of TypeIdResolver that discovers interface/bean subtypes using SPI configuration files.

Uses JsonTypeName annotations to map ID to subtype.

Example:

 package a.b.c;
 @JsonTypeInfo(
         use=JsonTypeInfo.Id.NAME,
         defaultImpl=DefaultImpl.class)
 @JsonTypeIdResolver(JacksonSpiTypeIdResolver.class)
 interface IBase { ... }
 
 package a.b.c;
 @JsonTypeName("Default")
 class DefaultImpl implements IBase { ... }
 
 package a.b.c.extra;
 
 @JsonTypeName("Extra")
 class ExtraImpl implements IBase { ... }
 
In (potentially multiple) META-INF/services/a.b.c.IBase file:
 a.b.c.DefaultImpl
 a.b.c.extra.ExtraImpl
 
Since:
0.6
Version:
1.0, 2023-11
Author:
(w) Iker Hernaez
See Also:
  • TypeIdResolver
  • Constructor Details

    • JacksonSpiTypeIdResolver

      public JacksonSpiTypeIdResolver()
      Creates a new instance.
  • Method Details

    • getSubtypes

      @NotNull public @NotNull Map<String,Class<?>> getSubtypes()
      Returns the registered types.
      Returns:
      The registered types
    • init

      public void init(@NotNull @NotNull com.fasterxml.jackson.databind.JavaType bt)
      Specified by:
      init in interface com.fasterxml.jackson.databind.jsontype.TypeIdResolver
      Overrides:
      init in class com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
    • getMechanism

      public com.fasterxml.jackson.annotation.JsonTypeInfo.Id getMechanism()
    • idFromValue

      public String idFromValue(@NotNull @NotNull Object value)
    • idFromValueAndType

      public String idFromValueAndType(Object value, @NotNull @NotNull Class<?> suggestedType)
    • typeFromId

      public com.fasterxml.jackson.databind.JavaType typeFromId(com.fasterxml.jackson.databind.DatabindContext context, String id)
      Specified by:
      typeFromId in interface com.fasterxml.jackson.databind.jsontype.TypeIdResolver
      Overrides:
      typeFromId in class com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
    • getRegisteredSubTypes

      @NotNull protected @NotNull Map<String,Class<?>> getRegisteredSubTypes(@NotNull @NotNull Class<?> baseClass)
      Discovers the registered sub-types scanning the classpath for service providers of the base type.
      Parameters:
      baseClass - The base type.
      Returns:
      The map of IDs to subtypes
    • findInheritedSubTypes

      @NotNull protected @NotNull Map<String,Class<?>> findInheritedSubTypes(@NotNull @NotNull Class<?> type)
      Discovers the registered sub-types scanning the classpath for service providers of the base type. Searchs for
      Parameters:
      type - The base type.
      Returns:
      The map of IDs to subtypes
    • checkForRepeatedName

      protected void checkForRepeatedName(@NotNull @NotNull Map<String,Class<?>> known, @NotNull @NotNull String id, @NotNull @NotNull Class<?> type)
      Checks the known sub-types for repeated IDs for different sub-type.
      Parameters:
      known - The known sub-types.
      id - The type ID to check.
      type - The type to check.
      Throws:
      IllegalArgumentException - If the ID has been used for another sub-type.
    • getIdFromBean

      @NotNull protected @NotNull String getIdFromBean(@NotNull @NotNull Object bean)
      Retrieves the type ID from the bean instance.

      Default implementation calls to getIdFromAnnotation(Class) when bean class.

      Parameters:
      bean - The bean to retrieve the type ID from
      Returns:
      The type ID, or null if not resolved
    • getIdFromAnnotation

      protected String getIdFromAnnotation(Class<?> type)
      Extracts the type ID from the JsonTypeName annotation of the specified type.
      Parameters:
      type - The actual bean type
      Returns:
      The type ID, or null if not resolved
    • defaultTypeId

      public static String defaultTypeId(@NotNull @NotNull Class<?> cls)
      If no name was explicitly given for a class, we will just use non-qualified class name
      Parameters:
      cls - The type class
      Returns:
      The non-qualified class name
      See Also:
      • TypeNameIdResolver
    • defaultTypeId

      public static String defaultTypeId(@NotNull @NotNull String clsName)
      If no name was explicitly given for a class, we will just use non-qualified class name
      Parameters:
      clsName - The class name
      Returns:
      The non-qualified class name
      See Also:
      • TypeNameIdResolver