java.lang.Object
dev.metaschema.databind.model.annotations.ModelUtil
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
FieldsModifier and TypeFieldDescriptionstatic final StringA sentinel value indicating that the default string value should be used.static final StringA sentinel value indicating that no string value was provided in an annotation.static final StringA placeholder for anullvalue for use in annotations, which cannot be null by default. -
Method Summary
Modifier and TypeMethodDescriptionstatic <A extends Annotation>
AgetAnnotation(Class<?> clazz, Class<A> annotationClass) Get the requested annotation from the provided Java class.static <A extends Annotation>
AgetAnnotation(Field javaField, Class<A> annotationClass) Get the requested annotation from the provided Java field.static IDataTypeAdapter<?>getDataTypeAdapter(Class<? extends IDataTypeAdapter<?>> adapterClass, IBindingContext bindingContext) Get the data type adapter instance of the provided adapter class.static IGroupAsresolveDefaultGroupAs(GroupAs groupAs, IModule module) Resolves aGroupAsannotation determining if an actual value is provided or if the value is the default, which indicates that no actual GroupAs was provided.static IntegerresolveDefaultInteger(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.static ObjectresolveDefaultValue(String defaultValue, IDataTypeAdapter<?> adapter) Given a provided default value string, get the data type specific default value using the provided data type adapter.static StringresolveNoneOrDefault(String value, String defaultValue) Resolves a string value.static StringresolveNoneOrValue(String value) Get the processed value of a string.static MarkupLineresolveToMarkupLine(String value) Get the markup value of a markdown string.static MarkupMultilineresolveToMarkupMultiline(String value) Get the markup value of a markdown string.static StringtoLocation(IBoundObject obj) Get a location string for the given bound object based on its metaschema data.static StringtoLocation(IBoundObject obj, URI uri) Get a location string for the given bound object, optionally including a URI.static Map.Entry<IAttributable.Key,Set<String>> toPropertyEntry(Property property) Convert aPropertyannotation to a map entry suitable for use in anIAttributableproperties map.
-
Field Details
-
NO_STRING_VALUE
A sentinel value indicating that no string value was provided in an annotation.- See Also:
-
DEFAULT_STRING_VALUE
A sentinel value indicating that the default string value should be used.- See Also:
-
NULL_VALUE
A placeholder for anullvalue for use in annotations, which cannot be null by default.Use of
" "simple substitute fornullto 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 fromannotationClass- 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 fromannotationClass- 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 isnullor "##default", then the provided default value will be used instead. If the value is "##none", then the value will benull. Otherwise, the value is returned.- Parameters:
value- the requested valuedefaultValue- the default value- Returns:
- the resolved value or
null
-
resolveNoneOrValue
Get the processed value of a string. If the value is "##none", then the value will benull. Otherwise the value is returned.- Parameters:
value- text or"##none"if no text is provided- Returns:
- the resolved value or
null
-
resolveToMarkupLine
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
nullif no markup content was provided
-
resolveToMarkupMultiline
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
nullif 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
NullJavaTypeAdapterclass, then the default data type adapter will be returned.- Parameters:
adapterClass- the data type adapter class to get the data type adapter instance forbindingContext- 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 anullvalue.- Parameters:
defaultValue- the string representation of the default valueadapter- 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
nullif the provided default value wasNULL_VALUE
-
resolveDefaultInteger
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
nullif the provided value was -2^31
-
resolveDefaultGroupAs
@NonNull public static IGroupAs resolveDefaultGroupAs(@NonNull GroupAs groupAs, @NonNull IModule module) Resolves aGroupAsannotation 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 resolvemodule- the containing module instance- Returns:
- a new
IGroupAsinstance or a singleton group as if the provided value was the default value
-
toLocation
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
Get a location string for the given bound object, optionally including a URI.- Parameters:
obj- the bound object to get the location foruri- the URI of the document containing the object, ornullif 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
Convert aPropertyannotation to a map entry suitable for use in anIAttributableproperties map.- Parameters:
property- the property annotation to convert- Returns:
- a map entry containing the property key and its set of values
-