public abstract class AnnotationSupport extends Object
The support methods allow to extract annotation properties used for building validation components.
The cache used to store Validation
instances having
corresponding annotations. If Validation
implementation class
has default constructor or constructor that accepts type of the value to be
validated then instances of this class can be cached. In other cases
Validation
instance can't be cached because its constructor
takes arguments that can differ in each instance for the same annotation
type and value type. The cache stores such validation components as
key-value pairs where value is validation component instance and key is
combination of annotation type and type of the value to be validated.
AnnotationSupport
is thread-safe.
Validation
Modifier and Type | Field and Description |
---|---|
protected static Set<String> |
DEFAULT_GROUP_SET
Default set of constraint groups.
|
protected static Set<ValidationTarget> |
DEFAULT_TARGET_SET
Default set of validation targets.
|
Constructor and Description |
---|
AnnotationSupport() |
Modifier and Type | Method and Description |
---|---|
protected static void |
addToCache(Class<? extends Annotation> annotationType,
Validation<?> validation)
Adds validation component to the cache.
|
protected static void |
addToCache(Class<? extends Annotation> annotationType,
Validation<?> validation,
Class<?> valueType)
Adds validation component to the cache.
|
protected static <T extends Validation<?>> |
createValidation(Class<T> validationType,
Annotation annotation,
Class<?> valueType)
Creates a new instance of the specified validation component type.
|
protected static String[] |
getAnnotationGroups(Annotation annotation,
String... defaults)
Returns
groups property value from the specified annotation
or the specified default groups if annotation has no such property and
array of default groups is not empty; returns null
otherwise. |
protected static Annotation[] |
getAnnotationList(Annotation annotation)
Returns array of annotations defined by the specified inner
List annotation or array with single element which
contains the specified annotation if it not conforms to the rules of
annotation list definition. |
protected static String |
getAnnotationMessage(Annotation annotation,
String namespace)
Returns
message property value from the specified
annotation or default annotation message if annotation has no such
property. |
protected static <T> T |
getAnnotationProperty(Annotation annotation,
String property,
Class<T> type)
Returns annotation property value for the specified property name and
type or
null if the specified annotation has no property
with such name and type. |
protected static Set<ValidationTarget> |
getAnnotationTargets(Annotation annotation)
Returns
targets property value from the specified annotation
or default target set if annotation has no such property. |
protected static Validation<?> |
getFromCache(Class<? extends Annotation> annotationType,
Class<?> valueType)
Returns validation component from cache for the specified annotation and
value types.
|
protected static boolean |
isAnnotationPresent(Annotation annotation,
Class<? extends Annotation> requiredType)
Determines recursively if the specified annotation has an annotation of
required type.
|
protected static final Set<ValidationTarget> DEFAULT_TARGET_SET
protected static void addToCache(Class<? extends Annotation> annotationType, Validation<?> validation)
annotationType
- Annotation type.validation
- Validation component to be cached.protected static void addToCache(Class<? extends Annotation> annotationType, Validation<?> validation, Class<?> valueType)
annotationType
- Annotation type.validation
- Validation component to be cached.valueType
- The type of a value to be validated.protected static Validation<?> getFromCache(Class<? extends Annotation> annotationType, Class<?> valueType)
annotationType
- Annotation type.valueType
- The type of a value to be validated.null
if cache has no such entry.protected static <T extends Validation<?>> T createValidation(Class<T> validationType, Annotation annotation, Class<?> valueType)
The validation component implementation class can define constructor with any access modifier. The following are valid validation component constructors in descending order of priority:
Validation(Annotation, Class)
Validation(Class, Annotation)
Validation(Annotation)
Validation(Class)
Validation()
DefaultValueConstraint(Class<V> type, DefaultValue annotation) { ... }
The returned validation component can be obtained from the cache if it was cached previously. Also this method automatically adds validation components to the cache if possible.
T
- The validation type.validationType
- Type of the validation component.annotation
- Annotation of the validation component.valueType
- Type of the value to be validated by the specified
validation component type.ValidationTypeException
- if the specified value type is not
supported by the specified validation component type.ValidationInstantiationException
- if validation component
instantiation fails.ValidationSignatureException
- if validation component doesn't
define valid constructor.protected static Annotation[] getAnnotationList(Annotation annotation)
List
annotation or array with single element which
contains the specified annotation if it not conforms to the rules of
annotation list definition.
Inner List
annotation should define value
property of an outer annotation array type.
annotation
- Annotation of the validation component.List
annotation or array with single element which
contains the specified annotation if it not conforms to the
rules of annotation list definition.protected static String getAnnotationMessage(Annotation annotation, String namespace)
message
property value from the specified
annotation or default annotation message if annotation has no such
property.
Default annotation message should be defined as follows:
namespace + "." + annotation.annotationType().getSimpleName()For example, default message for the
NotNull
annotation and
value
namespace should be value.NotNull
.annotation
- Annotation of the validation component.namespace
- Namespace of error message template key.message
property value from the specified
annotation or default annotation message if annotation has no
such property.protected static Set<ValidationTarget> getAnnotationTargets(Annotation annotation)
targets
property value from the specified annotation
or default target set if annotation has no such property.annotation
- Annotation of the validation component.targets
property value from the specified annotation
or default target set if annotation has no such property.protected static String[] getAnnotationGroups(Annotation annotation, String... defaults)
groups
property value from the specified annotation
or the specified default groups if annotation has no such property and
array of default groups is not empty; returns null
otherwise.annotation
- Annotation of the validation component.defaults
- Array of default groups.groups
property value from the specified annotation
or the specified default groups if annotation has no such
property and array of default groups is not empty; returns
null
otherwise.protected static <T> T getAnnotationProperty(Annotation annotation, String property, Class<T> type)
null
if the specified annotation has no property
with such name and type.T
- The expected annotation property type.annotation
- Annotation.property
- Property name.type
- Expected property type.null
if the specified annotation has no
property with such name and type.protected static boolean isAnnotationPresent(Annotation annotation, Class<? extends Annotation> requiredType)
annotation
- Annotation to be tested.requiredType
- Required annotation type.true
if the specified annotation has an annotation
of required type; false
otherwise.Copyright © 2011–2020 FoxLabs. All rights reserved.