1

I am working on develop a custom Validation Annotation , and the annotation need to be repeatable.

"Min.List" can meet the needs , and i did the same thing on my own annotation .

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = ComboValidator.class)
public @interface Combo {
    String dependField();
    String controlledField();
    Class<? extends Releation> relation() default BaseReleation.class;

    String message() default "{combo validation}";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default {};


    /**
     * Defines several {@link Combo} annotations on the same element.
     *
     * @see Combo
     */
    @Target({ ElementType.TYPE })
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @interface List {
        Combo[] value();
    }
}

It works.

In my mind, the validator will create different instance for differnt type which need to be validated. So inside the validator,i can get the data from the annotation and store it on type level field. But under the repeatable annotaion circumstances, i notice there is more than one instance created.

So my question is how does the @interface List means and works ?

xiesiyang
  • 21
  • 2
  • I did some research about the usage ,but i found nothing. If you can post the information about the usage, it is also helpful . Thx – xiesiyang Sep 28 '18 at 09:34

1 Answers1

0

The behaviour you are observing is defined in the Bean Validation Spec:

[...] Bean Validation provider treats regular annotations [...] whose value element has a return type of an array of constraint annotations in a special way. Each element in the value array are processed by the Bean Validation implementation as regular constraint annotations.

This was the way to support "Repeating Annotations" before Java 8.

DaniEll
  • 1,022
  • 4
  • 22
  • 31