I want to validate my arguments with Java Annotation. I don't know how to use write a own Annotation for Lists.
Here an simple example:
class test{
@myAnnotation
List<myObject> myElements =new List<>(); // validated List
}
class myObject{
String name;
}
my Annotation Interface:
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy=myAnnotation_Validator.class)
@Documented
public @interface myAnnotation {
String message() default "List is not valid";
Class <?>[] groups() default {};
Class <?extends Payload>[] payload() default{};
String[] namen();
}
public class myAnnotation_Validator implements ConstraintValidator<myAnnotation, Collection> {
@Override
public void initialize(Kredite_List_Check a) {
// What to do here???
}
@Override
public boolean isValid(Collection t, ConstraintValidatorContext cvc) {
// What to do here???
// How could i get the names from my List "myElements" ?
return false;
}
}
In this Example my List is valid, if each element from my List has another name. I don't know how I get the listobject in my Validator class and the names of myObject-elements.
UPDATE:
I try to describe my question in another way:
my list is not valid if two elements from type "myObject" in my list ("myElements") have the same name!
How could I realize this with Annotations?