重复注释Java 7

时间:2018-08-01 12:09:12

标签: java annotations

我有一个类,该类的某些属性根据所调用的方法具有不同的行为,如下所示:

public classe Pessoa{
    // ...

    @Validador(method="WS01", maxlength="11")
    @Validador(method="WS02", maxlength="14")
    // others validators ...
    public String getDocumento(){return documento;}
}

我需要为此类的每个属性创建六个验证。

但是它给了我重复的注释错误。

我看到使用Java 8可以实现它,但是我需要使用Java 7。

我可以创建参数列表并像@NamedQueries和@NamedQuery Hibernate注释一样调用它们吗?

我的想法是做

@Validadores({@Validador(method="WS01", maxlength="11"), @Validador(method="WS02", maxlength="14"), ..., })
public String getDocumento(){return documento;}

我明白了!我的注释界面:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Validador {
String ws();
    boolean obrigatorio() default false;
    int tamanho() default 0;
    String formato() default "";
    boolean apenasNumeros() default false;  

}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Validadores {
Validador[] value() default {};

}

0 个答案:

没有答案