diff --git a/README.md b/README.md index 500cfc8..67b2a59 100644 --- a/README.md +++ b/README.md @@ -82,10 +82,10 @@ ## Custom ## -Validates variable against a custom class +Validates against a custom class ```java -@Custom(className = "com.ljunggren.validator.evaluation.AlphaEvaluation") +@CustomValidator(className = "MyEvaluation") ``` Required diff --git a/src/main/java/com/ljunggren/validator/validation/CustomValidation.java b/src/main/java/com/ljunggren/validator/validation/CustomValidation.java index 4e7972f..bdd7c72 100644 --- a/src/main/java/com/ljunggren/validator/validation/CustomValidation.java +++ b/src/main/java/com/ljunggren/validator/validation/CustomValidation.java @@ -8,33 +8,30 @@ public class CustomValidation extends ValidationChain { - private Class annotationClass = CustomValidator.class; - @Override public void validate(Annotation annotation, Item item) { - if (annotation.annotationType() == annotationClass) { + if (annotation.annotationType() == CustomValidator.class) { String className = ((CustomValidator) annotation).className(); try { Evaluation evaluation = instantiateEvaluation(className); if (!evaluation.isValid(item.getValue())) { item.addErrorMessage(evaluation.getErrorMessage()); - return; } } catch (Exception e) { item.addErrorMessage(e.getMessage()); - return; } + return; } nextChain.validate(annotation, item); } @SuppressWarnings("unchecked") - public Evaluation instantiateEvaluation(String className) throws Exception { + private Evaluation instantiateEvaluation(String className) throws Exception { try { Class clazz = Class.forName(className); if (Evaluation.class.isAssignableFrom(clazz)) { return (Evaluation) clazz.newInstance(); - } + } throw new Exception("CustomValidator className does not implement Evaluation"); } catch (ClassNotFoundException e) { throw new Exception("CustomValidator class not found " + e.getMessage());