Example usage for org.springframework.validation Validator getClass

List of usage examples for org.springframework.validation Validator getClass

Introduction

In this page you can find the example usage for org.springframework.validation Validator getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.springmodules.validation.valang.javascript.taglib.ValangJavaScriptTagUtils.java

/**
 * Inserts the valang validator from the provided controller into the model using the controller's name as the
 * validation rule's key./*ww  w  .ja v  a  2 s. co m*/
 * 
 * @param controller
 *            the controller that will provide the command name and validator
 * @param model
 *            the model into which the validation rules will be placed
 * @throws IllegalArgumentException
 *             if the controller does not specify a command name
 * @throws IllegalArgumentException
 *             if the controller's validator is not an instance of
 *             {@link org.springmodules.validation.valang.ValangValidator}
 */
public static void addValangRulesToModel(BaseCommandController controller, Map model) {
    Assert.hasText(controller.getCommandName(), "controller must define a command name");
    Validator validator = controller.getValidator();
    Assert.isInstanceOf(ValangValidator.class, validator,
            "controller's validator of class '"
                    + (validator != null ? validator.getClass().getName() : "[null]")
                    + "' must be an instance of 'ValangValidator'");
    ValangValidator vv = (ValangValidator) validator;
    addValangRulesToModel(controller.getCommandName(), vv.getRules(), model);
}

From source file:org.agatom.springatom.cmp.wizards.validation.ValidationServiceImpl.java

@Override
public void validate(final Validator localValidator, final Errors errors, final WizardResult result) {

    final long startTime = System.nanoTime();
    localValidator.validate(localValidator, errors);
    final long endTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);

    result.addDebugData(WizardDebugDataKeys.VALIDATOR, ClassUtils.getShortName(localValidator.getClass()));
    result.addDebugData(WizardDebugDataKeys.VALIDATION_TIME, endTime);

}

From source file:org.agatom.springatom.cmp.wizards.core.AbstractWizardProcessor.java

@SuppressWarnings("UnusedAssignment")
private void doValidate(final WizardResult result, final DataBinder binder, String step,
        final Map<String, Object> formData, final Locale locale) throws Exception {

    final Object target = binder.getTarget();

    if (!StringUtils.hasText(step)) {
        // Wizard submission, because step is null
        step = this.stepHelperDelegate.getLastStep();
        if (!this.stepHelperDelegate.isValidationEnabled(step)) {
            // reset the step again
            step = null;// w  ww.  j  a va 2  s. c  o m
        }
    }

    try {
        final BindingResult bindingResult = binder.getBindingResult();
        boolean alreadyValidate = false;

        if (this.localValidator != null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(String.format("Validating via Validator instance=%s", this.localValidator));
            }
            this.validationService.validate(this.localValidator, bindingResult, result);
            alreadyValidate = true;
        }

        if (!alreadyValidate) {
            final ValidationBean bean = new ValidationBean();

            bean.setPartialResult(result);
            bean.setStepId(step);
            bean.setCommandBean(bindingResult.getTarget());
            bean.setCommandBeanName(this.getContextObjectName());
            bean.setFormData(formData);
            bean.setBindingModel(formData);

            if (this.validationService.canValidate(bean)) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(
                            String.format("Validating via validation service for validationBean=%s", bean));
                }
                this.validationService.validate(bean);
                alreadyValidate = true;
            }
        }

        /* Will validate only if not yet validated it is not step submission and wizard is allowed to validate
         * This a last opportunity to validate however unlike validation via
         * - localValidator
         * - validationService
         * this validation will be run only if
         * - not yet validated
         * - current (or last) step has validation flag set
         * - entire wizard has validation flag set
         */
        if (!alreadyValidate && this.isValidationEnabledForStep(step) && this.isValidationEnabled()) {
            LOGGER.debug(String.format(
                    "Not yet validated (tried localValidator and via validationService), assuming that is wizard submission due to step===null, validating through binder"));
            final Validator validator = binder.getValidator();
            if (validator != null) {

                final long startTime = System.nanoTime();
                validator.validate(target, bindingResult);
                final long endTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);

                result.addDebugData(WizardDebugDataKeys.VALIDATION_TIME, endTime);
                result.addDebugData(WizardDebugDataKeys.VALIDATOR,
                        ClassUtils.getShortName(validator.getClass()));

            }
        }

        if (LOGGER.isDebugEnabled()) {
            final Set<Message> messages = result.getValidationMessages();
            final short count = (short) (messages == null ? 0 : messages.size());
            LOGGER.debug(String.format("Validation completed, found %d validation errors", count));
        }
    } catch (Exception exp) {
        // Catch any validation exception and add it as an error
        LOGGER.error("Validation failed either via [localValidator,validationService,binder#validator", exp);
        result.addError(exp);
        result.addFeedbackMessage(FeedbackMessage.newError()
                .setTitle(this.messageSource.getMessage("sa.wiz.validationError.title", locale))
                .setMessage(this.messageSource.getMessage("sa.wiz.validationError.msg",
                        new Object[] { target.toString() }, locale)));
    }

}

From source file:org.springframework.validation.ValidationUtils.java

/**
 * Invoke the given {@link Validator}/{@link SmartValidator} for the supplied object and
 * {@link Errors} instance./*from   w w w  . ja va 2  s  .c  o m*/
 * @param validator the {@code Validator} to be invoked (must not be {@code null})
 * @param obj the object to bind the parameters to
 * @param errors the {@link Errors} instance that should store the errors (must not be {@code null})
 * @param validationHints one or more hint objects to be passed to the validation engine
 * @throws IllegalArgumentException if either of the {@code Validator} or {@code Errors} arguments is
 * {@code null}, or if the supplied {@code Validator} does not {@link Validator#supports(Class) support}
 * the validation of the supplied object's type
 */
public static void invokeValidator(Validator validator, @Nullable Object obj, Errors errors,
        @Nullable Object... validationHints) {

    Assert.notNull(validator, "Validator must not be null");
    Assert.notNull(errors, "Errors object must not be null");

    if (logger.isDebugEnabled()) {
        logger.debug("Invoking validator [" + validator + "]");
    }
    if (obj != null && !validator.supports(obj.getClass())) {
        throw new IllegalArgumentException(
                "Validator [" + validator.getClass() + "] does not support [" + obj.getClass() + "]");
    }

    if (!ObjectUtils.isEmpty(validationHints) && validator instanceof SmartValidator) {
        ((SmartValidator) validator).validate(obj, errors, validationHints);
    } else {
        validator.validate(obj, errors);
    }

    if (logger.isDebugEnabled()) {
        if (errors.hasErrors()) {
            logger.debug("Validator found " + errors.getErrorCount() + " errors");
        } else {
            logger.debug("Validator found no errors");
        }
    }
}