java.lang.Object
dev.metaschema.databind.model.annotations.ModelUtil

public final class ModelUtil extends Object
Utility methods for processing Metaschema binding annotations.

This class provides helper methods for extracting and interpreting annotation values from Java classes and fields.

  • Field Details

    • NO_STRING_VALUE

      public static final String NO_STRING_VALUE
      A sentinel value indicating that no string value was provided in an annotation.
      See Also:
    • DEFAULT_STRING_VALUE

      public static final String DEFAULT_STRING_VALUE
      A sentinel value indicating that the default string value should be used.
      See Also:
    • NULL_VALUE

      public static final String NULL_VALUE
      A placeholder for a null value for use in annotations, which cannot be null by default.

      Use of "" simple substitute for null to allow implementations to recognize the "no default value" state.

      See Also:
  • Method Details

    • getAnnotation

      @NonNull public static <A extends Annotation> A getAnnotation(@NonNull Class<?> clazz, Class<A> annotationClass)
      Get the requested annotation from the provided Java class.
      Type Parameters:
      A - the annotation Java type
      Parameters:
      clazz - the Java class to get the annotation from
      annotationClass - the annotation class instance
      Returns:
      the annotation
      Throws:
      IllegalArgumentException - if the annotation was not present on the class
    • getAnnotation

      @NonNull public static <A extends Annotation> A getAnnotation(@NonNull Field javaField, Class<A> annotationClass)
      Get the requested annotation from the provided Java field.
      Type Parameters:
      A - the annotation Java type
      Parameters:
      javaField - the Java field to get the annotation from
      annotationClass - the annotation class instance
      Returns:
      the annotation
      Throws:
      IllegalArgumentException - if the annotation was not present on the field
    • resolveNoneOrDefault

      @Nullable public static String resolveNoneOrDefault(@Nullable String value, @Nullable String defaultValue)
      Resolves a string value. If the value is null or "##default", then the provided default value will be used instead. If the value is "##none", then the value will be null. Otherwise, the value is returned.
      Parameters:
      value - the requested value
      defaultValue - the default value
      Returns:
      the resolved value or null
    • resolveNoneOrValue

      @Nullable public static String resolveNoneOrValue(@NonNull String value)
      Get the processed value of a string. If the value is "##none", then the value will be null. Otherwise the value is returned.
      Parameters:
      value - text or "##none" if no text is provided
      Returns:
      the resolved value or null
    • resolveToMarkupLine

      @Nullable public static MarkupLine resolveToMarkupLine(@NonNull String value)
      Get the markup value of a markdown string.
      Parameters:
      value - markdown text or "##none" if no text is provided
      Returns:
      the markup line content or null if no markup content was provided
    • resolveToMarkupMultiline

      @Nullable public static MarkupMultiline resolveToMarkupMultiline(@NonNull String value)
      Get the markup value of a markdown string.
      Parameters:
      value - markdown text or "##none" if no text is provided
      Returns:
      the markup line content or null if no markup content was provided
    • getDataTypeAdapter

      @NonNull public static IDataTypeAdapter<?> getDataTypeAdapter(@NonNull Class<? extends IDataTypeAdapter<?>> adapterClass, @NonNull IBindingContext bindingContext)
      Get the data type adapter instance of the provided adapter class.

      If the provided adapter Java class is the NullJavaTypeAdapter class, then the default data type adapter will be returned.

      Parameters:
      adapterClass - the data type adapter class to get the data type adapter instance for
      bindingContext - the Metaschema binding context used to lookup the data type adapter
      Returns:
      the data type adapter
      Throws:
      IllegalArgumentException - if the provided adapter is not registered with the binding context
    • resolveDefaultValue

      @Nullable public static Object resolveDefaultValue(@NonNull String defaultValue, IDataTypeAdapter<?> adapter)
      Given a provided default value string, get the data type specific default value using the provided data type adapter.

      If the provided default value is NULL_VALUE, then this method will return a null value.

      Parameters:
      defaultValue - the string representation of the default value
      adapter - the data type adapter instance used to cast the default string value to a data type specific object
      Returns:
      the data type specific object or null if the provided default value was NULL_VALUE
    • resolveDefaultInteger

      public static Integer resolveDefaultInteger(int value)
      Resolves an integer value by determining if an actual value is provided or -2^31, which indicates that no actual value was provided.

      The integer value -2^31 cannot be used, since this indicates no value.

      Parameters:
      value - the integer value to resolve
      Returns:
      the integer value or null if the provided value was -2^31
    • resolveDefaultGroupAs

      @NonNull public static IGroupAs resolveDefaultGroupAs(@NonNull GroupAs groupAs, @NonNull IModule module)
      Resolves a GroupAs annotation determining if an actual value is provided or if the value is the default, which indicates that no actual GroupAs was provided.
      Parameters:
      groupAs - the GroupAs value to resolve
      module - the containing module instance
      Returns:
      a new IGroupAs instance or a singleton group as if the provided value was the default value
    • toLocation

      public static String toLocation(@NonNull IBoundObject obj)
      Get a location string for the given bound object based on its metaschema data.
      Parameters:
      obj - the bound object to get the location for
      Returns:
      a location string in the format "line:column", or an empty string if location information is not available
    • toLocation

      public static String toLocation(@NonNull IBoundObject obj, @Nullable URI uri)
      Get a location string for the given bound object, optionally including a URI.
      Parameters:
      obj - the bound object to get the location for
      uri - the URI of the document containing the object, or null if not available
      Returns:
      a location string in the format "uri@line:column", or just the URI or location portion if only one is available, or an empty string if neither is available
    • toPropertyEntry

      public static Map.Entry<IAttributable.Key,Set<String>> toPropertyEntry(@NonNull Property property)
      Convert a Property annotation to a map entry suitable for use in an IAttributable properties map.
      Parameters:
      property - the property annotation to convert
      Returns:
      a map entry containing the property key and its set of values