Annotation Interface Polymorphic


@Target(TYPE) @Retention(RUNTIME) public @interface Polymorphic
Indicates that the annotated type is polymorphic. Serializers for polymorphic types are not selected based on the compile-time types of configuration elements, but instead are chosen at runtime based on the actual types of their values. This enables adding instances of subclasses / implementations of a polymorphic type to collections. The subtypes must be valid configurations.

For correct deserialization, if an instance of polymorphic type (or one of its implementations / subclasses) is serialized, an additional property that holds type information is added to its serialization. The type information can be customized using the PolymorphicTypes annotation.

 
 // Example 1
 @Polymorphic
 @Configuration
 static abstract class A { ... }

 static final class Impl1 extends A { ... }
 static final class Impl2 extends A { ... }

 List<A> as = List.of(new Impl1(...), new Impl2(...), ...);

 // Example 2
 @Polymorphic
 interface B { ... }

 record Impl1() implements B { ... }

 @Configuration
 static final class Impl2 implements B { ... }

 List<B> bs = List.of(new Impl1(...), new Impl2(...), ...);
 
 
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Returns the name of the property that holds the type information.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default name of the property that holds the type information.
  • Field Details

    • DEFAULT_PROPERTY

      static final String DEFAULT_PROPERTY
      The default name of the property that holds the type information.
      See Also:
  • Element Details

    • property

      String property
      Returns the name of the property that holds the type information.

      The property returned by this method must neither be blank nor be the name of a configuration element.

      Returns:
      name of the property that holds the type information
      See Also:
      Default:
      "type"