Java中的注释(参数)验证?

Java中的注释(参数)验证?,java,validation,annotations,Java,Validation,Annotations,有什么方法可以验证Java中的注释有效性吗 例如,我可能有一个注释 public @interface Foo { int number(); String name(); } public @interface Foos { Foo[] value(); /* Foos() { for(int i=0; i<value().length; ++i) { if( i != value()[i].number() ) {

有什么方法可以验证Java中的注释有效性吗

例如,我可能有一个注释

public @interface Foo {
   int number();
   String name();
}

public @interface Foos {
    Foo[] value();
    /* Foos() {
        for(int i=0; i<value().length; ++i) {
            if( i != value()[i].number() ) {
                throw new IllegalArgumentException();
            }
        }
    } */
}

您的意思是要检查使用注释的所有位置,以便每个数字从0开始只使用一次?这是不可能以简单的方式实现的

如果您真的想这样做,您必须编写代码来扫描类路径上的所有类,找到使用注释的所有类,然后检查注释的值。(如果注释用于方法或其他方面而不是类,那么就更难了)


您不能将实现代码(如构造函数)添加到批注中。通过这种方式,注释类似于界面。

虽然我的意思与您的想法不同,但从您的回答来看,答案可能是“不可能的”。请看我的更新:我的意思是在每次使用中检查注释完整性,而不是在所有使用中。由于注释被设计为独立的实体,后者是非法的。
// correctly annotated class
@Foos({@Foo(number=0, name="first"), @Foo(number=1,name="second")})
class MyCorrectAnnotatedClass {
}

// incorrectly annotated class 1
// number starts from 1 not form 0
@Foos({@Foo(number=1, name="first"), @Foo(number=2,name="second")})
class MyIncorrectAnnotatedClass1 {
}

// incorrectly annotated class 2
// number sequence has missed 1
@Foos({@Foo(number=0, name="first"), @Foo(number=2,name="second")})
class MyIncorrectAnnotatedClass2 {
}