for (ConstraintDefinitionType constraintDefinition : constraintDefinitionList) {
String annotationClassName = constraintDefinition.getAnnotation();
Class<?> clazz = loadClass(annotationClassName, defaultPackage);
if (!clazz.isAnnotation()) {
throw new ValidationException(annotationClassName + " is not an annotation");
}
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) clazz;
ValidatedByType validatedByType = constraintDefinition.getValidatedBy();
List<Class<? extends ConstraintValidator<?,?>>> classes = new ArrayList();
/*
If include-existing-validator is set to false,
ConstraintValidator defined on the constraint annotation are ignored.
*/
if (validatedByType.isIncludeExistingValidators() != null &&
validatedByType.isIncludeExistingValidators()) {
/*
If set to true, the list of ConstraintValidators described in XML
are concatenated to the list of ConstraintValidator described on the
annotation to form a new array of ConstraintValidator evaluated.
*/
classes.addAll(findConstraintValidatorClasses(annotationClass));
}
for (JAXBElement<String> validatorClassName : validatedByType.getValue()) {
Class<? extends ConstraintValidator<?, ?>> validatorClass;
validatorClass = (Class<? extends ConstraintValidator<?, ?>>) SecureActions
.loadClass(validatorClassName.getValue(), this.getClass());
if (!ConstraintValidator.class.isAssignableFrom(validatorClass)) {
throw new ValidationException(
validatorClass + " is not a constraint validator class");
}
/*
Annotation based ConstraintValidator come before XML based
ConstraintValidator in the array. The new list is returned
by ConstraintDescriptor.getConstraintValidatorClasses().
*/
if (!classes.contains(validatorClass)) classes.add(validatorClass);
}
if (factory.getConstraintsCache().containsConstraintValidator(annotationClass)) {
throw new ValidationException("Constraint validator for " +
annotationClass.getName() + " already configured.");
} else {
factory.getConstraintsCache().putConstraintValidator(annotationClass,
classes.toArray(new Class[classes.size()]));
}