@MinMuleVersion("4.1") @Target({FIELD,PARAMETER}) @Retention(RUNTIME) @Documented public @interface NullSafe
Indicates that the annotated parameter should never be assigned a null value, even though such value is a valid assignment on the mule DSL.

This annotation is intended to be used alongside Optional (the concept itself doesn't make sense for required parameters). When the annotated parameter is resolved to a null value, the runtime will create a default instance of such parameter to prevent a null variable.

This behaviour is implemented slightly different depending on the parameter type:

  • Collection and Map: an empty collection/map is created
  • A generic Pojo: A default instance is created (default constructor required)
  • An non-instantiable type: A default instance is created using the type provided as parameter in defaultImplementingType.
  • Any Optional Parameter fields with a default value will be assigned to such default. E.g.:
          public class HelloWorld {
          @Parameter
          @Optional(defaultValue="Hello world!"
          private String greeting;
          }
     

    A null safe parameter of type HelloWorld would be created with the greeting field assigned to Hello world!

  • Basic types: String, numbers, dates, etc, are not supported. A IllegalParameterModelDefinitionException will be thrown by the runtime if used in parameters of such type
Since:
1.0
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Declares the default Type that will be instantiated if the NullSafe mechanism is triggered on a non-instantiable type
  • Element Details

    • defaultImplementingType

      Class<?> defaultImplementingType
      Declares the default Type that will be instantiated if the NullSafe mechanism is triggered on a non-instantiable type
      Returns:
      the default Type to be used when creating the default instance
      Default:
      java.lang.Object.class