Implement Java annotations for validation -
i want call validator org.hibernate.validator.internal.constraintvalidators.
manually on value because can't annotate class. i've done :
lengthvalidator validator = new lengthvalidator(); validator.initialize(new length() { @override public class<? extends annotation> annotationtype() { return null; } @override public class<? extends payload>[] payload() { return null; } @override public int min() { return min == null ? defaultmin : min; } @override public string message() { return null; } @override public int max() { return max == null ? defaultmax : max; } @override public class<?>[] groups() { return null; } }); boolean valid = validator.isvalid(myvalue.astext(), null));
but sonar not happy :
lambdas , anonymous classes should not have many lines
so tried refactor code implementing @length in custom class :
public class stringlength implements length { // methods overriden @length }
but compiler complains
the annotation type length should not used superinterface stringlength
how can achieve want ?
note use of internal classes not encouraged, if wanted to, use hibernate validator annotationfactory
:
annotationdescriptor<length> descriptor = new annotationdescriptor<length>( length.class ); descriptor.setvalue( "min", 0 ); descriptor.setvalue( "max", 10 ); ... length lengthannotation = annotationfactory.create( descriptor );
i not quite sure though, gain using lengthvalidator
. still cannot make use of validator framework, right? if cannot annotate class, can not use xml configuration instead?
Comments
Post a Comment