/** * Since various objects may have subclass/interfaces that also require validation this mechanism allows for * targeted validation of different inherited subtypes on the same object. * * @param obj the object to validate * @param clazz the class type to validate it as. * validation. * @throws RaveException thrown if the validation process fails. */ void validateAs(Object obj, Class<?> clazz) throws RaveException { if (!clazz.isInstance(obj)) { throw new IllegalArgumentException("Trying to validate " + obj.getClass().getCanonicalName() + " as " + clazz.getCanonicalName()); } BaseValidator base; synchronized (this) { base = classValidatorMap.get(clazz); if (base == null) { base = getValidatorInstance(clazz); } if (base == null) { throw new UnsupportedObjectException(Collections.singletonList( new RaveError(obj.getClass(), "", RaveErrorStrings.CLASS_NOT_SUPPORTED_ERROR))); } } base.validateAs(obj, clazz); }
/** * Validate an object. * * @param object the object to be validated. * @throws RaveException if validation fails. */ public void validate(Object object) throws RaveException { Class<?> clazz = object.getClass(); Validated validated = clazz.getAnnotation(Validated.class); BaseValidator validator; synchronized (this) { if (validated == null && !unannotatedModelValidator.hasSeen(clazz)) { unannotatedModelValidator.processNonAnnotatedClasses(clazz); } validator = classValidatorMap.get(clazz); if (validator == null) { validator = getValidatorInstance(clazz); } if (validator == null) { throw new UnsupportedObjectException(Collections.singletonList( new RaveError(clazz, "", RaveErrorStrings.CLASS_NOT_SUPPORTED_ERROR))); } } validator.validate(object); }