Package br.com.caelum.stella.hibernate.validator

Source Code of br.com.caelum.stella.hibernate.validator.AnnotationMessageProducer

package br.com.caelum.stella.hibernate.validator;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import br.com.caelum.stella.MessageProducer;
import br.com.caelum.stella.SimpleValidationMessage;
import br.com.caelum.stella.ValidationMessage;
import br.com.caelum.stella.validation.InvalidValue;

/**
* Recupera mensagens de validação definida na anotação do Hibernate Validator.
*
* @author Fabio Kung
*/
public class AnnotationMessageProducer implements MessageProducer {

    private final Annotation constraint;

    public AnnotationMessageProducer(Annotation constraint) {
        this.constraint = constraint;
    }

    /**
     * This method will always return the same ValidationMessage, as Hibernate
     * Validator only let one message per Validator, defined inside the
     * constraint annotation.
     *
     * @param invalidValue
     *            will be ignored
     * @return the message defined by the related constraint annotation
     */
    public ValidationMessage getMessage(InvalidValue invalidValue) {
        try {
            Method constraintMessage = constraint.annotationType().getMethod("message");
            String message = constraintMessage.invoke(constraint).toString();
            return new SimpleValidationMessage(message);
        } catch (NoSuchMethodException e) {
            // same behavior as Hibernate Validator built-in validators
            // see
            // org.hibernate.validator.interpolator.DefaultMessageInterpolator
            throw new IllegalArgumentException("Annotation " + constraint
                    + " does not have an (accessible) message attribute");
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
TOP

Related Classes of br.com.caelum.stella.hibernate.validator.AnnotationMessageProducer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.