I would like to create a custom constraint @LengthIfNotBlank(min=,max=)
based on Hibernates @Length
constraint:
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = LengthIfNotBlankValidator.class)
@Documented
public @interface LengthIfNotBlank {
String message() default "{com.mycompany.LengthIfNotBlank}";
Class<? extends Payload>[] payload() default {};
int max() default Integer.MAX_VALUE;
Class<?>[] groups() default {};
int min() default 0;
}
public class LengthIfNotBlankValidator extends LengthValidator {
@Override
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
return StringUtils.isBlank(value) || super.isValid(value, constraintValidatorContext);
}
}
Exception:
[java] javax.validation.ValidationException: Unable to initialize com.example.constraints.LengthIfNotBlankValidator
[java] Caused by: java.lang.ClassCastException: $Proxy32 cannot be cast to org.hibernate.validator.constraints.Length
UPDATE
Still getting this exception :-(
public class LengthIfNotBlankValidator extends LengthValidator {
public void initialize(LengthIfNotBlank parameters) {
Map<String,Object> elements = new HashMap<String,Object>();
elements.put("message", parameters.message());
elements.put("payload", parameters.payload());
elements.put("groups", parameters.groups());
elements.put("min", parameters.min());
elements.put("max", parameters.max());
AnnotationDescriptor<Length> descriptor = AnnotationDescriptor.getInstance(Length.class, elements);
Length length = AnnotationFactory.create(descriptor);
super.initialize(length);
}
@Override
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
return StringUtils.isBlank(value) || super.isValid(value, constraintValidatorContext);
}
}
Since annotation types cannot be inherited, you should override initialize()
and pass an actual instance of type Length
to super.initialize()
.
Note that you cannot create an instance of annotation type directly, therefore you have to use something like AnnotationFactory
.
Also I'm not sure that compiler would allow you to do it due to type safety constraints. If it doesn't, use composition instead of inheritance (i.e. create a field of type LengthValidator
).