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 Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    A sentinel value indicating that the default string value should be used.
    static final String
    A sentinel value indicating that no string value was provided in an annotation.
    static final String
    A placeholder for a null value for use in annotations, which cannot be null by default.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <A extends Annotation>
    A
    getAnnotation(Class<?> clazz, Class<A> annotationClass)
    Get the requested annotation from the provided Java class.
    static <A extends Annotation>
    A
    getAnnotation(Field javaField, Class<A> annotationClass)
    Get the requested annotation from the provided Java field.
    static dev.metaschema.core.datatype.IDataTypeAdapter<?>
    getDataTypeAdapter(Class<? extends dev.metaschema.core.datatype.IDataTypeAdapter<?>> adapterClass, IBindingContext bindingContext)
    Get the data type adapter instance of the provided adapter class.
    static IGroupAs
    resolveDefaultGroupAs(GroupAs groupAs, dev.metaschema.core.model.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.
    static Integer
    Resolves an integer value by determining if an actual value is provided or -2^31, which indicates that no actual value was provided.
    static Object
    resolveDefaultValue(String defaultValue, dev.metaschema.core.datatype.IDataTypeAdapter<?> adapter)
    Given a provided default value string, get the data type specific default value using the provided data type adapter.
    static String
    resolveNoneOrDefault(String value, String defaultValue)
    Resolves a string value.
    static String
    Get the processed value of a string.
    static dev.metaschema.core.datatype.markup.MarkupLine
    Get the markup value of a markdown string.
    static dev.metaschema.core.datatype.markup.MarkupMultiline
    Get the markup value of a markdown string.
    static String
    toLocation(dev.metaschema.core.model.IBoundObject obj)
    Get a location string for the given bound object based on its metaschema data.
    static String
    toLocation(dev.metaschema.core.model.IBoundObject obj, URI uri)
    Get a location string for the given bound object, optionally including a URI.
    static Map.Entry<dev.metaschema.core.model.IAttributable.Key,Set<String>>
    Convert a Property annotation to a map entry suitable for use in an IAttributable properties map.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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 dev.metaschema.core.datatype.markup.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 dev.metaschema.core.datatype.markup.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 dev.metaschema.core.datatype.IDataTypeAdapter<?> getDataTypeAdapter(@NonNull Class<? extends dev.metaschema.core.datatype.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, dev.metaschema.core.datatype.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 dev.metaschema.core.model.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 dev.metaschema.core.model.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 dev.metaschema.core.model.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<dev.metaschema.core.model.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